0
System.IO.File.WriteAllBytes(NetworkPath, Encoding.UTF8.GetBytes(stringBuilder.ToString())); 

When running this code in Visual Studio, I get Access Denied.

However if I navigate to Network Path with File Explorer, I can right click and create Files and Folders with the same User account that is running Visual Studio 2019 and the code above.

software is fun
  • 7,286
  • 18
  • 71
  • 129
  • Try running vs as administrator – 3xGuy Sep 27 '21 at 16:14
  • Is the file opened by some other (or your) app? – Steeeve Sep 27 '21 at 16:29
  • 1
    What sort of app are you creating? For instance a Web app may run as a special account, without network rights – Hans Kesting Sep 27 '21 at 16:30
  • It's txt files being created but the files never get created so ... no it's not being held open by another application. – software is fun Sep 27 '21 at 16:33
  • Impersonating some account? The code shown doesn't have any problems writing to a network path. Are you using UNC path or a mapped network drive? – Steeeve Sep 27 '21 at 16:40
  • @Steeeve I am using a DFS path \\servername\foldername\file.txt – software is fun Sep 27 '21 at 16:53
  • @softwareisfun What happens if you [touch](https://stackoverflow.com/questions/30011267/create-an-empty-file-on-the-commandline-in-windows-like-the-linux-touch-command) that DFS path from the command line? – Tech Inquisitor Sep 27 '21 at 17:08
  • @TechInquisitor everything works from Windows or the Operating System. It's from C# (Visual Studio), I get Access Denied. – software is fun Sep 27 '21 at 18:30
  • The comment about project type is probably relevant. – Tech Inquisitor Sep 27 '21 at 18:32
  • OT simpler suggestion: [File.WriteAllText](https://learn.microsoft.com/en-us/dotnet/api/system.io.file.writealltext?view=net-5.0#System_IO_File_WriteAllText_System_String_System_String_System_Text_Encoding_) - not that that will help you with your network issues, alas – Hans Kesting Sep 29 '21 at 06:48

0 Answers0