以非管理员身份运行时,C# 程序不会启动

发布于 2024-09-16 14:09:27 字数 4740 浏览 2 评论 0 原文

首先,我没有任何 C# 技能或经验。我的一个朋友在大学里参加了几门课程,并且能够向我提供到目前为止我在这个 C# 程序中所学到的知识。

我让我的朋友创建一个程序,该程序将在 WMI 中查找当前登录用户的全名,然后查看 RegisteredOwner 值。如果全名与 RegisteredOwner 相同,则程序退出(全部静默),如果全名与 RegisteredOwner 不同,则程序将启动一个表单一些文本和是/否选项。如果用户单击,则程序将 RegisteredOwner 值设置为登录用户的全名,如果用户单击,则程序将退出。

他完全满足了我的要求;但是,它仅在由具有本地管理员权限的用户运行时运行,不幸的是,在我的环境中,没有用户是其计算机上的本地管理员。当我向他提出这个问题时,他不确定他能做些什么来解决这个问题,并且在研究了一整天之后,恐怕没有什么可以做的来解决这个问题并允许使用本地用户权限启动程序。

所以我要问你的问题是,你是否知道我们可以使用该程序的不同方式来允许它由没有本地管理员权限的用户运行?我希望将可执行文件存储在 PC 本地的某个位置,然后将其存储在启动项列表中,以便在启动项列表中启动它。也许有一种方法可以使用与非本地管理员权限一起使用的可执行文件,然后让它与在 System 帐户下运行的 Windows 服务一起使用?

当由非本地管理员运行时,启动脚本时不会发生任何事情。

下面是代码。

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();
                }
            }
        }
    }
}

任何意见和建议将不胜感激!

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.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();
                }
            }
        }
    }
}

Any advice and suggestions would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

野味少女 2024-09-23 14:09:27

WMI 是这里的杀手。我想 WMI 的整个“管理”部分强制它在管理空间中运行。

我在网上找到了此资源:

我测试了一下,发现它在我的 Win7 X86 机器上运行良好。从网络上的其他来源来看,这对于大多数最新版本的 Windows(包括多个移动版本)来说应该是有好处的。

祝你好运!

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:

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文