C# Service Run Code as Administrator

Walter Stifter (Privat) 41 Reputation points
2021-10-05T09:47:45.44+00:00

What I know,
if I have Code with Shell32 it have to run it in a STA Task. --> no Problem
the Code have to run as "admin" --> no Problem in an Desktop-App

Now i try to build a service, what i have to do that the shell32 Section is executed as "admin"

private static void MTAThread(string file, string oldfile, string changetype)
{

     object[] args = new object[] { file, oldfile, changetype, 20 };
     if (Thread.CurrentThread.GetApartmentState() == ApartmentState.STA)
     {
        GetLastUser(args);
     }
     else
     {
         Thread staThread = new Thread(new ParameterizedThreadStart(GetLastUser));
         staThread.SetApartmentState(ApartmentState.STA);
         staThread.Start(args);
         staThread.Join();
     }
 }

private static void GetLastUser(object param)
{
try
{
object[] args = (object[])param;
string fileName = Path.GetFileName((string)args[0]);
string folderName = Path.GetDirectoryName((string)args[0]);
string oldFilename = (string)args[1];
string _change = (string)args[2];
Shell32.Shell shell = new Shell32.Shell();
Shell32.Folder objFolder = shell.NameSpace(folderName);
String user = "";
string[] f = fileName.Split('.'); //fileName.Substring(0, fileName.Length - 4);
foreach (Shell32.FolderItem2 item in objFolder.Items())
{
if (f[0] == item.Name)
{
user = objFolder.GetDetailsOf(item, 10);
break;
}
}
}
catch (Exception ex) { EventLog.WriteEntry("Sharelogger", ex.Message, EventLogEntryType.Error); }
}

greetings, Walter

Developer technologies C#
0 comments No comments
{count} votes

Accepted answer
  1. Jack J Jun 25,296 Reputation points
    2021-10-06T06:13:41.087+00:00

    @Walter Stifter (Privat) , based on my research, I find it will be hard to only set the admin power for the shell32 Section. As usual, we need to set the admin power for the whole application.

    We could add Application Manifest File to the Windows service project and set the following properties:

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />  
    

    You could refer to the following link to know the detailed steps to do it.

    How to Start Windows Service As Administrator Privileges


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


1 additional answer

Sort by: Most helpful
  1. RLWA32 49,536 Reputation points
    2021-10-06T09:31:09.133+00:00

    What account are you using for your Windows Service? If you are using an account that is a member of the Administrators group and have granted that account the right to logon as a service then the SCM will start the service under that account with elevated privileges as an Administrator without the need for a manifest.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.