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.