C# 试图编辑文件时拒绝访问ProgramFiles
我正在试图编辑程序文件中的配置文件。这些配置文件由Windows服务使用。详情如下:C# 试图编辑文件时拒绝访问ProgramFiles,c#,file,uac,C#,File,Uac,我正在试图编辑程序文件中的配置文件。这些配置文件由Windows服务使用。详情如下: 我无法更改要编辑的文件的位置 UAC关闭了 以防万一,app.manifest已被编辑,因此它以管理员身份运行 代码如下: public void UpdateDNSNameInConfigFile(string v, ConfigFileOption cf) { string ConfigFilePath = Environment.GetFolderPath(System.Env
- 我无法更改要编辑的文件的位置
- 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);
}
public void UpdateDNSNameInConfigFile(字符串v,ConfigFileOption cf)
{
字符串ConfigFilePath=Environment.GetFolderPath(System.Environment.SpecialFolder.ProgramFiles)+@“\\”;
string ConfigFileContents=string.Empty;
字符串DNSName=v.Replace(“/ucf:,”);//从值中删除命令行参数
开关(cf)
{
案例ConfigFileOption.CobanSystem:
ConfigFilePath=ConfigFilePath+@“\.System”;
打破
案例ConfigFileOption.Agent\u AVL:
ConfigFilePath=ConfigFilePath+@“\.Agent.AVL”;
打破
案例ConfigFileOption.Agent\u数据库:
ConfigFilePath=ConfigFilePath+@“\.Agent.DB”;
打破
案例ConfigFileOption.Agent\u DVD:
ConfigFilePath=ConfigFilePath+@“\.Agent.DVD”;
打破
案例ConfigFileOption.Agent\u文件代理:
ConfigFilePath=ConfigFilePath+@“\.Agent.FileAgent”;
打破
案例ConfigFileOption.Agent\u日志:
ConfigFilePath=ConfigFilePath+@“\.Agent.Log”;
打破
案例ConfigFileOption.Agent\u流媒体:
ConfigFilePath=ConfigFilePath+@“\.Agent.Streaming”;
打破
案例ConfigFileOption.Agent\u侦听器:
抛出新的NotImplementedException();
案例ConfigFileOption.Agent\u化身:
抛出新的NotImplementedException();
案例ConfigFileOption.Agent\u磁带:
抛出新的NotImplementedException();
}
ConfigFileContents=File.ReadAllText(ConfigFilePath);
ConfigFileContents=ConfigFileContents.Replace(“,v”);
writealText(ConfigFilePath,ConfigFileContents);
}
有什么想法吗?尝试更改文件系统权限以获取(写入)对该文件的访问权限。如果您的系统受到外部或未经授权访问的保护,您还可以允许用户everyone更改文件(写入权限)。我在文件夹中添加了我自己的“everyone”和“匿名登录”。没有一个奏效。代码在“ConfigFileContents=File.ReadAllText(ConfigFilePath);”处失败。但是我可以用streamreader读取文件。您是否尝试授予所有权限?@ernest如果使用streamreader可以读取此文件,为什么不改为使用它?@rekire是的。他们有完全的权限。@Rafael我在玩弄它。我只是用一个StreamReader来测试。我试图找出如何读取数据,然后编辑一行特定的代码。“代码失败”,代码如何失败?