0

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

有任何想法吗?

4

1 回答 1

0

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

于 2012-12-21T18:29:21.173 回答