[英]Where should I store my log file so a user has write permissions to it?
[英]My winform app uses xml files to store data, where should I store them so Vista users can write to them?
我的 winform 应用程序使用 xml 文件来存储数据,我应该将它们存储在哪里以便 Vista 用户可以写入它们?
谢谢
使用Environment.GetFolderPath
以独立于操作系统的方式获取最合适的文件夹。
特别是,您需要以下 SpecialFolder 值之一:
ApplicationData
- 如果文件是漫游的,每个用户,并且仅供应用程序使用,不代表用户可能关心的文档。LocalApplicationData
- 如果文件是非漫游的,每个用户的,并且仅供应用程序使用,不代表用户可能关心的文档。CommonApplicationData
- 如果文件是漫游的,对所有用户通用并且仅供应用程序使用,不代表用户可能关心的文档。 注意:在 Vista 上,这映射到C:\ProgramData
,默认情况下对普通用户是只读的(因为更改其中的文件可能会影响管理员使用的程序的行为)。 您可以显式更改应用程序子文件夹的权限,也可以选择其他选项之一。MyDocuments
- 如果文件是每个用户的并代表文档。 请注意,没有像CommonDocuments
这样的SpecialFolder
枚举值可以代表机器范围的文档存储,即使有一个文件夹旨在像一个一样服务( C:\Documents and Settings\All Users\Documents
on XP 和C:\Users\Public\Documents
)。 如果要写入这些位置,您必须自己查找操作系统版本并选择适当的文件夹。
在内部Environment.GetFolderPath
使用 Win32 API SHGetFolderPath
。 SHGetFolderPath
使用的枚举为您提供了几个其他特殊文件夹(包括 Common Documents)的众所周知的位置。 您可以直接使用SHGetFolderPath
; 您可以在 PInvoke.net 上找到它的p/invoke 定义和相应的CSIDL 枚举定义。
您还可以使用IsolatedStorage
。 但是,它是每个用户不可漫游的,具有有限的配额,并且用户无法从 Windows Explorer 轻松访问。 因此,它实际上是相当于SpecialFolder.ApplicationData
的中/低信任度。
Environment.GetFolderPath 应该告诉你 Windows 希望你在哪里存储东西。
对于用户特定的数据,
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
通常返回 C:\Users\%user%\AppData\Roaming
对于普通数据,
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
通常返回 C:\ProgramData。
这也将在 Windows 的旧版本下做正确的事情; XP 通常会返回 C:\Documents and Settings\%user%\Application Data 和 C:\Documents and Settings\All Users\Application Data。
尝试使用 .Net Framework 中的IsolatedStorage 。 它会为你完成这项工作。
该框架可以为您管理这些位置,而不是管理驱动器、文件夹和文件等。 它的目的是拥有一个您不必担心用户权限的区域。
以下代码序列直接来自 MSDN,但准确显示了您将如何使用这些文件。
using System;
using System.IO;
using System.IO.IsolatedStorage;
public class ReadingAndWritingToFiles{
public static int Main(){
// Get an isolated store for this assembly and put it into an
// IsolatedStoreFile object.
IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
// This code checks to see if the file already exists.
string[] fileNames = isoStore.GetFileNames("TestStore.txt");
foreach (string file in fileNames){
if(file == "TestStore.txt"){
Console.WriteLine("The file already exists!");
Console.WriteLine("Type \"StoreAdm /REMOVE\" at the command line to delete all Isolated Storage for this user.");
// Exit the program.
return 0;
}
}
writeToFile(isoStore);
Console.WriteLine("The file \"TestStore.txt\" contains:");
// Call the readFromFile and write the returned string to the
//console.
Console.WriteLine(readFromFile(isoStore));
// Exit the program.
return 0;
}// End of main.
// This method writes "Hello Isolated Storage" to the file.
private static void writeToFile(IsolatedStorageFile isoStore){
// Declare a new StreamWriter.
StreamWriter writer = null;
// Assign the writer to the store and the file TestStore.
writer = new StreamWriter(new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew,isoStore));
// Have the writer write "Hello Isolated Storage" to the store.
writer.WriteLine("Hello Isolated Storage");
writer.Close();
Console.WriteLine("You have written to the file.");
}// End of writeToFile.
// This method reads the first line in the "TestStore.txt" file.
public static String readFromFile(IsolatedStorageFile isoStore){
// This code opens the TestStore.txt file and reads the string.
StreamReader reader = new StreamReader(new IsolatedStorageFileStream("TestStore.txt", FileMode.Open,isoStore));
// Read a line from the file and add it to sb.
String sb = reader.ReadLine();
// Close the reader.
reader.Close();
// Return the string.
return sb.ToString();
}// End of readFromFile.
}
您希望您的应用程序数据是用户特定的吗? 然后你应该考虑把它放在C:\Users\%username%\%appname%\...
否则,@Mike_G 建议简单地将它放在与你的应用程序相同的目录中并没有错。
编辑:除了,正如您的评论所指出的那样, C:\Program Files\...
不可写,在这种情况下,我可能会考虑在所有情况下使其特定于用户(具有良好的默认值),除非我有一个好的有理由希望它合并在一个地方。 如果是这种情况,我会将其作为应用程序配置的一部分,并让用户确定他们想要(并且可以)保存数据的位置。
如果数据适用于所有用户,那么您可以尝试使用“共享文档”区域。 在 XP 上,它位于 C:\Documents and Settings\All Users\Documents。 我不确定 Vista,但它可能在 C:\Users 中。
为什么不在应用程序目录中(安装应用程序的目录相同)?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.