How to prompt for elevation for a single operation?

  • Thread starter Thread starter Oolis Kraprin
  • Start date Start date
O

Oolis Kraprin

Hi, I have a program running with normal user privileges, that needs
to update an entry in an ini file that happens to be in ProgramData,
so that a server program, running as administrator can read it.
Editing the ini file directly causes only the VirtualStore copy to be
edited, so my plan is to prompt for elevation as administrator before
this edit and restore normal user permission after. If the elevation
fails then a message box reminding the user to edit the file as admin
is sufficient.

I haven't found any information on how to do this though. I am using
c# with .net 2.0.

Thanks
Oolis.
 
Oolis said:
Hi, I have a program running with normal user privileges, that needs
to update an entry in an ini file that happens to be in ProgramData,
so that a server program, running as administrator can read it.
Editing the ini file directly causes only the VirtualStore copy to be
edited, so my plan is to prompt for elevation as administrator before
this edit and restore normal user permission after. If the elevation
fails then a message box reminding the user to edit the file as admin
is sufficient.
Best as I can tell, you can't do this. Elevation happens at the start of a
process and you can't change elevation status once the process is running.

You can delegate the task to a helper application that's marked as requiring
administrative privileges. I don't know of a native way to check whether
your own application is being virtualized in .NET, but you can use P/Invoke
to call OpenProcessToken() and GetTokenInformation() with the
TokenVirtualizationEnabled flag. If your application detects that it's
virtualized, it could call the helper application.

All this may be more trouble than it's worth, if a reminder is already
acceptable.
 
Back
Top