Search code examples
c#create-directory

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


I'm writing a custom application for work. The application will create a folder structure for our client projects. The machine I am creating the program on works both while in debug from the code window and after building a debug compile binary. Creates the folders no problem. Another test machine, an old win7 machine, works fine, no issues.

I hand the binary application file over to my coworker to test for me, it throws an error when trying to create the next section of folders. (error at the end)

He has .Net 4.8 installed (it's running on 4.7.2). I've checked and turned on the LongFolderPath flag in the registry, but it wasn't active on my main pc (and not in the win7 machine). I installed VS on his machine so we can step through the code and found it errors on the below code (notated).

here is the full code for adding the folder structure: https://pastebin.com/E26UFeuz

Here is a clip of the relevant sections that fail on that one machine.

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

the error message says this:

************** Exception Text **************
System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Test\Client\Market\NSB\KSMO-P-041370_A\L2500 GSM \100 - 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)

so to recap.. of 3 machines, crashes on 1 with the above error, can't seem to make the folders after a certain point.

Any ideas?


Solution

  • the path is created as we go.. it was the space at the end of the '\L2500 GSM \' part. I fixed it with .Trim() added to each component of the path build.

    Credit goes to panoskarajohn