Greg Owen
Over 27 Years of Programming Experience
Programming Examples, Portfolio, and More
Home

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
Some words from the sponsors...

Add a C# Dropdown to a Bootstrap Modal Dialog

This example combines C# and Bootstrap to create a bootstrap modal dialog containing an asp dropdown and an asp button to retrieve...

Continue Reading

Use C# to Populate Dropdown Lists With XML

The code below opens an xml file, reads the data from the xml file, dynamically adds a label for the dropdown list, and adds the dropdown list to the asp.net page...

Continue Reading

Add Style Properties to ASP.Net Controls to Improve Appearance

The default formatting for ASP.Net controls is very dated.  The default formatting is basically the same as the formatting in Visual Basic 1.0 ...

Continue Reading

Export To A Text File From A DataGridView Control

Add a button control to Form1.
Add a DataGridView Control to Form1....

Continue Reading