C# program won't launch when running as non-admin
<p>First off, I don't have any C# skills or experience. A friend of mine took a couple classes in college and was able to give me what I've got so far in this C# program. </p>

<p>I asked my friend to create a program that would look at WMI for the current logged on user's full name, then look at the RegisteredOwner value. If the full name is the same as the registeredowner then the program quits (all silent), if the full name is different than the registeredowner then the program would launch a form with some text and a yes/no option. If the user clicks yes then the program sets the registeredowner value to the logged on users full name, and if they click no the program quit.</p>

<p>He delivered exactly what I asked for however it only runs if ran by a user with local admin rights and unfortunately in my environment no user is a local admin on their machine. When I presented the issue to him he wasn't sure what he could do to resolve the problem, and after looking into this all day I'm afraid there isn't much that can be done to resolve the issue and allow the program to be launched using the local users permissions.</p>

<p>So my question for you is do you know of a different way we could go with this program that would allow it to be run by a user without local admin rights? I would like to have the executable stored somewhere locally on the PC and then it in the startup items list of having something in the startup items list launch it. Maybe there's a way I can use a executable that works with non-local admin rights and then have it work with a windows service that's running under the System account?</p>

<p>When ran by a non local admin nothing happens when you launch the script. </p>

<p>Below is the code.</p>

<pre><code>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Management;
using System.Security.Principal;
using Microsoft.Win32;
using System.Threading;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        bool compare;

    public Form1()
    {
        InitializeComponent();

        if (PreLoad())
            compare = true;
        else
        {
            this.Text = GetUser();
            compare = false;
        }
    }

    private bool PreLoad()
    {
        string temp = GetCaption(GetUser());

        RegistryKey regKey1 = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
        string keyString = regKey1.GetValue("RegisteredOwner").ToString();

        if (temp == keyString)
            return true;
        else
            return false;

    }
    private void btnYes_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Are you sure?", "Confirmation",MessageBoxButtons.OKCancel);

        string temp = GetCaption(GetUser());
        DoRegistryEdit(temp);
        lblShowAll.Text = "-Successfully registered the machine to: " + temp + " -";

        //Refreshes the screen so that the status message displays
        this.Refresh();

        Thread.Sleep(5000);

        this.Close();
    }

    private void btnNo_Click(object sender, EventArgs e)
    {
        //MessageBox.Show("Better change computers then!");

        this.Close();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        if (compare)
            this.Close();
    }

    public string GetCaption(string userName)
    {
        String QueryStringTemp = "Select * from Win32_NetworkLoginProfile where Caption = '" + userName +"'";
        System.Management.ObjectQuery oQuery = new ObjectQuery(QueryStringTemp);
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);

        ManagementObjectCollection oReturnCollection = oSearcher.Get();

        string capturedResults = "";

        foreach (ManagementObject oReturn in oReturnCollection)
        {
            capturedResults += oReturn["FullName"].ToString();
        }
        return capturedResults;
    }
    public string GetUser()
    {
        System.Management.ObjectQuery oQuery = new ObjectQuery("Select * from Win32_ComputerSystem");
        ManagementObjectSearcher oSearcher = new ManagementObjectSearcher(oQuery);

        ManagementObjectCollection oReturnCollection = oSearcher.Get();

        string capturedResults = "";

        foreach (ManagementObject oReturn in oReturnCollection)
        {
            capturedResults += oReturn["UserName"].ToString(); 
        }

        int hold = capturedResults.IndexOf("\\");
        capturedResults = capturedResults.Substring(hold + 1);
        return capturedResults;
    }


    public void DoRegistryEdit(string name)
    {
        RegistryKey masterKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion");
        if (masterKey == null)
            MessageBox.Show("Null Master Key!");
        else
        {
            try
            {
                masterKey.SetValue("RegisteredOwner", name);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Uh OH!" + ex);
            }
            finally
            {
                masterKey.Close();
            }
        }
    }
}
</code></pre>

<p>}</p>

<p>Any advice and suggestions would be appreciated! </p>
