Alan
Alan

Reputation: 2086

C# Console App - Modify Permissions

I have a Windows Service that checks for a web API for updates and when it finds them it downloads them, unzips them to a temp directory and then launches a separate console app that stops the Windows Service and copies all the files overwriting the old ones.

The issue is that the updater doesn't have permission to overwrite the files in that directory, c:\Program Files (x86)\myApp, unless it runs as Administrator. I have tried to add the .manifest file with requestedExecutionLevel requireAdministrator to the update script, but that doesn't do anything. The Service that launches it is running as LocalSystem, so I tried asInvoker, but that didn't work either.

The interesting thing is that part of the script has a StreamWriter to log events. I am able to create a file in the directory, but then don't have rights to edit that file.

I originally had the updater being launched from a WPF section of the app rather than the Windows Service, but then realized that if no user was logged in, then it wouldn't update. Is there a way around this? Here is the relevant snippet I am working with:

// 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
    }
}

The Console.WriteLine() used to write to a file, but it broke there too so I changed it.

Upvotes: 0

Views: 2286

Answers (1)

Kishore
Kishore

Reputation: 693

I have the following code to grant access to the Folder I created.

Need to use the namespace "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; 
}

Upvotes: 2

Related Questions