如何在c#windows窗体应用程序中复制和重命名文件?

如何在c#windows窗体应用程序中复制和重命名文件?,c#,winforms,C#,Winforms,我想将服务器上的一个文件夹和其中的所有文件复制到c磁盘中。如果磁盘c中有相同的文件夹,我想更改磁盘c中文件夹的名称,将服务器上的文件夹复制到磁盘c string localFile = @"C:\42Apps"; string sourceFile = @"\\10.10.97.121\pckasabatch\42Apps"; bool dizinverisi = Directory.Exists(localFile);

我想将服务器上的一个文件夹和其中的所有文件复制到c磁盘中。如果磁盘c中有相同的文件夹,我想更改磁盘c中文件夹的名称,将服务器上的文件夹复制到磁盘c

            string localFile = @"C:\42Apps";
            string sourceFile = @"\\10.10.97.121\pckasabatch\42Apps";

            bool dizinverisi = Directory.Exists(localFile);
            if (localFile != null && localFile.Length >0)
            {
                int i = 0;

               Directory.Move(@"C:\42Apps", "42Apps(old" +i++ +")");
               //File.Move(sourceFile , localFile);  // second method
            }

            else { }

我会这样做:

string localDirectory = @"C:\"; // no need to get the folder name as it will be the same than the source one
string sourceDirectory = @"\\10.10.97.121\pckasabatch\42Apps";
string directoryName = Path.GetDirectoryName(sourceDirectory); // getting only the name of the folder

string tempDirPath = Path.Combine(localDirectory, directoryName); // creating a temp variable which will be used to check if the folder exists
string dirPath = tempDirPath; // saving for the renaming
string tempDirName = directoryName;

int inc = 0;
// we create a loop to check the first non existing folder name
While(Directory.Exists(tempDirPath))
{
    tempDirName = $"{directoryName}(old{i})";
    tempDirPath = Path.Combine(localDirectory, directoryName);
    i++;
}

if (inc != 0) // if the folder has to be renames
{
    Directory.Move(dirPath, tempDirPath);
}

Directory.Move(sourceDirectory, dirPath);
仅供参考,在代码中,您创建了一个忘记使用的布尔值
dizinverisi
。 此外,不要使用
if(localFile!=null&&localFile.Length>0)
,而是使用
if(String.IsNullOrEmpty(localFile))
,如果您希望路径来自不确定输入,可以在其后的条件中添加布尔值。 如果文件不存在,可以将此条件放在代码的开头并返回。
也许您也应该测试目标文件夹以确保它存在

我会这样做:

string localDirectory = @"C:\"; // no need to get the folder name as it will be the same than the source one
string sourceDirectory = @"\\10.10.97.121\pckasabatch\42Apps";
string directoryName = Path.GetDirectoryName(sourceDirectory); // getting only the name of the folder

string tempDirPath = Path.Combine(localDirectory, directoryName); // creating a temp variable which will be used to check if the folder exists
string dirPath = tempDirPath; // saving for the renaming
string tempDirName = directoryName;

int inc = 0;
// we create a loop to check the first non existing folder name
While(Directory.Exists(tempDirPath))
{
    tempDirName = $"{directoryName}(old{i})";
    tempDirPath = Path.Combine(localDirectory, directoryName);
    i++;
}

if (inc != 0) // if the folder has to be renames
{
    Directory.Move(dirPath, tempDirPath);
}

Directory.Move(sourceDirectory, dirPath);
仅供参考,在代码中,您创建了一个忘记使用的布尔值
dizinverisi
。 此外,不要使用
if(localFile!=null&&localFile.Length>0)
,而是使用
if(String.IsNullOrEmpty(localFile))
,如果您希望路径来自不确定输入,可以在其后的条件中添加布尔值。 如果文件不存在,可以将此条件放在代码的开头并返回。 也许您也应该测试目标文件夹,以确保它存在