我有一个用C#编写的应用程序,我正在设法向隐藏的ProgramData写入一些信息,以便从应用程序的前端和后端访问相同的连接字符串。
我使用path变量访问目录,如下所示:
private bool ProgramDataWriteFile(string contentToWrite)
{
try
{
string strProgramDataPath = "%PROGRAMDATA%";
string directoryPath = Environment.ExpandEnvironmentVariables(strProgramDataPath) + "\\MyApp\\";
string path = Environment.ExpandEnvironmentVariables(strProgramDataPath)+"\\MyApp\\ConnectionInfo.txt";
if (Directory.Exists(directoryPath))
{
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
file.Write(contentToWrite);
file.Close();
}
else
{
Directory.CreateDirectory(directoryPath);
System.IO.StreamWriter file = new System.IO.StreamWriter(path);
file.Write(contentToWrite);
file.Close();
}
return true;
}
catch (Exception e)
{
}
return false;
}这看起来工作正常。然而,我的问题是,当我使用path变量:%AllUsersProfile%(%PROGRAMDATA%)时,它扩展为一个非法(和冗余的)文件路径:C:\ProgramData(C:\ProgramData)\。然而,我认为后一个path变量是正确的全名。我是不是用错了?我需要确保所有用户都可以访问此连接信息,仅使用%PROGRAMDATA%就可以吗?我使用的是Windows7,以防万一。
发布于 2012-12-05 04:32:50
来自here
FOLDERID_ProgramData / System.Environment.SpecialFolder.CommonApplicationData
用户永远不会想要在资源管理器中浏览此处,此处更改的设置应该会影响机器上的每个用户。在Windows Vista安装中,默认位置为%systemdrive%\ProgramData,这是一个隐藏文件夹。您需要在安装时创建自己的目录并设置所需的ACL。
所以,只需使用%PROGRAMDATA%,或者更好:
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)https://stackoverflow.com/questions/13711284
复制相似问题