简体   繁体   English

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

The same code, one on windows 10, the other on windows 7. The idea is to have a directory from a network drive replicate over to a local drive.相同的代码,一个在 windows 10 上,另一个在 windows 7 上。这个想法是让一个目录从网络驱动器复制到本地驱动器。 On windows 10, the machine I am writing it on, it works perfectly fine as intended.在我正在写它的机器 windows 10 上,它按预期工作得非常好。 On windows 7, the target machine, it 'works' but the sub folder structure is messed up.在目标机器 windows 7 上,它“工作”,但子文件夹结构混乱。

Example,例子,

C:\target -> the target location C:\target -> 目标位置

C:\targetNewFolderName1 -> What its being copied to C:\targetNewFolderName1 -> 它被复制到什么

C:\targetNewFolderName2 C:\targetNewFolderName2

C:\targetNewFolderNameN C:\targetNewFolderNameN

When it should be doing this below,(which it is, on windows 10, not on windows 7)当它应该在下面执行此操作时,(在 windows 10 上,而不是在 windows 7 上)

C:\target -> the target location C:\target -> 目标位置

C:\target\NewFolderName1 -> What its being copied to C:\target\NewFolderName1 -> 它被复制到什么

C:\target\NewFolderName2 C:\target\NewFolderName2

C:\target\NewFolderNameN C:\target\NewFolderNameN

Master is a network directory, @"\\server\fu\bar\target" master是一个网络目录,@"\\server\fu\bar\target"

Slave is a local directory, @"C:\target"从属是本地目录,@"C:\target"

These are passed to the function.这些被传递给 function。

Function header, private void CheckMasterToSlave(string MasterPath, string SlavePath, string BackupPath, string[] MasterFilesList, string[] SlaveFilesList) Function header, private void CheckMasterToSlave(string MasterPath, string SlavePath, string BackupPath, string[] MasterFilesList, string[] SlaveFilesList)

The below code snipit is within a foreach;下面的代码片段在一个 foreach 中; foreach (string master in MasterFilesList). 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.");
   }

I do not understand why this works as intended on windows 10 but moving it to windows 7 causes this issue.我不明白为什么这在 windows 10 上按预期工作,但将其移动到 windows 7 会导致此问题。 What would be causing this and how can I stop the new folder from appending to the parent folder in windows 7?什么会导致这种情况,如何阻止新文件夹附加到 windows 7 中的父文件夹?

Use Path.Combine to build a path name from different path components instead of just using string concatenation.使用Path.Combine从不同的路径组件构建路径名,而不仅仅是使用字符串连接。

Alright, I am stupid and forgot to change to release.好吧,我傻了,忘记改release了。 When changes that NineBerry mentioned were made.当进行了 NineBerry 提到的更改时。 It did work.它确实奏效了。 I still do not understand why the original did work on windows 10 but not on windows 7. Especially since the BackupFile portion does the same thing as the old 'wrong' way.我仍然不明白为什么原版在 windows 10 上有效,但在 windows 7 上无效。特别是因为 BackupFile 部分与旧的“错误”方式做同样的事情。 But both work now.但现在两者都有效。 Regardless, here is the updated bit.无论如何,这是更新的位。

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.");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 (Unity C#)Directory.CreateDirectory()在应该创建文件夹时 - (Unity C#) Directory.CreateDirectory() not creating folder when it should C#Directory.CreateDirectory停止工作 - C# Directory.CreateDirectory stop working C#ODBC驱动程序在Win10上可以正常工作,但在Win7上不能正常工作 - C# ODBC Driver working fine on Win10 but not Win7 无法以编程方式查找Win10启动文件夹C# - Cant Find Win10 Startup Folder Programmatically C# File.Copy()或Directory.CreateDirectory()方法的路径变量中的额外斜杠 - Extra slashes in path variable of File.Copy() or Directory.CreateDirectory() methods Directory.CreateDirectory不会创建文件夹 - Directory.CreateDirectory does not create a folder Directory.CreateDirectory 并不总是创建文件夹 - Directory.CreateDirectory does not always create the folder C# 文件夹浏览对话框不显示网络共享文件夹 win10 - C# Folder browse Dialog not showing Network shared folders win10 C#-Directory.CreateDirectory()中的UnauthorizedAccessException - C# - UnauthorizedAccessException in Directory.CreateDirectory() 使用 Directory.Delete() 和 Directory.CreateDirectory() 覆盖文件夹 - Using Directory.Delete() and Directory.CreateDirectory() to overwrite a folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM