How to prompt for elevation for a single operation?

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.
 
J

Jeroen Mostert

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top