DESKTOP

Windows 7 : ADDING UAC SUPPORT TO YOUR APPLICATION (part 4)

1/27/2014 1:46:13 AM
2.4. Writing the Primary Application Code

It's time to add some code to the application. Begin with the primary application, Modify Permission. This application can perform any number of tasks that rely on the user's standard credentials. In fact, when working with a production application, it's likely to be the largest part of the application because there really are very few tasks that require administrator privileges. Listing 2 shows the code you need for the example application.

Example 2. Calling an external application
private void btnChange_Click(object sender, EventArgs e)
{
// Obtain the application path.
String ThePath = "\"" + Application.StartupPath + "\"";

// Create a new process for changing the permissions.
ProcessStartInfo PSI = new ProcessStartInfo(
"SetPermission", ThePath);

// Configure the process to run in an elevated state.
PSI.Verb = "runas";

// Run the process.
Process.Start(PSI).WaitForExit();

// Display a success message.
MessageBox.Show("Change Succeeded!");
}

The code begins by creating a path to the secondary application. It then creates a ProcessStartInfo object, PSI, which accepts the secondary application name and path as arguments. The next step is to set the PSI.Verb property to "runas". This step is very important because it tells the system to elevate the process rights to administrator mode.

At this point, the code calls Process.Start() with PSI as the argument. You want to be sure that you add WaitForExit() so that the secondary application exits before the primary application continues processing. The final step is to display a success message.

2.5. Writing the Secondary Application Code

Unlike your production application, the secondary application does most of the work in the example. In this case, the secondary application receives the application path as an input and uses it to modify the rights for Temp.TXT, as shown in Listing 3.

Example 3. Modifying file or directory permissions externally
static void Main(string[] args)
{
// Create a file security object for the target file.
FileSecurity FS = File.GetAccessControl(
args[0] + @"\Temp.TXT");

// Create a new rule.
FileSystemAccessRule Rule = new FileSystemAccessRule(
new NTAccount(@"BUILTIN\Users"),
FileSystemRights.Write,
AccessControlType.Allow);

// Add the rule to the file security object.
FS.AddAccessRule(Rule);

// Save the rule to the file.
File.SetAccessControl(
args[0] + @"\Temp.TXT", FS);
}

The code begins by creating a FileSecurity object, FS, with the application path (passed as args[0]) and the filename, @"\Temp.TXT", as an argument. The code then builds a new FileSystemAccessRule object, Rule, that contains the account name, rights, and type of access (allow or deny). The kind of identity, in this case, is an NTAccount, @"BUILTIN\Users". The code is requesting the FileSystemRights.Write right and will allow (AccessControlType.Allow) the action.

Figure 7. The result of this two-part application is to set the BUILTIN\Users account rights.

Now, the code adds the new rule to FS using the AddAccessRule(). It then uses the File.SetAccessControl() method to actually change the rights on the file.

Make absolutely certain that you perform this task on a closed file to ensure that your application doesn't experience an error. Performing the task on an open file can cause conflicts, especially if the file is opened for write access, which locks it. To see the results of this example, right-click the file and choose Properties from the Context menu. Select the Security tab to see the results shown in Figure 7.

Other  
  •  Parallels Desktop 9 For Mac - The Best Of Both Worlds
  •  Windows Server 2008 and Windows Vista : Benefits of Group Policy Preferences (part 2) - Working with Any Organizational Unit Design
  •  Windows Server 2008 and Windows Vista : Benefits of Group Policy Preferences (part 1) - User-Friendly Interface
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 4) - Using ADMX File Language
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 3) - Core ADMX File Concepts
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 2) - Core ADMX File Concepts
  •  Windows Server 2008 and Windows Vista : Creating Custom ADMX and ADML Files (part 1) - ADMX Schema , ADMX File Structure , ADML File Structure
  •  Windows 7 : Custom Libraries and Saved Searches (part 2) - Using Saved Searches
  •  Windows 7 : Custom Libraries and Saved Searches (part 1) - Creating Custom Libraries
  •  Windows 7 : Visualization and Organization - How to Make the Windows Shell Work for You - The Organizational Advantage of Libraries
  •  
    Top 10
    Extending LINQ to Objects : Writing a Single Element Operator (part 2) - Building the RandomElement Operator
    Extending LINQ to Objects : Writing a Single Element Operator (part 1) - Building Our Own Last Operator
    3 Tips for Maintaining Your Cell Phone Battery (part 2) - Discharge Smart, Use Smart
    3 Tips for Maintaining Your Cell Phone Battery (part 1) - Charge Smart
    OPEL MERIVA : Making a grand entrance
    FORD MONDEO 2.0 ECOBOOST : Modern Mondeo
    BMW 650i COUPE : Sexy retooling of BMW's 6-series
    BMW 120d; M135i - Finely tuned
    PHP Tutorials : Storing Images in MySQL with PHP (part 2) - Creating the HTML, Inserting the Image into MySQL
    PHP Tutorials : Storing Images in MySQL with PHP (part 1) - Why store binary files in MySQL using PHP?
    REVIEW
    - First look: Apple Watch

    - 3 Tips for Maintaining Your Cell Phone Battery (part 1)

    - 3 Tips for Maintaining Your Cell Phone Battery (part 2)
    VIDEO TUTORIAL
    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 1)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 2)

    - How to create your first Swimlane Diagram or Cross-Functional Flowchart Diagram by using Microsoft Visio 2010 (Part 3)
    Popular Tags
    Microsoft Access Microsoft Excel Microsoft OneNote Microsoft PowerPoint Microsoft Project Microsoft Visio Microsoft Word Active Directory Biztalk Exchange Server Microsoft LynC Server Microsoft Dynamic Sharepoint Sql Server Windows Server 2008 Windows Server 2012 Windows 7 Windows 8 Adobe Indesign Adobe Flash Professional Dreamweaver Adobe Illustrator Adobe After Effects Adobe Photoshop Adobe Fireworks Adobe Flash Catalyst Corel Painter X CorelDRAW X5 CorelDraw 10 QuarkXPress 8 windows Phone 7 windows Phone 8 BlackBerry Android Ipad Iphone iOS