Store file in specific folder using directory class in VB.NET

First directory create folder with Files name then after that file will store in that folder.
  • 3139

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)

partial-class-notification-in-vb.net.gif

Figure 1

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

file-upload-in-vb.net.gif

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.

solution-explore-in-vb.net.gif

Figure 3

Categories

More Articles

© 2013 dotNetheaven. All rights reserved.