【问题标题】:How to create a folder or copy a file in ProgramData while installing an application in Visual Studio 2017?在 Visual Studio 2017 中安装应用程序时,如何在 ProgramData 中创建文件夹或复制文件?
【发布时间】:2018-10-03 09:05:34
【问题描述】:

我在 Visual Studio 2017 社区版的“Visual Studio 安装程序”上为 WPF 应用程序创建了一个安装程序。在我的应用程序中,有一个设置文件需要写入权限,因为在应用程序运行期间设置会发生变化。

我想在“Programe Files”文件夹中安装我的应用程序,但同时,我想在“ProgramData”位置创建一个文件夹来复制我的设置文件。

我无法创建文件夹并将文件复制到“ProgramData”,我已经浏览了很多资源,但最后没有运气。

我在链接 here 中找到了相关答案,但它与 Visual Studio 2010 相关,我在 Visual Studio 2017 的 DefaultLocation 中没有找到提到的选项 [CommonAppDataFolder]。

你能帮我实现这个目标吗?感谢您的帮助。

【问题讨论】:

    标签: windows-installer setup-project


    【解决方案1】:

    虽然 DefaultLocation ComboBox 中没有此选项,但您可以手动提供 [CommonAppDataFolder] 选项。提供此选项后,自定义文件夹将被复制到 ProgramData 文件夹。

    但是这样创建的文件夹或文件只有只读权限。为了使文件夹/文件也具有可写权限,我使用了替代方法。我使用了安装程序文件而不是上面的。下面是提供文件夹和文件完全访问控制的代码示例。

        [RunInstaller(true)]
        public partial class Installer1 : Installer
        {
            private string fullPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + "\\YourFolder";
    
            public override void Install(IDictionary savedState)
            {
                base.Install(savedState);
                //Add custom code here
    
    
                if (!Directory.Exists(fullPath))
                {
                    Directory.CreateDirectory(fullPath);
    
                    // Create files on inside the folder to make them writable
                       ... 
                }
    
                // https://stackoverflow.com/questions/9108399/how-to-grant-full-permission-to-a-file-created-by-my-application-for-all-users
    
                DirectoryInfo dInfo = new DirectoryInfo(fullPath);
                DirectorySecurity 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);
            }
    
            public override void Rollback(IDictionary savedState)
            {
                base.Rollback(savedState);
                //Add custom code here
    
                if (Directory.Exists(fullPath))
                {
                    Directory.Delete(fullPath, true);
                }
            }
    
            public override void Commit(IDictionary savedState)
            {
                base.Commit(savedState);
                //Add custom code here
            }
    
            public override void Uninstall(IDictionary savedState)
            {
                Process application = null;
                foreach (var process in Process.GetProcesses())
                {
                    if (!process.ProcessName.ToLower().Contains("yourprocessname"))
                        continue;
                    application = process;
                    break;
                }
    
                if (application != null && application.Responding)
                {
                    application.Kill();
                    base.Uninstall(savedState);
                }
    
                if (Directory.Exists(fullPath))
                {
                    Directory.Delete(fullPath, true);
                }
            }
        }
    

    【讨论】:

    • 您的这个应用程序是否以管理员权限/权限运行?我不认为 CommonAppDataFolder 中的文件对普通用户是可写的?
    • @SteinÅsmul 是的,你是对的。我使用了不同的方法,我也更新了我的答案。如果您知道更好的方法,请与我分享。谢谢
    【解决方案2】:

    设置文件模板:我们倾向于多次重复此建议,但您打算在应用程序运行期间修改的任何文件,我们建议您从通过在 ProgramFiles 下安装只读模板文件来解决部署问题,您的应用程序会复制这些文件然后存储在用户配置文件中。在创建文件后,设置将永远不会干扰文件。没有意外的设置文件清除。在用户配置文件中,文件将具有完全写入权限。

    共享设置文件:如果您希望所有用户共享一个设置文件(在大多数情况下是个坏主意),您可以将其复制到 CommonAppDataFolder 在您为使用 ACL 权限的普通用户打开的用于写入访问的文件夹中。不是很好,但有可能。在 WiX、Advanced Installer 和 Installshield 中,您可以将 ACL 更改为内置功能,但您已经使用 Visual Studio Installer 项目进行了自定义操作。好的,但是please read these notes on limitations of Visual Studio Installer projects

    相关链接

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-10
      相关资源
      最近更新 更多