gpt4 book ai didi

c# - %AllUsersProfile%(%PROGRAMDATA%) 给出重复的文件路径

转载 作者:行者123 更新时间:2023-12-02 11:19:15 25 4
gpt4 key购买 nike

我有一个用 C# 编写的应用程序,我正在寻求将一些信息写入隐藏的 ProgramData,以便从应用程序的前端和后端访问相同的连接字符串。

我使用路径变量访问目录,如下所示:

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;
}

这似乎工作正常。但是,我的问题是,当我使用此路径变量时: %AllUsersProfile%(%PROGRAMDATA%)相反,它扩展为非法(且冗余)文件路径:C:\ProgramData(C:\ProgramData)\但是,我认为后一个路径变量是正确的全名。难道只是我用错了?我需要确保所有用户都可以访问此连接信息,仅使用 %PROGRAMDATA% 就可以吗?我正在使用 Windows 7,以防相关。

最佳答案

来自here :

FOLDERID_ProgramData / System.Environment.SpecialFolder.CommonApplicationData

The user would never want to browse here in Explorer, and settings changed here should affect every user on the machine. The default location is %systemdrive%\ProgramData, which is a hidden folder, on an installation of Windows Vista. You'll want to create your directory and set the ACLs you need at install time.

所以,只需使用%PROGRAMDATA%,或者更好:

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)

关于c# - %AllUsersProfile%(%PROGRAMDATA%) 给出重复的文件路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13711284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com