开发者

C# program won't launch when running as non-admin

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.

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 quits.

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.

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?

When ran by a non local admin, nothing happens when you launch the script.

Below is the 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.S开发者_JS百科ecurity.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();
                }
            }
        }
    }
}

Any advice and suggestions would be appreciated!


WMI is the killer here. I suppose the whole "Management" part of WMI forces it to run in the admin space.

I found this resource on the Web:

  • http://skysigal.xact-solutions.com/Blog/tabid/427/EntryId/417/C-Compact-Framework-Getting-the-Registered-Owner.aspx

I tested it out to see that it worked respectably well on my Win7 X86 box. Judging from other sources on the web, this should be good for most recent versions of Windows, including several mobile editions.

Good luck!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜