ARTICLE

Store file in specific folder using directory class

Posted by Sapna Articles | ADO.NET in VB.NET July 29, 2011
First directory create folder with Files name then after that file will store in that folder.
 
Reader Level:

In this article, I am going to create a folder and also store file in this folder.
Strings are immutable. You can use String as an identifier. The contents of a string object cannot be changed after the object is created.
Path class used to perform operation on String instance that contain full path information of file or directory. GetFileName method returns the file name with extension of specified path to Path class.
AppDomain used as a container for code and date in .NET runtime. It is represented by AppDomain object. AppDomain is a virtual location in memory where a process runs. AppDomain provide isolation, security and unloading boundaries for executing managed code. AppDomains are created using the CreateDomain method. AppDomain instances are used to load and execute assemblies. When an AppDomain is no longer in use, it can be unloaded.
Directory class expose methods to create, delete, EnumerateDirectories, EnumerateFiles, Exists, move etc. operations to directories and subdirectories.
Exists method use to check that directory or folder exists or not.
CreateDirectory method used to create directory, we can call CreateDirectory method directly from Directory class.

Code:

FilePage.aspx


<%
@ Page Language="VB" AutoEventWireup="false" CodeFile="FilePage.aspx.vb" Inherits="FilePage" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>

</
head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:FileUpload ID="FileUpload1" runat="server" />
        <asp:Button ID="Button1" runat="server"
            Text="Add" onclick="Button1_Click" />
    </div>
   </form>

</
body>
</
html>

FilePage.aspx.vb

Imports
System.IO
Partial
Class FilePage
    Inherits System.Web.UI.Page
   Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Dim strFilename As [String] = Path.GetFileName(FileUpload1.FileName)
        Dim StrFilePathName As [String] = [String].Empty
        Dim strBaseDir As [String] = AppDomain.CurrentDomain.BaseDirectory + "Files"
        If Not Directory.Exists(strBaseDir) Then
            Directory.CreateDirectory(strBaseDir)
        End If
        StrFilePathName = strBaseDir & "\" & strFilename
        FileUpload1.SaveAs(StrFilePathName)
        If Not FileUpload1.HasFile Then
            Return
        End If
        Response.Redirect("~/Notification.aspx")
    End Sub

End
Class

Notification.aspx

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Notification.aspx.vb" Inherits="Notification" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<
head runat="server">
    <title></title>

</
head>
<
body>
    <form id="form1" runat="server">
   <div>
    <h1>Your file loaded successfully in Files folder</h1>
        <asp:Button ID="Button1" runat="server" Text="Button" />
    </div>
    </form>

</
body>
</
html>

Notification.aspx.vb

Partial Class Notification
    Inherits System.Web.UI.Page
    Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
        Response.Redirect("~/FilePage.aspx")
    End Sub

End
Class

Output :

First click on browse button. Select any file. Then click on Add button (see figure 1)

directory class

Figure 1

You will move to another page, that page look like below figure 2.

directory class

Figure 2

Click on button to move back to FilePage.aspx

See Figure 3. Now your file is added properly  in your application. First refresh the application.

directory class

Figure 3




 

Login to add your contents and source code to this article
share this article :
post comment
 

I am new in programming and im trying to explore.. can i please have the source code of this? thank you

Posted by Morie Cuaton Sep 07, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor