简体   繁体   English

在 UWP 应用程序中使用 File.WriteAllText 访问被拒绝,但在控制台应用程序中没有

[英]Acces denied with File.WriteAllText in UWP app, but not in console app

I'm trying to write a file from a UWP i'm developing.我正在尝试从我正在开发的 UWP 中编写一个文件。 But i keep getting an acces denied error.但我不断收到访问被拒绝的错误。

I've tried: Create a console app to write a file using the same filepath and this works.我已经尝试过:创建一个控制台应用程序来使用相同的文件路径编写一个文件,这很有效。 Resetting windwows rights (done by windows domain admin).重置 windwows 权限(由 windows 域管理员完成)。 Using different locations.使用不同的位置。 This doesnt help.这没有帮助。 I've set Visual Studio start as admin.我已将 Visual Studio 设置为以管理员身份启动。 This doesnt help either.这也无济于事。

errormessage:错误信息:

"System.UnauthorizedAccessException: Access to the path 'C:\Users\reinder\source\repos\UWPTeamAdministration\UWPTeamAdministration\bin\x86\Debug\AppX\entrypoint\players.json' is denied.\r\n at System.IO.FileStream.ValidateFileHandle(SafeFileHandle fileHandle)\r\n at System.IO.FileStream.CreateFileOpenHandle(FileMode mode, FileShare share, FileOptions options)\r\n at System.IO.File “System.UnauthorizedAccessException:访问路径 'C:\Users\reinder\source\repos\UWPTeamAdministration\UWPTeamAdministration\bin\x86\Debug\AppX\entrypoint\players.json' 被拒绝。\r\n 在 System.IO .FileStream.ValidateFileHandle(SafeFileHandle fileHandle)\r\n 在 System.IO.FileStream.CreateFileOpenHandle(FileMode 模式,FileShare 共享,FileOptions 选项)\r\n 在 System.IO.File

I really dont know where to go from here.我真的不知道从这里到 go 哪里。 It doesnt seem to me that it is a permissions issue.在我看来,这不是权限问题。 From what i read i understand by setting VS to start as admin ensures that the app i'm developing also is run in admin mode.据我了解,我通过将 VS 设置为以管理员身份启动来确保我正在开发的应用程序也在管理员模式下运行。

Any ideas?有任何想法吗?

I'have editted the question because the problem description seems slightly different then in my original post.我已经编辑了这个问题,因为问题描述似乎与我原来的帖子略有不同。

You should not expect to be able to write to the application folder.您不应期望能够写入应用程序文件夹。 Typically apps are installed at locations that do not have write access.通常,应用程序安装在没有写入权限的位置。 If this is a file that you want to reuse then look at some of the other "special" folders that you could use (most likely ApplicationData ).如果这是一个您想要重复使用的文件,那么请查看您可以使用的其他一些“特殊”文件夹(很可能是ApplicationData )。

Note that in any case you should use a sub-folder specific to your app since other apps can read/write to these folders as well.请注意,在任何情况下,您都应该使用特定于您的应用程序的子文件夹,因为其他应用程序也可以读取/写入这些文件夹。

Not sure if I need to post this or not.不确定我是否需要发布这个。 Maybe other new programmers encounter this same problem and can use the info.也许其他新程序员遇到同样的问题并且可以使用该信息。

It seems to me and correct me if im wrong that reading and writing files from an UWP app are restricted to certain folders.在我看来,如果我错了,请纠正我,从 UWP 应用程序读取和写入文件仅限于某些文件夹。 Also the methods used to do this are async methods.用于执行此操作的方法也是异步方法。

I wasn't aware of the differences between a console app and UWP app at the time I asked.在我询问时,我并不知道控制台应用程序和 UWP 应用程序之间的区别。

I found the information on the websites below useful.我发现以下网站上的信息很有用。

https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files https://learn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files

https://www.tutorialspoint.com/windows10_development/windows10_development_file_management.htm# https://www.tutorialspoint.com/windows10_development/windows10_development_file_management.htm#

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM