简体   繁体   中英

C# File and Renaming Automatically from 1?

I wrote this code in c# for change location of folder and all sub-folders of this folder. I want to change name of all folders when copy it to destination. for example I want to change name from 1 to .... . how should I change my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace Folder
{
    class clssMoveFolder
    {

        string temppath;
        public string StrtxtSource;
        public string destDirName;

        public void Directorycopy(string sourceDirName, string destDirName, bool cutSubDirs, string strExtension)
        {
           try
            {

                DirectoryInfo dir = new DirectoryInfo(sourceDirName);
                DirectoryInfo[] dirs = dir.GetDirectories();
                // If the source directory does not exist, throw an exception.

                if (!dir.Exists)
                {
                    throw new DirectoryNotFoundException("Source directory does not exist or could not be found: " + sourceDirName);
                }
                FileInfo[] files = dir.GetFiles();
                if (!Directory.Exists(destDirName))
                {
                    Directory.CreateDirectory(destDirName);
                }
                //Get the file contents of the directory to copy.
                for (int a = 0; a < files.Length; ++a)
                {
                    // Create the path to the new copy of the file.
                    if (files[a].Extension == "."+strExtension  )
                    {
                        temppath = Path.Combine(destDirName, files[a].Name);
                        files[a].MoveTo(temppath);
                    }
                    else if (files[a].Extension == strExtension)
                    {
                        temppath = Path.Combine(destDirName, files[a].Name);
                        files[a].MoveTo(temppath);
                    }
                    else if (strExtension == "AllFiles")
                    {
                        temppath = Path.Combine(destDirName, files[a].Name);
                        files[a].MoveTo(temppath);
                    }
                    else
                        files[a].Delete();
                   dir.Refresh();
                }
                FileInfo[] filesCheck = dir.GetFiles();
                if (dirs.Length == 0 && filesCheck.Length == 0 && dir.Name != StrtxtSource)
                {
                    dir.Delete();
                }

                // If copySubDirs is true, copy the subdirectories.
                if (cutSubDirs)
                {

                    foreach (DirectoryInfo subdir in dirs)
                    {

                        // Copy the subdirectories.

                        string temppath= Path.Combine(destDirName, subdir.Name);

                        Directorycopy(subdir.FullName, temppath,cutSubDirs,strExtension);
                    }
                }
            }
            catch (Exception e)
            {

                MessageBox.Show(e.ToString());
            }
        }

    }
}

If you just want to rename a file after copying it you could instead directly copy it under a different name. Eg instead of copying a.txt and rename it to b.txt afterwards, you could just copy the original a.txt as b.txt to the destination directory.

Renaming can be done by using the File.Move method.

File.Move("a.txt", "b.txt");

If you want to be able to do this even if someone else copies files into a directory (eg by using the windows explorer or files written by another application), you may want to take a closer look at the FileSystemWatcher . You can use it to monitor changes in a specified directory (optionally including it's subdirectories) and react on these changes.

...
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"D:\SomeTestDirectory";
watcher.Filter = "*.*";
watcher.NotifyFilter = NotifyFilters.FileName; // Trigger only on events associated with the filename
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;            // Starts the "watching"
...

private static void OnChanged(object source, FileSystemEventArgs e)
{
   // Do whatever you want here, e.g. rename the file.
   Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM