C# 文件夹创建不是创建所有文件夹,而是只在某些机器上创建

C# Folder creation not creating all folders, but only on some machines

我正在为工作编写自定义应用程序。该应用程序将为我们的客户项目创建一个文件夹结构。我正在创建程序的机器在从代码 window 进行调试时和在构建调试编译二进制文件后都可以工作。创建文件夹没有问题。另一台测试机,一台旧的win7机器,工作正常,没有问题。

我将二进制应用程序文件交给我的同事进行测试,在尝试创建下一部分文件夹时抛出错误。 (最后出错)

他安装了 .Net 4.8(在 4.7.2 上是 运行)。我已经检查并打开了注册表中的 LongFolderPath 标志,但它在我的主电脑上没有激活(而不是在 win7 机器上)。我在他的机器上安装了 VS,这样我们就可以单步执行代码,发现下面的代码有错误(已标注)。

这里是添加文件夹结构的完整代码:https://pastebin.com/E26UFeuz

这里是在那台机器上失败的相关部分的剪辑。

siteTypeName = "NSB";
projectPath = "C:\Test\";

private void createDirectoryStructure(string siteNumber, string siteName, string siteProject)
{
...
    string[] siteProjectFolderTemplate = {   
                                         "100 - Existing Data",
                                         "200 - Site Walk Info",
                                         "300 - Drawings",
                                         "400 - Engineering",
                                         "500 - Photo Sims",
                                         "600 - Surveys",
                                         "700 - RFDS",
                                        "1000 - File Reveiw",
                                        "2000 - Leasing",
                                        "3000 - Zoning",
                                        "4000 - Permitting",
                                        "5000 - Subcontractor Services",
                                        "6000 - NTP",
                                        "7000 - SAQ File Closeout Docs",
                                        "8000 - Submittal Documents and QCs",
                                        "9000 - SCIP"
    };

...
//This is the section that breaks.
    for (int j = 0; j < siteProjectFolderTemplate.Length; j++)
    {
        System.IO.Directory.CreateDirectory(projectPath +
                                            cboClient.Text + "\" +
                                            cboMarket.Text + "\" +
                                            siteTypeName + "\" +
                                            siteNumber + "_" + siteName + "\" + siteProject + 
                                            "\" + siteProjectFolderTemplate[j]
                                            );
    }
...
}

错误消息是这样说的:

************** Exception Text **************
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Test\Client\Market\NSB\KSMO-P-041370_A\L2500 GSM 0 - Existing Data'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
   at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
   at System.IO.Directory.CreateDirectory(String path)
   at AE_CreateFolderStructures.frm_CreateDirectoryStructure.createDirectoryStructure(String siteNumber, String siteName, String siteProject) in C:\Code Projects\AE-CreateFolderStructures\AE-CreateFolderStructures\frm_CreateDirectoryStructure.cs:line 547
   at AE_CreateFolderStructures.frm_CreateDirectoryStructure.btnImportCSV_Click(Object sender, EventArgs e) in C:\Code Projects\AE-CreateFolderStructures\AE-CreateFolderStructures\frm_CreateDirectoryStructure.cs:line 917
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

所以回顾一下 3 台机器,在 1 台机器上崩溃并出现上述错误,似乎无法在某个点后创建文件夹。

有什么想法吗?

路径是在我们进行的过程中创建的。它是“\L2500 GSM \”部分末尾的 space。我通过将 .Trim() 添加到路径构建的每个组件来修复它。

功劳归功于 panoskarajohn