Create Directory and Copy/Save Image file on fileName from directory

Looking for a code to copy a file. Then here is a solution for your. You can use the a built in function of File class present in System.IO namespace. Here In this article I will talk about this function and describe you how can you use this function. This function may not be successful if you don't have proper permissions to write files to the location where you are trying to copy the file. First, give the proper permission to write file(s).

Instructions:

Crate a windows project.
Paste image into Drive:\application folder path\bin\debug\image folder
Create a destination folder where you want to save your image on folder wise.
  1. Initialize three string variables.
    --One with the full name and path of the file you want to copy.
    --Second will be having the directory path where you want to copy the file.
    --Third variable will store the name of new file at the destination directory.
Here is the code
 
        private void btnCreateFolder_Click(object sender, EventArgs e)
        {
            foreach (string f in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "EmployeePhoto\\"))
            {
                try
                {
                    string[] FileName;
 
                  FileName = f.Split("file://%22.tochararray/.ToCharArray());
                    string str = FileName[FileName.Length - 1];
                    string[] substr = str.Split('.');
                    string dirStr = AppDomain.CurrentDomain.BaseDirectory + "EmployeePhoto\\" + substr[0];
                    string SourcePath = AppDomain.CurrentDomain.BaseDirectory + "EmployeePhoto\\" + str;
                    string DestinationPath = AppDomain.CurrentDomain.BaseDirectory + "EmployeeData\\" + substr[0] + "\\";
                    if (!Directory.Exists(DestinationPath))
                    {
                        Directory.CreateDirectory(DestinationPath);
                        if (!File.Exists(DestinationPath + str))
                        {
                            Image bitmap = Image.FromFile(SourcePath);
                            bitmap.Save(DestinationPath + str);
                            //File.Create(DestinationPath + str);
                        }
                        else
                        {
                            File.Replace(SourcePath, DestinationPath, "backup.jpg");
                        }
                    }
                    else
                    {
                        if (!File.Exists(dirStr + str))

                        {
                            File.Create(DestinationPath + str);
                        }
                        else
                        {
                            File.Replace(SourcePath, DestinationPath, "backup.jpg");
                        }
                    }
                }
                catch { }
            }
        }

     2. In foreach loop I am getting the all file name witch exist in this folder. I also check directory if exist or
         not using !File.Exists(DestinationPath + str) Function. Note File.Copy method work only if you
         provide the the full path of the source file and destination file including name of the file and extension.
         In the upper code, In variable source I have the full path of source file. But I have the directory
         where I want to save the file, file name but not the extension. Now As we are copying the file,
         extension of the file is supposed to be same as that of the source file. So, I am fetching the
         extension of the source file and concatenating that with the string we get by concatenating
         destination directory and name of the file.

     3. If you have the full path address of destination file in a variable, then you just can use the following
         statement File.Replace(SourcePath, DestinationPath, "backup.jpg");
     4. I am enclosing the function in Try block because error may occur during copying of the file. Error can
        occur because of number problems, and following are some of those errors.

  • Source File is not there in your directory.
  • There is no such directory present in your Computer from where you are trying to fetch the source file or where you want to save the destination file.
  • Source File directory or destination file directory is not ready to perform the operation.
  • You don't have permission to write something in the destination directory.
  • There is already a file exists in the destination directory with the same name and extension of your destination file.
With this blog I also attached source code, So you can download and test this.
Ebook Download
View all
Learn
View all