Move a directory to a new path
.Net framework DirectoryInfo.MoveTo() method allow us to move a DirectoryInfo instance and its contents to
a new path. DirectoryInfo class MoveTo() method exists in System.IO namespace.
DirectoryInfo MoveTo() method require to pass a parameter named 'destDirName'. this 'destDirName 'parameter value data type is System.String which represents the name and path to which to move this directory. the destination can be an existing directory to which we want to add the source directory as a sub directory.
MoveTo() method throw ArgumentNullException exception, if the 'destDirName' is null. it throw ArgumentException, if the 'destDirName' is an empty string. method throw IOException, if an attempt was made to move a directory to a different volume or 'destDirName' already exists or caller are not authorized to access this path or source and destination directory have the same name.
MoveTo() method throw SecurityException, if the caller does not have the required permission. method also throw DirectoryNotFoundException, if the destination directory cannot be found.
.Net framework Directory class Directory.Move() method also allow us to move a file or directory and its contents to a new location. this method has two required parameter named 'sourceDirName' and 'destDirName'.
the following asp.net c# example code demonstrate us how can we move a directory to a specified path programmatically at run time in an asp.net application.
DirectoryInfo MoveTo() method require to pass a parameter named 'destDirName'. this 'destDirName 'parameter value data type is System.String which represents the name and path to which to move this directory. the destination can be an existing directory to which we want to add the source directory as a sub directory.
MoveTo() method throw ArgumentNullException exception, if the 'destDirName' is null. it throw ArgumentException, if the 'destDirName' is an empty string. method throw IOException, if an attempt was made to move a directory to a different volume or 'destDirName' already exists or caller are not authorized to access this path or source and destination directory have the same name.
MoveTo() method throw SecurityException, if the caller does not have the required permission. method also throw DirectoryNotFoundException, if the destination directory cannot be found.
.Net framework Directory class Directory.Move() method also allow us to move a file or directory and its contents to a new location. this method has two required parameter named 'sourceDirName' and 'destDirName'.
the following asp.net c# example code demonstrate us how can we move a directory to a specified path programmatically at run time in an asp.net application.
DirectoryMove.aspx
<%@ Page Language="C#" %>
<%@ Import Namespace="System.IO" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if(!this.IsPostBack)
{
string path = Request.PhysicalApplicationPath + "FishImage";
string pathTo = Request.PhysicalApplicationPath + "Images\\FishImage";
TextBox1.Text = path;
TextBox2.Text = pathTo;
}
}
protected void Button1_Click(object sender, System.EventArgs e) {
DirectoryInfo srcDirectory = new DirectoryInfo(TextBox1.Text);
string desDirectory = TextBox2.Text.ToString();
try
{
srcDirectory.MoveTo(desDirectory);
Label1.Text = "Directory moved";
}
catch(Exception ex)
{
Label1.Text = "an error occured!<br/>"+ ex.ToString();
}
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>How to move directory in asp.net programmatically</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:Maroon">asp.net example: directory move</h2>
<asp:Label
ID="Label1"
runat="server"
Font-Size="Larger"
ForeColor="CornflowerBlue"
>
</asp:Label>
<br /><br />
<asp:Label
ID="Label2"
runat="server"
Text="Source Directory"
ForeColor="Crimson"
Font-Bold="true"
>
</asp:Label>
<asp:TextBox
ID="TextBox1"
runat="server"
ReadOnly="true"
BackColor="Crimson"
ForeColor="AliceBlue"
>
</asp:TextBox>
<br />
<asp:Label
ID="Label3"
runat="server"
Text="Move To Directory"
ForeColor="Crimson"
Font-Bold="true"
>
</asp:Label>
<asp:TextBox
ID="TextBox2"
runat="server"
ReadOnly="true"
BackColor="Crimson"
ForeColor="AliceBlue"
Width="200"
>
</asp:TextBox>
<br /><br />
<asp:Button
ID="Button1"
runat="server"
Font-Bold="true"
ForeColor="IndianRed"
Text="Move Directory"
OnClick="Button1_Click"
/>
</div>
</form>
</body>
</html>




- How to create a directory programmatically in asp.net
- How to delete a directory programmatically in asp.net
- How to get whether the specified directory exists or not in asp.net
- How to get sub directory list under a directory in asp.net programmatically
- How to get file list under a directory in asp.net programmatically