2. Executing as a Separate Process
Setting an entire application to execute in
administrator mode is uncommon and unsafe. The more common scenario is
to separate the application into pieces that can execute at one of
three levels:
The .NET Framework currently executes each level as
a separate process, rather than using threads or some other convenient
method. The following sections show one scenario that uses separate
processes to execute in standard mode and in administrator mode.
2.1. Defining the Separate Process Application
The example begins with a Windows Forms template application named Modify Permission that has a single button, btnChange.
You can give the solution and project the same name, if desired. You
don't need to add any special references for the example, but you do
need to add the following using statement:
using System.Diagnostics;
2.2. Creating the Secondary Project
You add the second process as a separate project. Use the following steps to create the secondary project:
Right-click the solution entry in Solution Explorer and choose Add => New Project from the Context menu. You'll see the Add New Project dialog box shown in Figure 5.
Highlight the Console Application template.
Type SetPermission
(no spaces) in the Name field and click OK. Visual Studio adds the
SetPermission project to the Modify Permission solution. You also see
the SetPermission.CS file opened for editing.
2.3. Configuring the Secondary Project
As a console application, SetPermission doesn't
include any controls or other special interface needs. You also won't
need to add any references to it. However, you do need to add the
following using statements:
using System.Security.AccessControl;
using System.Security.Principal;
using System.IO;
The project needs a special setup as well. You want the output of the build process to appear with the Modify Permission.EXE file to make it easier for Modify Permission.EXE to find it. Use the following steps to configure the build output:
Right-click
the SetPermission project entry in Solution Explorer and choose
Properties from the Context menu. You see the SetPermission properties
window.
Select Debug in the Configuration drop-down list box.
Type ..\Modify Permission\bin\Debug\ in the Output Path field. Your Build tab should look like the one shown in Figure 6.
Select Release in the Configuration drop-down list box.
Type ..\Modify Permission\bin\Release\ in the Output Path field.
You also want to be sure that the correct project
executes when you debug the application. Right-click the Modify
Permission project in Solution Explorer and choose Set as StartUp
Project from the context menu. The Modify Permission entry will appear
in bold type to indicate that it's the startup project.