Create , delete , copy folder, files in C# (win app)




01. How to check whether the perticular directory is existing with a given path.

System.IO.Directory.Exists(destPath)

this is given boolean result such is directory is exist as "true" and else as " false"

Ex:- Imagine you want to check C:\windows\test is exists.
then the path should be "C:\\windows\\test"

Then

System.IO.Directory.Exists("C:\\windows\\test")

--------------------------------------------------------------------

02. Now you want to create a folder with a given path.
    Below is the code for it.

 string folderFullPath = "C:\\windows\\test";
 System.IO.Directory.CreateDirectory(folderFullPath);

//this will create a folder named "test" in the C:\windows\

--------------------------------------------------------------------

03. Below code show how to copy files of a directory to a another directory

 if (System.IO.Directory.Exists(@"C:\\windows\\originalFolder"))
            {
string fileName="";
                string[] files = System.IO.Directory.GetFiles(sourcePath);
                string destFile = System.IO.Path.Combine("C:\\windows\\NewFolder", fileName);

                foreach (string s in files)
                {
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine("C:\\windows\\NewFolder", fileName);
                    System.IO.File.Copy(s, "C:\\windows\\NewFolder", true);
                    System.IO.File.Delete(s);
                }
            }

This code uses foreach iteration to catch all the files of initial folder to be copied.
Also by the line System.IO.File.Delete(s); it delete the previous original file at the
original folder.

Note : All of these code is my developing stuff. Therefore if you have any doubt free to contact me.

Comments

Popular Posts