-3

Simple question, but couldn't find anything useful. I need to iterate through files and check whether they have administrator privileges. But remember, the file is not running, so I need to basically check if the software has a 'run as administrator' checkbox set to true or false. Probably this has something to do with attributes I guess. So, how could I achieve that? (I have no idea...)

Edit: I found another way to do this, I did not test it yet. To start the file as a process, immediately (without Thread.Sleep or any operations) suspend the newly created process' main thread. Then a process is easier to check for elevated rights than a simple file because the runtime is the actual 'real' way the file behaves.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Yogibear
  • 153
  • 12
  • 15 seconds of googling: http://stackoverflow.com/questions/2313045/how-to-set-run-this-program-as-an-administrator-programatically – itsme86 Oct 12 '16 at 18:12
  • There are multiple ways that a process can be configured to execute with elevated privileges, including the process itself doing it. If you _only_ want to know the state of the checkbox in the Windows Shell shortcut, then you need to look for topics discussing that specifically. It's not clear from your question what your actual _goal_ is (i.e. why do you care about this setting?), but as asked your question is far too broad. Some relevant posts include http://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator, ... – Peter Duniho Oct 12 '16 at 18:19
  • https://stackoverflow.com/questions/16926232/run-process-as-administrator-from-a-non-admin-application, https://stackoverflow.com/questions/11785413/set-administrative-privileges-in-c-sharp, https://stackoverflow.com/questions/8802261/c-sharp-exe-should-ask-for-run-as-administrator-prompt-when-opened, https://stackoverflow.com/questions/1726048/runasadmin-in-registry-doesnt-seem-to-work-in-windows-7, https://stackoverflow.com/questions/1220213/detect-if-running-as-administrator-with-or-without-elevated-privileges, ... – Peter Duniho Oct 12 '16 at 18:19
  • https://stackoverflow.com/questions/133379/elevating-process-privilege-programatically, https://stackoverflow.com/questions/3224804/what-are-the-differences-between-run-as-administrator-and-a-manifest-with-requ, and https://stackoverflow.com/questions/8670839/can-i-modify-a-win7-environment-to-allow-a-net-program-to-always-run-as-adminis – Peter Duniho Oct 12 '16 at 18:20
  • okay... You guys, are writing so much that differs a lot from my actual question... let me get this straight... It's okay to downvote, but think first. Im writing an antivirus software in c# no realtime protection yet (no c++...). And as a little heuristics, i need to know the state of the properties of the file, regarding to the administrator setting. – Yogibear Oct 12 '16 at 18:23
  • @Yogibear I provided a link that tells you exactly where to find whether or not that checkbox is checked. – itsme86 Oct 12 '16 at 18:25
  • And please... Some people are really helpful, but to the others, dont be a stackoverflow-score hunter... and answer my ACTUAL question. I clearly stated that im talking about a non running file, not my own file, a random third party executable file – Yogibear Oct 12 '16 at 18:25
  • okay, i'm checking it out – Yogibear Oct 12 '16 at 18:26
  • Specifically, I should check so many files, and probably it would be in their file permission attributes, but that was my question, where? – Yogibear Oct 12 '16 at 18:36
  • And by the way, the stuff that I have been provided with (webpages) are totally useless in my case, they are involved with processes and the self-process. – Yogibear Oct 12 '16 at 18:37
  • To be fair, your question is very poorly worded. Files never "have" admin privs. Users can have admin privs. Files can require admin privs. Also, looking at that checkbox doesn't fulfill your stated requirement. There are many ways to tell an executable to launch with admin privs. That checkbox is only one way. – itsme86 Oct 12 '16 at 18:43
  • By the way, english is not my primary language – Yogibear Oct 12 '16 at 18:46
  • Checkbox thing was just to show what i mean, i meant the file itself, not process, i know whats behind the checkboxes, im not a little kid, just to make you better understand what i meant, but i guess it just got y'all confused, sorry about that... – Yogibear Oct 12 '16 at 18:47
  • sry for so many comments btw... but i know what you mean, I just meant that editing registry is unusable (with the regard that it's not a process), and checking the user itself is still not usable, cause the user has nothing to do with the file's actual admin right requirement, and my question has nothing to do with shortcuts or checkboxes or xml files, just file privs – Yogibear Oct 12 '16 at 18:50

1 Answers1

1

You can use the below method. You can define variable acording to your requirement.

string sShortcutPath="";
        string sAppName="";
        string sDesktopPath="";
        string vbScript = GENERICVBFILE.Replace("@@SHORTCUTPATH@@", sShortcutPath);
        vbScript = vbScript.Replace("@@SHORTCUTNAME@@", sAppName);
        vbScript = vbScript.Replace("@@DESKTOPFOLDER@@", sDesktopPath);
        string vbFile = "Script.vbs";
        if (File.Exists(vbFile))
            File.Delete(vbFile);
        File.WriteAllText(vbFile, vbScript);
        Process cProc = new Process();
        cProc.StartInfo.FileName = "cscript.exe";
        cProc.StartInfo.Arguments = vbFile;
        cProc.StartInfo.UseShellExecute = false;
        cProc.StartInfo.CreateNoWindow = true;
        cProc.Start();


    public const string GENERICVBFILE =
        "Option Explicit\n" +
        "Dim ShellApp, FSO, Desktop\n" +
        "Set ShellApp = CreateObject(\"Shell.Application\")\n" +
        "Set FSO = CreateObject(\"Scripting.FileSystemObject\")\n" +

        "Set Desktop =  ShellApp.NameSpace(\"@@DESKTOPFOLDER@@\")\n" +
        "Dim LnkFile\n" +
        "LnkFile = \"@@SHORTCUTPATH@@\"\n" +

        "If(FSO.FileExists(LnkFile)) Then\n" +
        "Dim verb\n" +
        "Dim desktopImtes, item\n" +
        "Set desktopImtes = Desktop.Items()\n" +

        "For Each item in desktopImtes\n" +
        "If (item.Name = \"@@SHORTCUTNAME@@\") Then\n" +
        "For Each verb in item.Verbs\n" +
        "If (verb.Name = \"Run as &administrator\") _\n" +
        "Then\n" +
        "verb.DoIt\n" +
        "End If\n" +
        "Next\n" +
        "End If\n" +
        "Next\n" +
        "End If";
}
Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
  • Okay... im not a vbscript expert actually, but... you gave a code that creates a vbscript, and the script itself check wheter the specified file is administrator or not. Some minor issues... every file I check, wouldn't it be a bit costy if i start a vbscript file everytime... secondly, how will a c# software communicate with that vbscript file. It would require complicated client server communication probably. But anyways thanks for your efforts – Yogibear Oct 12 '16 at 18:35
  • you can search for NSIS script also, its also very useful in such scenarios. – Vivek Nuna Oct 12 '16 at 18:39
  • Could you please post the original code though, It would make my job easier, cause i could use it directly from a dll probably. – Yogibear Oct 12 '16 at 18:45