尝试编辑文件ProgramFiles时访问被拒绝

时间:2012-12-21 18:27:24

标签: c# file uac

我正在尝试编辑程序文件中的配置文件。这些配置文件由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);
    }

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试更改文件系统权限以获取(写入)对该文件的访问权限。如果您的系统受到外部或未授权访问的保护,您还可以允许用户所有人更改文件(写入权限)。

相关问题