本文介绍了Environment.SpecialFolder.ApplicationData 返回错误的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个奇怪的问题:我的 .NET 4.0 WPF 应用程序正在将数据保存到 ApplicationData 文件夹中.

I have a strange problem: my .NET 4.0 WPF application is saving data to the ApplicationData folder.

 Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\myProgram\\";

99.9% 的案例运行良好,但在某些计算机上它返回错误的文件夹 - 而不是返回用户文件夹,而是返回另一个文件夹:

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:

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.

推荐答案

创建一个文本文件来编写吗?

Do you also create a text file to write?

如果是这样,请保存一个文件,例如:

If so save a file such as:

String path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

var filePath = Path.Combine(path, "filetowrite.log"); // Handles whether there is a `\` or not.

if (File.Exists(filePath))
{
     ......................
}

注意在进行任何文件操作之前,应该检查目录是否存在.

Note also before doing any file operations, one should check if directory exists.

这篇关于Environment.SpecialFolder.ApplicationData 返回错误的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:09