C Sharp File Functions
C# File Functions
The example below shows different ways to use the Copy and Move functions associated with
the System.IO class library in Visual Studio. It is written using C#.
In order to test, copy and paste the code below into a Windows Form in a C# application,and call the CopyFiles() routine.
private void CopyFiles()
{
//Create a file to be copied
System.IO.StreamWriter swFile = new System.IO.StreamWriter("C:\\testfile.txt swFile.WriteLine("test"); //This line writes a line of text to the newly created file.
swFile.Close();
//Check to see if a new directory exists, if not create a new directory
if (System.IO.Directory.Exists("C:\newdir false)
{
System.IO.Directory.CreateDirectory("c:\\newdir }
//Copy file to a new directory
//System.IO.File.Copy(sourceFileName, destFileName,overwrite(true, false))
System.IO.File.Copy("c:\\testfile.txt:\\newdir\\testfile.txt",true //Copy file to a new directory and rename file
System.IO.File.Copy("c:\\testfile.txt:\\newdir\\testfile_bak.txt",true //Make a copy of the file with a new name in the same directory
System.IO.File.Copy("c:\\testfile.txt","c:\\testfile_bak.txt",trueame the original file
System.IO.File.Move("c:\\testfile.txt:\\testfile1.txt");
//Move the original file to the new directory
System.IO.File.Move("c:\\testfile1.txt:\\newdir\\testfile1.txt");
}
#CSharp #VisualStudio