Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

How to move and copy files in ASP.NET

Author: Rachel Nicole

“File” or “FileInfo” classes are used to create, copy and move files. This article will show how you can create, copy and move files using both classes.

 

Both “File” and “FileInfo” classes are in “System.IO” namespace. So you must have to import or use this namespace to work with files. The code snippet will also clarify that we can use methods of “File” class without creating instances of this class. These methods are static so it provides more simplicity to the code. When you create an instance of “FileInfo” class, the security check is done only once while it is done every time when you call “File” class methods so it is another difference between “File” and “FileInfo” classes. 

 

C#

//use namespace
using System.IO;
 
//creating file paths with names
string sourcefilePath = Server.MapPath("~/NewFolder1/TextFile.txt");
string destFilePath = Server.MapPath("~/NewFolder2/TextFile.txt");
 
//using FileInfo class
FileInfo fileInfo = new FileInfo(sourcefilePath);
//to create file
fileInfo.Create();
if (fileInfo.Exists) {
    //to copy file
    fileInfo.CopyTo(destFilePath);
    fileInfo.MoveTo(destFilePath);
}
//using File class
//to create file
File.Create(sourcefilePath);
if (File.Exists(sourcefilePath)) {
    //to copy file
    File.Copy(sourcefilePath, destFilePath);
    //to move file
    File.Move(sourcefilePath, destFilePath);
}
 
VB.NET
 
'Import namespace
Imports System.IO
 
'creating file paths with names
Dim sourcefilePath As String = Server.MapPath("~/NewFolder1/TextFile.txt")
Dim destFilePath As String = Server.MapPath("~/NewFolder1/TextFile.txt")
'using FileInfo class
Dim fileInfo As FileInfo = New FileInfo(sourcefilePath)
'to create file
fileInfo.Create()
If fileInfo.Exists Then
     'to copy file
     fileInfo.CopyTo(destFilePath)
     'to move file
     fileInfo.MoveTo(destFilePath)
End If
 
'using File class
'to create file
File.Create(sourcefilePath)
If File.Exists(sourcefilePath) Then
     'to copy file
     File.Copy(sourcefilePath, destFilePath)
     'to move file
     File.Move(sourcefilePath, destFilePath)
End If
 
First of all, I am creating source and destination paths of the file. Source path means the folder where you want to create file (or file already exists there) with the file name you want to create (or you want to copy or move). Destination path means the folder where you want to copy or move the file with file name (you can provide different file name here). For this example, I have two folders at the root of my application with “NewFolder1” and “NewFolder2” names and name of the file that I want to create copy or move) is “TextFile.txt”. 
 
If you want to use “FileInfo” class then you have to create an instance of the class by providing full path with name of the file in constructor. To create file, just call the Create() method of the “FileInfo” object. If you want to use “File” class then simply call the Create() method of “File” class and provide source file path in argument of the method.
 
If you want to check that the file exists then call the Exists() method of the both classes. Notice the difference again, you have to call Exists() method of FileInfo class with its object and call Exists() method of File class by providing source file path as argument.
 
FileInfo class has the CopyTo() and MoveTo() methods which takes single parameter of destination file path to copy and move file. File class has the Copy() and Move() methods that takes two parameters; source file path and destination file path.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Reading and Writing Text Files in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net