Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Allow Created Directory to be Written To?

Discussion in 'Scripting' started by JudahMantell, Aug 4, 2020.

  1. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Hey!
    So for my project I need to create a few folders that I can then use File.WriteAllText to add a text file in.
    I created the folders fine using Directory.CreateDirectory, but when I try to write text to it, I get this error:

    UnauthorizedAccessException: Access to the path 'C:\Users\ygman\Desktop\NewFolder' is denied.

    Is there a way to add the proper permissions to a newly created folder?

    As per a few other places I've looked, I tried this, but I'm still getting the error.
    Code (CSharp):
    1. DirectoryInfo rootPathInfo = Directory.CreateDirectory(m_directorypath);
    2. rootPathInfo.Attributes = FileAttributes.Normal;
    3. File.SetAttributes(rootPathInfo.FullName, File.GetAttributes(rootPathInfo.FullName) & ~FileAttributes.ReadOnly);
    Any ideas?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Your best bet for a writeable directory is going to be
    Application.persistentDataPath
    . Try making a folder there.
     
  3. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Thanks for the reply!
    I definitely see the advantages of that, but unfortunately, with my project I need the user to be able to choose where this/these folders are created.
    I'm using an in-game file browser to select the location, then I create the folders there.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,893
    Then it's pretty much outside of your application's control. The user will have to make sure they pick a directory that is writeable by whatever user they ran the application as.

    I believe typically the app runs as the user that started it though, so you might just have weird permissions settings on your Desktop or home directory. Check the windows permissions on those folders.
     
  5. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Strange, because I'm just testing by creating the folder on my desktop, which should have relatively open permissions.
    Even if I do change my own permissions, there's no guarantee that the end user will have the same situation, so that's something I need to figure out, if that does end up working.
     
  6. JudahMantell

    JudahMantell

    Joined:
    Feb 28, 2017
    Posts:
    476
    Found the solution, for anyone else with this problem:
    I was trying to write to the folder, not a file, which isn't possible.
    All I had to do is append the file name.

    I changed the line from this:
    Code (CSharp):
    1. File.WriteAllText(rootPathInfo.FullName, savedScene);
    To this, and it worked:
    Code (CSharp):
    1. File.WriteAllText(Path.Combine(rootPathInfo.FullName, fileName+extention), savedScene);
     
    zanemxx and PraetorBlue like this.