Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Saving File on Desktop ? [SOLVED]

Discussion in 'Scripting' started by Eldoir, Feb 27, 2015.

  1. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Hello everyone,

    I'm in trouble with this : I want to create a file where I write something, and save it on Desktop.

    I use this code :

    Code (csharp):
    1.  
    2. public void Save() {
    3.     DirectoryInfo info = Directory.CreateDirectory (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Desktop) + "\\App");
    4.     System.IO.File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+"\\App", "Test");
    5. }
    6.  
    The first line creates an "App" folder on Desktop. That part works.
    The problem is WriteAllText : it always says :
    "UnauthorizedAccessException: Access to the path 'C:\Users\Arthur\Documents\App' is denied.

    I checked Properties->Security of the folder App, and it is Read-Only. Hence it is created in runtime with code, I don't know how to set on Read/Write.

    Or is there a best solution ?

    Anything I found on the web didn't work for me, like this :

    Code (csharp):
    1.  
    2. public void Save() {
    3.         DirectoryInfo info = Directory.CreateDirectory (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Desktop) + "\\App");
    4.         SetAcl (info.FullName);
    5.         System.IO.File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+"\\App", "Test");
    6.     }
    7.  
    8.     bool SetAcl(string path)
    9.     {
    10.         FileSystemRights Rights = (FileSystemRights)0;
    11.         Rights = FileSystemRights.FullControl;
    12.  
    13.         // *** Add Access Rule to the actual directory itself
    14.         FileSystemAccessRule AccessRule = new FileSystemAccessRule("Users", Rights,
    15.                                                                    InheritanceFlags.None,
    16.                                                                    PropagationFlags.NoPropagateInherit,
    17.                                                                    AccessControlType.Allow);
    18.  
    19.         DirectoryInfo Info = new DirectoryInfo(path);
    20.         DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access);
    21.  
    22.         bool Result = false;
    23.         Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);
    24.  
    25.         if (!Result)
    26.             return false;
    27.  
    28.         // *** Always allow objects to inherit on a directory
    29.         InheritanceFlags iFlags = InheritanceFlags.ObjectInherit;
    30.         iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
    31.  
    32.         // *** Add Access rule for the inheritance
    33.         AccessRule = new FileSystemAccessRule("Users", Rights,
    34.                                               iFlags,
    35.                                               PropagationFlags.InheritOnly,
    36.                                               AccessControlType.Allow);
    37.         Result = false;
    38.         Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);
    39.  
    40.         if (!Result)
    41.             return false;
    42.  
    43.         Info.SetAccessControl(Security);
    44.  
    45.         return true;
    46.     }
    47.  
    This code gives me the following error, in addition to the first I described above :
    "Error CS1061 : Type 'System.IO.DirectoryInfo' does not contain a definition for 'GetAccessControl' and no extension method 'GetAccessControl' of type 'System.IO.DirectoryInfo' could be found (are you missing a using directive or an assembly reference ?"
    (The same for SetAccessControl)

    The link to the Microsoft Developer Network (https://msdn.microsoft.com/en-us/library/8e1fc3b8(v=vs.110).aspx) gave me the idea of including mscorlib.dll to my project, but then more and more errors appeared, so I don't think this was a good idea.

    So I'm really stucked, I don't know where to search right now.

    Is there anyone here who had the same issue ?

    Thanks in advance. :)
     
    Last edited: Feb 27, 2015
    RyanMartin likes this.
  2. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    Up !
    I really need help on this...
     
  3. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    You need to give WriteAllText a filename to write to. Right now you're just giving it the folder name but no file name, so it's trying to write the text to a file named "App" but it can't because there's already a folder with that name (the one you just created). Try this instead:
    Code (csharp):
    1. System.IO.File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+"\\App\\mytextfile.txt", "Test");
     
  4. Eldoir

    Eldoir

    Joined:
    Feb 27, 2015
    Posts:
    60
    ... I feel really dumb right now. Works like a charm. Thank you very much ! :)
     
    makeshiftwings likes this.
  5. Shinimini

    Shinimini

    Joined:
    Apr 19, 2020
    Posts:
    2
    I know this topic is very old, but the solution didn't work for me. After knocking myself out, I figured out I need to switch from "\\" to "/".