none
Creating multiple folders RRS feed

  • Question

  • For a long time I had to create one folder at a time in c#. Is there a better way of creating folders then this,

    string dName = MLHSetting.Default.currGame;
                DirectoryInfo sd = new DirectoryInfo(FullPathWrite + "maps");
                if (!sd.Exists)
                    sd.Create();
    
                DirectoryInfo sd2 = new DirectoryInfo(FullPathWrite + @"maps\" + dName);
                if (!sd2.Exists)
                    sd2.Create();
    
    


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Wednesday, March 20, 2019 1:58 PM

Answers

  • OK, Karen. I'll have to try it again, Last time I tried it it just won't create folders like that. Unless something was changed.

    Thanks


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    The only thing that would be an issue would be Windows policies and/or user permissions.

    One way to test this is to try and create multiple folders under C:\Users\YourProfilleName\Documents where the user has rights and permissions by default to create folders. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Joesoft11a Friday, March 22, 2019 12:58 PM
    Wednesday, March 20, 2019 5:20 PM
    Moderator

All replies

  • Look at Directory.CreateDirectory,

    Given say C:\Path1\Path2\etc each folder will be created to create etc folder.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 20, 2019 3:01 PM
    Moderator
  • Karen, Thank you. It's just that's not what I had in mine. All the folders have names. Not numbers. I need a way to create named folders all on one line.

    directoryinfo Dr = new directoryinfo(@"c:\temp\temp2\temp3");
    
    if(!dr.exists())
    {
       dr.create();
    }
    

    The ideas is to create multi named folders. As I stated above. The code above with crash because it can't create all of those folders at the same time.


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Wednesday, March 20, 2019 3:43 PM
  • string sPath = @"c:\temp\temp2\temp3";
    DirectoryInfo di = Directory.CreateDirectory(sPath);
    works normally on my OS (Windows 10)
    Wednesday, March 20, 2019 3:50 PM
  • Before posting I ran the following w/o any crashes/exceptions thrown. Final result all folders were created.

    Directory.CreateDirectory(@"C:\oed\karen\payne");

    If I run again, no issues. If we peek at the source that might help to understand the method.

    [SecuritySafeCritical]
    public static DirectoryInfo CreateDirectory(string path)
    {
      if (path == null)
        throw new ArgumentNullException(nameof (path));
      if (path.Length == 0)
        throw new ArgumentException(Environment.GetResourceString("Argument_PathEmpty"));
      return Directory.InternalCreateDirectoryHelper(path, true);
    }
    
    [SecurityCritical]
    internal static DirectoryInfo UnsafeCreateDirectory(string path)
    {
      if (path == null)
        throw new ArgumentNullException(nameof (path));
      if (path.Length == 0)
        throw new ArgumentException(Environment.GetResourceString("Argument_PathEmpty"));
      return Directory.InternalCreateDirectoryHelper(path, false);
    }
    
    [SecurityCritical]
    internal static DirectoryInfo InternalCreateDirectoryHelper(string path, bool checkHost)
    {
      string checkPermissions = Directory.GetFullPathAndCheckPermissions(path, checkHost, FileSecurityStateAccess.Read);
      Directory.InternalCreateDirectory(checkPermissions, path, (object) null, checkHost);
      return new DirectoryInfo(checkPermissions, false);
    }
    
    internal static string GetFullPathAndCheckPermissions(string path, bool checkHost, FileSecurityStateAccess access = FileSecurityStateAccess.Read)
    {
      string fullPathInternal = Path.GetFullPathInternal(path);
      Directory.CheckPermissions(path, fullPathInternal, checkHost, access);
      return fullPathInternal;
    }
    
    [SecuritySafeCritical]
    internal static void CheckPermissions(string displayPath, string fullPath, bool checkHost, FileSecurityStateAccess access = FileSecurityStateAccess.Read)
    {
      if (CodeAccessSecurityEngine.QuickCheckForAllDemands())
        FileIOPermission.EmulateFileIOPermissionChecks(fullPath);
      else
        FileIOPermission.QuickDemand((FileIOPermissionAccess) access, Directory.GetDemandDir(fullPath, true), false, false);
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, March 20, 2019 3:59 PM
    Moderator
  • OK, Karen. I'll have to try it again, Last time I tried it it just won't create folders like that. Unless something was changed.

    Thanks


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Wednesday, March 20, 2019 5:00 PM
  • OK, Karen. I'll have to try it again, Last time I tried it it just won't create folders like that. Unless something was changed.

    Thanks


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    The only thing that would be an issue would be Windows policies and/or user permissions.

    One way to test this is to try and create multiple folders under C:\Users\YourProfilleName\Documents where the user has rights and permissions by default to create folders. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by Joesoft11a Friday, March 22, 2019 12:58 PM
    Wednesday, March 20, 2019 5:20 PM
    Moderator
  • Hi

    Is your problem solved? If so, please post "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Jack

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, March 22, 2019 5:48 AM
    Moderator
  • I will be coding later on this morning so I should know by then. I'm not in any rush to complete the update. So I'll let you know. since I have have other issues that I wanted to update.

    The next issue is image files. My app has an option that let's the users view images for one game that has 3 images that show the user the maps or terrains that the user can pick when creating his missions. The images are all on the hdd and I wanted to see if there was anther way to do this. Like make a library file and add all the images to that file in some way and then let the app access that library and browse each image.

    Joe


    http://www.df-barracks.com Delta Force Barracks
    http://www.starfiresoft.com Starfire Software

    Friday, March 22, 2019 9:38 AM