Click here to Skip to main content
14,735,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
my Application run as administrator, but can't correctly with some code, Because they need to run in normal user, How Can Switch it

What I have tried:

private bool runNormal()
{
    try
    {
        //Do Somthing normal , if run by admin it dosen't work
        return true;
    }
    catch { return false; }
}
private bool runAdmin()
{
    try
    {
        //Do Somthing advance, if run by NormalUser it dosen't work
        return true;
    }
    catch { return false; }
}
public void StartMainProcess()
{
    if (runNormal() && runAdmin())
    {
        //continue to next step
    }
}
Posted
Updated 6-Oct-16 3:44am
Comments
johannesnestler 6-Oct-16 8:02am
   
that sounds a little odd - Administrator has less rights to do the work than a normal user? Can you name any application where you have seen a behavior like that? If I come up which such 'WTF requirements' I always step back and question what I'm doing... just my 2c...
Member 12680324 6-Oct-16 8:38am
   
Yes, With Somthing like this
private bool runNormal()
{
    try
    {
        //Do Somthing normal , if run by admin it dosen't work
        ////Map Network Driver for user

    }
    catch { return false; }
}
private bool runAdmin()
{
    try
    {
        //Do Somthing advance, if run by NormalUser it dosen't work
        //Unlock Folder Form Deny Everyone To acessWrite
        //Ex. "C:// Programs file" or "%UserProfile%\Download"        
        //Unlock User Folder to possible for Write
    }
    catch { return false; }
}
public void StartMainProcess()
{
    if (runNormal() && runAdmin())
    {
        //Afert Unlock UserFile, 
        //Afert Map Network Drive
        //Continue to tranfer all file form mapped NW. Drive
        //To unlocked Folder
        // then Lock folders again (byadmin)
        // and remove network drive (byuser)
        // Finally User can only read and excute folder
    }
}
Dave Kreskowiak 6-Oct-16 8:45am
   
Your question is a a bit backwards. You haven't said anything about why you think you need to do this, just that you need to do it. That's is rarely ever the case. Why do you think you need this solution and what are you really doing in this app?
Member 12680324 6-Oct-16 9:37am
   
Sorry about that, My app about plane to access all user's folder and file, list them and send to NAS every day, To review what happen, programs event change in their machine, Web browser history. and tranfer user work(etc. doc, xlm, mp4 mp3 jpeg and other media) store in to NAS, and get somefile in NAS back to them,

Maybe
1. Map network Drive
2. access and unlock some User Folder
3. transfer file. Send, Recive
4. Finallly, Lock Folder, Disconnect Mapped network drive


Dave Kreskowiak 6-Oct-16 10:08am
   
What do you mean by "unlock some user folder" and for what purpose?

What do you mean by "transfer file, Send, Receive"? For what kind of server and for what purpose?
Member 12680324 6-Oct-16 8:50am
   
similar to if your try in cmd
"icacls /deny everyone:W "
if you run as Nomal user your will got Acess Deny

"net use n: \\nas\publicEveryday /user:employee employee"
if you run as Administrator, you will get wrong result

1 solution

You should check to see if they're in the admin role first.

using System.Security.Principal;

public static bool IsAdmin()
{
    WindowsIdentity  identity  = WindowsIdentity.GetCurrent();
    WindowsPrincipal principal = new WindowsPrincipal(identity);
    return principal.IsInRole(WindowsBuiltInRole.Administrator);
}


IMHO, you should separate the admin code into a different DLL and manually load it if - and only if - the user is in the admin role.

How you marshal the code for the different user roles is an exercise I leave to you.
   
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900