How to give Read/Write permissions to a Folder during installation using .NET

后端 未结 9 1514
不思量自难忘°
不思量自难忘° 2020-11-27 14:33

I have a Setup project that I have build using Visual Studio 2010.

The installer works fine in terms of installing the application and all its dependencies into thei

相关标签:
9条回答
  • 2020-11-27 15:20

    I stumbled also into filesystem permissions while trying to write into SQLite database after application was installed.

    As mentioned in this thread, data files can be placed into user's AppData-folder instead of modifying permissions in Program Files and such. AppData also has user permissions set to allow writing as default.

    In Setup project, this is done by adding "User's Application Data Folder" into setup File System, under which application folder can be created. Database file is then added into this application folder. Application folder with database file will be created during setup inside AppData-folder.

    Code for creating a database connection string pointing to AppData-folder is as follows:

    public static Environment.SpecialFolder DataPath = Environment.SpecialFolder.ApplicationData;
    public static string ConnectionString = "Data Source=" + Environment.GetFolderPath(DataPath) + "\\ApplicationName\\database.SQLite";
    

    I used this solution with Visual Studio 2019.

    0 讨论(0)
  • 2020-11-27 15:24

    As it has been mentioned above Users group does not have write permission in Program Files. If you don't want to deal with installer class or Wix (if it is a simple program), just prefer installing your software under a Windows Volume.

    I am talking about Visual Studio Setup Wizard: Change Application Folder 'DefaultLocation' Property from [ProgramFilesFolder] to [WindowsVolume][Manufacturer][ProductName] in File System on Target Machine.

    0 讨论(0)
  • 2020-11-27 15:29
    private static void GrantAccess(string file)
    {
        bool exists = System.IO.Directory.Exists(file);
        if (!exists)
        {
            DirectoryInfo di = System.IO.Directory.CreateDirectory(file);
            Console.WriteLine("The Folder is created Sucessfully");
        }
        else
        {
            Console.WriteLine("The Folder already exists");
        }
        DirectoryInfo dInfo = new DirectoryInfo(file);
        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);
       
    }
    

    The above code will set the access rights of the folder to full control/ read-write to every user (everyone).

    0 讨论(0)
提交回复
热议问题