首先,我没有任何C#技能或经验。我的一个朋友在大学上了几堂课,并且能够给我这个C#程序中迄今为止所获得的。以非管理员身份运行C#程序时不会启动
我让我的朋友创建一个程序,查看当前登录用户全名的WMI,然后查看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();
}
}
}
}
}
任何意见和建议,将不胜感激!
一个好的开始就是告诉我们什么时候出现错误,当非管理员用户运行 – pm100 2010-09-01 23:19:43
还提供*代码*的当前实现。 – TheCloudlessSky 2010-09-01 23:25:51