0

I'm trying to do a very VERY basic operation:

    public async Task WriteAsync(string path)
    {
        // Write to text file

        try
        {
            lines = "Hello, World";

            File.WriteAllText("Assets\\Write.txt", lines); 

        }
        catch (System.Exception e)
        {
            lines = e.Message;
        }
    }

I am getting the following exception:

{System.UnauthorizedAccessException: Access to the path '.......AppX\Assets' is denied.

at System.IO.Win32FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)

at System.IO.Win32FileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)

at System.IO.MultiplexingWin32WinRTFileSystem.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, FileStream parent)

at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options)

at System.IO.File.InternalWriteAllText(String path, String contents, Encoding encoding)

at System.IO.File.WriteAllText(String path, String contents)

at SynSent18.KnowledgeBase.d__8.MoveNext()}

I have Assets set to Everyone full control. I have even attempted to pre-create the file in Windows and give THAT full control. The error persists.

Ideas?

Daniel Möller
  • 84,878
  • 18
  • 192
  • 214
Steve C
  • 1
  • 1
  • 2
    Error message is clear . `Access to the path '.......AppX\Assets' is denied.` even you say otherwise. See the *whole path* and check the security – Eser Apr 14 '18 at 21:10
  • Use full path name including drive. The folder AppX either doesn't exist or you do not have permission to write. – jdweng Apr 14 '18 at 21:23
  • Provide the exact path where you would like to store like "C:\\YourFolderName\\YourFileName.txt" and check whether you have enough user privilege to access the folder location. – kaarthick raman Apr 14 '18 at 21:32
  • The path definitely exists, and Assets is a directory with full privilege to everyone. The exact same code is being used to read from that directory moments earlier (it's a debug routine, read -from/write-to and diff files. I can manually create files there and I can set THOSE to Everyone=Full Control. Even if I pre-create the file with full control to everyone, it still fails. I'm running VS 2017 (15.6.6) as admin. How do I tell what user the code is running as? I would assume it's my windows login credentials. – Steve C Apr 14 '18 at 22:37
  • Exact path shouldn't be necessary and isn't ideal since the full path includes my Google Drive mount point and changes for the 3 machines I do dev work on. If I hard code the full path, 2 machines will always fail. And yes, I've tried c:\\temp and given THAT full permissions and that fails too. – Steve C Apr 14 '18 at 22:41
  • Further info: I created a new project in C++ without closing VS: int main() { FILE * f = NULL; fopen_s(&f, "C:\\Temp\\Write.txt", "w"); fprintf(f, "Hello, World\n"); return 0; } -- and this works perfectly. So it's something about C#, not the directory. – Steve C Apr 14 '18 at 22:48
  • Ok, one more piece of data. I created a c# console app: static void Main(string[] args) { string s = "Hello World!"; Console.WriteLine(s); File.WriteAllText(@"C:\Temp\Write2.txt", s + "\n"); } And that works fine. So it's only C# Universal Windows apps that are failing. Ideas? – Steve C Apr 15 '18 at 00:11
  • The folder is read only to your app due to sandboxing. – Sheng Jiang 蒋晟 Apr 15 '18 at 02:28
  • Feature, not a bug. One of the nice UWP features is that the user can easily reset the app back to the original install state. This requires you to only write to the directories that the OS can correctly clean up. https://learn.microsoft.com/en-us/uwp/api/windows.storage.applicationdata – Hans Passant Apr 15 '18 at 08:31

0 Answers0