C# Environment.SpecialFolder.ApplicationData 返回错误的文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16943707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Environment.SpecialFolder.ApplicationData returns the wrong folder
提问by Bastianon Massimo
I have a strange problem: my .NET 4.0 WPF application is saving data to the ApplicationData folder.
我有一个奇怪的问题:我的 .NET 4.0 WPF 应用程序正在将数据保存到 ApplicationData 文件夹。
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\myProgram\";
99.9% of the cases are working great, but on some computers it returns the wrong folder - instead of returning the user folder it returns another folder:
99.9% 的案例运行良好,但在某些计算机上它返回错误的文件夹 - 而不是返回用户文件夹,而是返回另一个文件夹:
C:\Users\<user>\AppData\Roaming\myProgram\ --correct
C:\Users\s\AppData\Roaming\myProgram\ --wrong
The wrong folder has no write/read permission so my program doesn't work.
错误的文件夹没有写/读权限,所以我的程序不起作用。
It seems the program is running under a different user, but if I check the Task Manager the user is the logged one.
该程序似乎在不同的用户下运行,但是如果我检查任务管理器,则该用户是已登录的用户。
The problem seems to be occurring with domain users with few permissions.
问题似乎发生在权限很少的域用户身上。
回答by Alex
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
do you create a text file to write that you want??
你创建一个文本文件来写你想要的吗??
and also you need to assign a variable, for example:
并且您还需要分配一个变量,例如:
String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
if(File.Exists(path + "filetowrite.log"))
{
......................
}
also before to check the file, check if directory existe to be more sure. greetings i hope help you.
在检查文件之前,请检查目录是否存在以更确定。问候我希望对你有帮助。

