【问题标题】:C# Console App - Modify PermissionsC# 控制台应用程序 - 修改权限
【发布时间】:2018-11-23 17:56:01
【问题描述】:

我有一个 Windows 服务,它检查 Web API 的更新,当它找到它们时,它会下载它们,将它们解压缩到临时目录,然后启动一个单独的控制台应用程序,它会停止 Windows 服务并复制所有文件以覆盖旧的。

问题是更新程序无权覆盖该目录c:\Program Files (x86)\myApp 中的文件,除非它以管理员身份运行。我尝试将带有requestedExecutionLevel requireAdministrator.manifest 文件添加到更新脚本中,但这没有任何作用。启动它的服务是作为 LocalSystem 运行的,所以我尝试了asInvoker,但这也没有用。

有趣的是脚本的一部分有一个 StreamWriter 来记录事件。我可以在目录中创建一个文件,但没有权限编辑该文件。

我最初是从应用程序的 WPF 部分而不是 Windows 服务启动更新程序,但后来意识到如果没有用户登录,那么它就不会更新。有没有解决的办法?这是我正在使用的相关 sn-p:

// Load the update path
Console.WriteLine("Loading update path from UpdatePath.txt"); 
string[] zipDir = File.ReadAllLines("UpdatePath.txt");
Console.WriteLine("Loading update from: " + zipDir);

// Copy all the updated files to the current directory (this will include the renamed service.exe)
Console.WriteLine("Copying zip contents to current directory ( " + Directory.GetCurrentDirectory() + " )");
string[] files = Directory.GetFiles(zipDir[0], "*.*", SearchOption.TopDirectoryOnly);
foreach (string f in files)
{
    string fileName = Path.GetFileName(f);
    if(fileName != "Configuration.ini") {
        Console.WriteLine("Copying " + f + " =>" + Directory.GetCurrentDirectory() +"//" + fileName);
        File.Copy(f, fileName, true);  //Breaks right here
    }
}

Console.WriteLine() 曾经写入文件,但它也损坏了,所以我更改了它。

【问题讨论】:

  • 好。升级绝对是需要最终用户许可的事情。不一定是管理员特权,但这仍然是一件的事情。在没有用户或管理员注意或同意的情况下尝试安装某些东西不是一个好主意
  • 无论如何,所有这些都是已解决的问题 - 您可以使用著名的自动更新程序 Squirrel。它最近出现在in this DotNetRocks 剧集中。 Windows 本身可以将 MSI 安装程序部署到域中的任何计算机,无论用户是否通过组策略、Powershell DSC 等登录。
  • 在任何情况下,Windows 自 Windows 95 天以来都禁止修改 Program Files,以防止行为不端的应用程序破坏系统或其他应用程序。用户特定的数据、文档应该存储在用户的个人资料或My Documents 中。这也是众所周知的。应用程序数据应该存储在ProgramData 中。例如 Chocolatey 将所有包存储在 ProgramData\chocolatey 文件夹中
  • 谢谢大家。 Panagiotis,该应用程序安装在无头服务器上,要访问办公室,通常必须联系 IT 公司进行服务。作为一项新服务,我们对应用程序进行半频繁的更改,并且不能每次都涉及最终用户。我正在研究松鼠,它看起来不错。
  • 没关系。持续部署或 Powershell DSC 无需用户交互即可部署到多个服务器。问题是安装程序不合适,而不是权限或用户

标签: c# .net file-permissions auto-update


【解决方案1】:

我有以下代码来授予对我创建的文件夹的访问权限。

需要使用命名空间“using System.Security.AccessControl;

 private static void CreateSomeFolder()
 {
        try
        {
            //Create some folder and grant full permissions
            var someFolder = "SomeDataFolderPath";
            if (Directory.Exists(someFolder)) return;
            Directory.CreateDirectory(someFolder);
            GrantAccess(someFolder);
        }
        catch (Exception ex)
        {
            log.Error("Folder Creation Error - ", ex);
        }
 }

private static bool GrantAccess(string fullPath)
{
   var dInfo = new DirectoryInfo(fullPath);
   var dSecurity = dInfo.GetAccessControl();
   dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
   dInfo.SetAccessControl(dSecurity);
   return true; 
}

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
  • 1970-01-01
  • 2010-12-18
  • 1970-01-01
相关资源
最近更新 更多