C# File.copy 和 Directory.CreateDirectory 在 Win10 上工作,但在 Win7 中它将文件夹附加到父文件夹

C# File.copy and Directory.CreateDirectory working on Win10 but in Win7 it appends folder to parent folder

相同的代码,一个在windows 10,另一个在windows 7。 这个想法是将目录从网络驱动器复制到本地驱动器。 在 windows 10,我正在写它的机器上,它按预期工作得很好。 在windows 7,目标机器上,它'works'但是子文件夹结构乱七八糟。

示例,

C:\target -> 目标位置

C:\targetNewFolderName1 -> 它被复制到什么地方

C:\targetNewFolderName2

C:\targetNewFolderNameN

它应该在下面执行此操作的时间,(它是在 windows 10 上,而不是在 windows 7 上)

C:\target -> 目标位置

C:\target\NewFolderName1 -> 它被复制到什么地方

C:\target\NewFolderName2

C:\target\NewFolderNameN

master是网络目录,@"\\server\fu\bar\target"

slave是本地目录,@"C:\target"

这些被传递给函数。

函数头,private void CheckMasterToSlave(string MasterPath, string SlavePath, string BackupPath, string[] MasterFilesList, string[] SlaveFilesList)

下面的代码片段在 foreach 中; foreach(MasterFilesList 中的字符串母版)。

    log.Info(master + " doesnt exist, copying");
    string directoryCheck = (SlavePath + master.Substring(MasterPath.Length)).Substring(0, 
                            (SlavePath + master.Substring(MasterPath.Length)).LastIndexOf("\"));
    if (!Directory.Exists(directoryCheck))
    {
       log.Debug(directoryCheck + " Directory not present, touching.");
       try
       {
           Directory.CreateDirectory((SlavePath + 
                                    master.Substring(MasterPath.Length)).Substring(0, (SlavePath + 
                                    master.Substring(MasterPath.Length)).LastIndexOf("\")));
       }
       catch
       {
           log.Error(master + " directory failed to be created in slave environment.");
       }
   }
   try
   {
       File.Copy(master, SlavePath + master.Substring(MasterPath.Length));
       log.Info(SlavePath + master.Substring(MasterPath.Length) + " Successfully created.");
       BackupFile(master.Replace(MasterPath, SlavePath), BackupPath, SlavePath);
   }
   catch
   {
       log.Error(master + " failed to copy, backup has been halted for this file.");
   }

我不明白为什么这会在 windows 10 上按预期工作,但将其移至 windows 7 会导致此问题。 是什么原因导致的?如何阻止新文件夹附加到 windows 7 中的父文件夹?

使用 Path.Combine 从不同的路径组件构建路径名,而不是仅仅使用字符串连接。

好吧,我傻了,忘记换release了。当进行 NineBerry 提到的更改时。它确实起作用了。 我仍然不明白为什么原来的在 windows 10 上工作但在 windows 7 上不工作。特别是因为 BackupFile 部分与旧的 'wrong' 方式做同样的事情。但现在两者都有效。 无论如何,这是更新的位。

log.Info(master + " doesnt exist, copying");

string[] EndDirectoryFile = master.Substring(MasterPath.Length).Split('\');

string[] EndDirectory = new string[EndDirectoryFile.Length-1];
for (int i = 0; i < EndDirectoryFile.Length - 1; i++)
{
    EndDirectory[i] = EndDirectoryFile[i];
}

string directoryCheck = Path.Combine(SlavePath, Path.Combine(EndDirectory));

if (!Directory.Exists(directoryCheck))
{
    log.Debug(directoryCheck + " Directory not present, touching.");
    try
    {
        Directory.CreateDirectory(directoryCheck);
    }
    catch
    {
        log.Error(master + " directory failed to be created in slave environment.");
    }
}
try
{
    File.Copy(master, SlavePath + master.Substring(MasterPath.Length));
    log.Info(SlavePath + master.Substring(MasterPath.Length) + " Successfully created.");
    BackupFile(master.Replace(MasterPath, SlavePath), BackupPath, SlavePath);
}
catch
{
    log.Error(master + " failed to copy, backup has been halted for this file.");
}