programming4us
programming4us
DESKTOP

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

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
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
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us