so there isn't a way to run, say, a single method, as a specified user?

where i'm coming from is...
i want to create a directoryInfo list for a unc directory. i'm running the app as a non-admin user (eventually will be a service) but it's not finding the directory due to permissions.
i want to be able to 'elevate' to admin privileges for a one-of method within the app.

like how you can specify sa and sa password for databases access.

pw_jamison: Please create your own thread to ask questions, and do not append them to someone elses thread. This thread has been sitting here since 2006!

To answer your question you can impersonate a user:

using System.Runtime.InteropServices; // DllImport
using System.Security.Principal; // WindowsImpersonationContext
using System.Security.Permissions; // PermissionSetAttribute

public WindowsImpersonationContext 
    ImpersonateUser(string sUsername, string sDomain, string sPassword)
    // initialize tokens
    IntPtr pExistingTokenHandle = new IntPtr(0);
    IntPtr pDuplicateTokenHandle = new IntPtr(0);
    pExistingTokenHandle = IntPtr.Zero;
    pDuplicateTokenHandle = IntPtr.Zero;
    // if domain name was blank, assume local machine
    if (sDomain == "")
        sDomain = System.Environment.MachineName;

        string sResult = null;

        const int LOGON32_PROVIDER_DEFAULT = 0;

        // create token
        const int LOGON32_LOGON_INTERACTIVE = 2;
        //const int SecurityImpersonation = 2;

        // get handle to token
        bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, 
                ref pExistingTokenHandle);

        // did impersonation fail?
        if (false == bImpersonated)
            int nErrorCode = Marshal.GetLastWin32Error();
            sResult = "LogonUser() failed with error code: " + 
                nErrorCode + "\r\n";

            // show the reason why LogonUser failed
            MessageBox.Show(this, sResult, "Error", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);

        // Get identity before impersonation
        sResult += "Before impersonation: " + 
            WindowsIdentity.GetCurrent().Name + "\r\n";

        bool bRetVal = DuplicateToken(pExistingTokenHandle, 
                ref pDuplicateTokenHandle);

        // did DuplicateToken fail?
        if (false == bRetVal)
            int nErrorCode = Marshal.GetLastWin32Error();
            // close existing handle
            sResult += "DuplicateToken() failed with error code: " 
                + nErrorCode + "\r\n";

            // show the reason why DuplicateToken failed
            MessageBox.Show(this, sResult, "Error", 
                MessageBoxButtons.OK, MessageBoxIcon.Error);
            return null;
            // create new identity using new primary token
            WindowsIdentity newId = new WindowsIdentity
            WindowsImpersonationContext impersonatedUser = 

            // check the identity after impersonation
            sResult += "After impersonation: " + 
                WindowsIdentity.GetCurrent().Name + "\r\n";
            MessageBox.Show(this, sResult, "Success", 
                MessageBoxButtons.OK, MessageBoxIcon.Information);
            return impersonatedUser;
    catch (Exception ex)
        throw ex;
        // close handle(s)
        if (pExistingTokenHandle != IntPtr.Zero)
        if (pDuplicateTokenHandle != IntPtr.Zero) 

Taken from:

See thread:

This question has already been answered. Start a new discussion instead.