Saving File on Desktop ? [SOLVED]

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 :

public void Save() {
    DirectoryInfo info = Directory.CreateDirectory (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Desktop) + "\\App");
    System.IO.File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+"\\App", "Test");
}

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 :

public void Save() {
        DirectoryInfo info = Directory.CreateDirectory (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Desktop) + "\\App");
        SetAcl (info.FullName);
        System.IO.File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+"\\App", "Test");
    }

    bool SetAcl(string path)
    {
        FileSystemRights Rights = (FileSystemRights)0;
        Rights = FileSystemRights.FullControl;
 
        // *** Add Access Rule to the actual directory itself
        FileSystemAccessRule AccessRule = new FileSystemAccessRule("Users", Rights,
                                                                   InheritanceFlags.None,
                                                                   PropagationFlags.NoPropagateInherit,
                                                                   AccessControlType.Allow);
 
        DirectoryInfo Info = new DirectoryInfo(path);
        DirectorySecurity Security = Info.GetAccessControl(AccessControlSections.Access);
 
        bool Result = false;
        Security.ModifyAccessRule(AccessControlModification.Set, AccessRule, out Result);
 
        if (!Result)
            return false;
 
        // *** Always allow objects to inherit on a directory
        InheritanceFlags iFlags = InheritanceFlags.ObjectInherit;
        iFlags = InheritanceFlags.ContainerInherit | InheritanceFlags.ObjectInherit;
 
        // *** Add Access rule for the inheritance
        AccessRule = new FileSystemAccessRule("Users", Rights,
                                              iFlags,
                                              PropagationFlags.InheritOnly,
                                              AccessControlType.Allow);
        Result = false;
        Security.ModifyAccessRule(AccessControlModification.Add, AccessRule, out Result);
 
        if (!Result)
            return false;
 
        Info.SetAccessControl(Security);
 
        return true;
    }

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. :slight_smile:

1 Like

Up !
I really need help on this…

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:

System.IO.File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop)+"\\App\\mytextfile.txt", "Test");
6 Likes

… I feel really dumb right now. Works like a charm. Thank you very much ! :slight_smile:

1 Like

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 “/”.

1 Like