我正在尝试在程序文件中编辑配置文件。这些配置文件由 Windows 服务使用。这是详细信息:
- 我无法更改要编辑的文件的位置
- UAC 关闭
- 以防万一, app.manifest 已被编辑,因此它以管理员身份运行
这是代码:
public void UpdateDNSNameInConfigFile(string v, ConfigFileOption cf)
{
string ConfigFilePath = Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles) + @"\<redacted>\<redacted>";
string ConfigFileContents = string.Empty;
string DNSName = v.Replace("/ucf:", ""); //Remove the command line arg from the value
switch (cf)
{
case ConfigFileOption.CobanSystem:
ConfigFilePath = ConfigFilePath + @"\<redacted>.<redacted>System";
break;
case ConfigFileOption.Agent_AVL:
ConfigFilePath = ConfigFilePath + @"\<redacted>.Agent.AVL";
break;
case ConfigFileOption.Agent_DB:
ConfigFilePath = ConfigFilePath + @"\<redacted>.Agent.DB";
break;
case ConfigFileOption.Agent_DVD:
ConfigFilePath = ConfigFilePath + @"\<redacted>.Agent.DVD";
break;
case ConfigFileOption.Agent_FileAgent:
ConfigFilePath = ConfigFilePath + @"\<redacted>.Agent.FileAgent";
break;
case ConfigFileOption.Agent_Log:
ConfigFilePath = ConfigFilePath + @"\<redacted>.Agent.Log";
break;
case ConfigFileOption.Agent_Streaming:
ConfigFilePath = ConfigFilePath + @"\<redacted>.Agent.Streaming";
break;
case ConfigFileOption.Agent_Listener:
throw new NotImplementedException();
case ConfigFileOption.Agent_InCar:
throw new NotImplementedException();
case ConfigFileOption.Agent_Tape:
throw new NotImplementedException();
}
ConfigFileContents = File.ReadAllText(ConfigFilePath);
ConfigFileContents = ConfigFileContents.Replace("<redacted>", v);
File.WriteAllText(ConfigFilePath, ConfigFileContents);
}
有任何想法吗?