System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator)==true)
{
//Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join(" ", Args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
}
这上次一位大侠给出了在Main()中获得管理员权限的代码,将代码移植到checkbox下就不行了,望指教~
{
WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
//创建Windows用户主题
Application.EnableVisualStyles();
WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator); if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{ //创建启动对象
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
//设置运行文件
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
//设置启动参数
startInfo.Arguments = String.Join(" ", Program.args);
//设置启动动作,确保以管理员身份运行
startInfo.Verb = "runas";
//如果不是管理员,则启动UAC
System.Diagnostics.Process.Start(startInfo);
//Application.EnableVisualStyles();
// Application.SetCompatibleTextRenderingDefault(false);
// Application.Run(new Form1());
}
}
//设置运行文件
startInfo.FileName = "A.exe路径";
以管理员身份运行A.exe就可以了
startInfo.Arguments = String.Join(" ", Program.args);要修改时就传一个参数进来,根剧参数来决定是否要修改注册表.修改完后直接再退出程序.这样就保证了你只有一个程序在运行了.如果不需要修改注册表就正常运行你的程序既可
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args != null)
{
if (args.Length > 0)
{
if (args[0] == "Modify")
{ Modfiy();
Application.Exit();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
}
}
}Modfiy方法进行修改清册表.修改完后就退出这个程序,保证只有一个程序在运行.其它地方保持不变
using System;
using System.Collections.Generic;
using System.Windows.Forms;namespace WindowsApplication1
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
if (args != null)
{
if (args.Length > 0)
{
if (args[0] == "Modify")
{
GetAdmin();
Modfiy();
Application.Exit();
return;
}
}
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
上面那个写的不够严密,看这个
你双击程序,程序因为没有传入参数,所以在检测 "Modify"这个参数,没有检测到就会往下继续执行.也就是运行 Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
这样你的主界面就显示出来了.然后你点主界面的checkbox后再调用GetAdmin方法,只是这个时候要传入"Modify"这个参数,这样程序执行时就会进入
if (args != null)
{
if (args.Length > 0)
{
if (args[0] == "Modify")
{
GetAdmin();
Modfiy();
Application.Exit();
return;
}
}
}
这样就会修改注册表了,扫行完后就return,这样程序就退出了.你原来的程序还是存在的,也是有界面的
不明白你的意思,把你把的流程详细的说下把完整代码也贴上来看看
static void Main(string[]Args)
{
//args = Args;
int processcount = 0;
Process[] pro = Process.GetProcesses();
foreach (Process isPro in pro)
{
if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
{
processcount += 1;
}
}
if (processcount > 1)
{ //MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
} else if (Args != null)
{
if (Args.Length > 0)
{
if (Args[0] == "autunRun")
{
SysOper.GetAdmin(Args);
RegistryInfo.AutunRun(true);
Application.Exit();
return;
}
}
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); /* else
{
Application.EnableVisualStyles();
Application.DoEvents();
Application.Run(new Form1());
}
*/
} }
}这是main函数的代码
{
if (Args.Length > 0)
{
if (Args[0] == "autunRun")
{
SysOper.GetAdmin(Args);
RegistryInfo.AutunRun(true);
Application.Exit();
return;
}
}防止运行多个进程的部分要放在修改注册表的后面.因为你在调试时的进程名字和你双击时的进程名字是不一样的,所以你点checkbox后可以用Process.Start的方法运行一个程序,然后就可以修改注册表了.但是你双击程序运行时,Process.Start运行的进程的名字就是一样的了,这个时候你的程序就会执行到防止程序多开的部分,而修改注册表的部分没有执行到.所以交换两者的顺序就可以了
{
//args = Args;
int processcount = 0;
Process[] pro = Process.GetProcesses();
foreach (Process isPro in pro)
{
if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
{
processcount += 1;
}
} if (Args != null)
{
if (Args.Length > 0)
{
if (Args[0] == "autoRun")
{
SysOper.GetAdmin(Args);
RegistryInfo.autoRun(true);
Application.Exit();
return;
}
}
} if (processcount > 1)
{ //MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
} Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
这样?ms不起作用~
static void Main(string[]Args)
{
//args = Args;
if (Args != null)
{
if (Args.Length > 0)
{
if (Args[0] == "autoRun")
{
SysOper.GetAdmin(Args);
RegistryInfo.autoRun(true);
Application.Exit();
return;
}
}
}int processcount = 0;
Process[] pro = Process.GetProcesses();
foreach (Process isPro in pro)
{
if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
{
processcount += 1;
}
}if (processcount > 1)
{//MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Return;
}Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
晕倒了,说了半天你没看明白先判断要不要修改注册表,要就修改,修改完了就退出程序.
如果不要修改,就判断当前的程序是否多开了,如果多开就退出,如果没有就继续运行出现程序的主界面.你上面的修改和之前的效果是一样的,等于没改.
{
WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
Application.EnableVisualStyles();
WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator);
if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
{
ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
startInfo.FileName = System.Windows.Forms.Application.ExecutablePath;
startInfo.Arguments = String.Join("", args);
startInfo.Verb = "runas";
System.Diagnostics.Process.Start(startInfo);
//退出
// System.Windows.Forms.Application.Exit();
}
}这是GetAmin()方法,这样又问题吗?用你上面调回来后的代码也不行,无论调试运行与否,点击checkbox后都会抛出异常了~~~~
{
//SysOper.GetAdmin(Program.args);
bool Flag = auntunstartup.IsRegeditExist("WinForm");
if (checkBox3.Checked == true)
{
WritePrivateProfileString("IsAutunRun", "isautunrun", "Yes", FileName);
if (Flag == false)
{
RegistryInfo.autoRun(true);
}
}
else
{
auntunstartup.DeleteRegist("WinForm");
WritePrivateProfileString("IsAutunRun", null, null, FileName);
}
}checkbox中的code也顺便贴出看看...
这个是什么意思?private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
//SysOper.GetAdmin(Program.args);
bool Flag = auntunstartup.IsRegeditExist("WinForm");
if (checkBox3.Checked == true)
{WritePrivateProfileString("IsAutunRun", "isautunrun", "Yes", FileName);//这里有调用GetAdmin方法,你如果不调用程序根本就不会以管理员身分重新运行一个新的程序.SysOper.GetAdmin("autoRun");if (Flag == false)
{
RegistryInfo.autoRun(true);
}
}
else
{
auntunstartup.DeleteRegist("WinForm");
WritePrivateProfileString("IsAutunRun", null, null, FileName);
}
main方法中就不需要调用GetAdmin方法了.
static void Main(string[]Args)
{
//args = Args;
if (Args != null)
{
if (Args.Length > 0)
{
if (Args[0] == "autoRun")
{
//这里就不需要了,因为在checkbox里已经用管理员身份在运行了
//SysOper.GetAdmin(Args);
RegistryInfo.autoRun(true);
Application.Exit();
return;
}
}
}int processcount = 0;
Process[] pro = Process.GetProcesses();
foreach (Process isPro in pro)
{
if (isPro.ProcessName == Process.GetCurrentProcess().ProcessName)
{
processcount += 1;
}
}if (processcount > 1)
{//MessageBox.Show("a instance has already run!", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Return;
}Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
呵呵,这句是定义判断是否已经改变注册表的变量,方便下面代码对注册表的修改或者清零,auntunstarup是一个累的对象。
SysOper.GetAdmin("autoRun");
定义的参数是为了传给join的第二个参数啊。它规定了第二个参数必须为字符串数组类型的,我才应是为了接受main的参数吧
SysOper.GetAdmin(str);思路对了,其它的就好说了.需要怎么改你就怎么改就可以了.
{
if (args != null)
{
if (args.Length > 0)
{
if (args[0] == "Modify")
{ Modfiy();
Application.Exit();
}
else
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
我想请问一下子if (args[0] == "Modify")
Modify参数是修改注册表的方法吗?它又是怎么传到GetAdmin()方法中的呢 ?