升级程序为独立的exe程序,由客户端程序调用实现。
客户端调用部分
/* 客户端代码中,调用更新程序部分 */
static bool CheckUpdate()
{
try
{
//string tmpFileName = System.IO.Directory.GetCurrentDirectory() + "\\" + "Update.exe";
//Process p = Process.Start(tmpFileName, (CommonSetting.SchoolCode + "+" + CommonSetting.SchoolPassword + "+" + CommonSetting.Url));
//p.WaitForExit();
//启动客户端升级程序
string tmpFileName = System.IO.Directory.GetCurrentDirectory() + "\\" + "xxx客户端升级程序.exe";
string argu1 = "\"" + CommonSetting.SchoolCode + "\""; //向exe传递的参数1
string argu2 = "\"" + CommonSetting.SchoolPassword + "\""; //向exe传递的参数2
string argu3 = "\"" + CommonSetting.Url + "\""; //向exe传递的参数3
Process p = new Process();
p.StartInfo.FileName = tmpFileName;
p.StartInfo.Arguments = argu1 + " " + argu2 + " " + argu3; //构造传递的入参
//p.StartInfo.Verb = "RunAs"; //使用管理员权限调用(只在xp系统下有效)
p.Start();
p.WaitForExit(); //等待exe程序执行结束
int exCode = p.ExitCode; //获取exe的返回值
if (exCode == -1)
{
return false;
}
return true;
}
catch (Exception)
{
//MessageBox.Show("更新失败!", "警告", MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
更新程序部分
/* 更新程序部分 */
static int Main(string[] args)
{
bool flagAdmin = IsAdministrator(); //获取Windows系统管理员权限(下载文件到C盘)
if (!flagAdmin)
{
MessageBox.Show("xxx升级程序无法获取系统管理员权限!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
return -1;
}
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length != 3) //校验参数个数
{
return -1;
}
//MessageBox.Show("args length: " + args.Length.ToString());
//for(int i=0; i<args.Length; i++)
//{
// MessageBox.Show(args[i]);
//}
string schoolID = args[0]; //获取参数1
string linkCode = args[1]; //获取参数2
string yjServerURL = args[2]; //获取参数3
//启动升级程序主界面
//......
flagAdmin = false;
return 0;
}
判断程序的运行是否有Windows系统管理员权限
/* 判断当前是否具有管理员权限 */
public static bool IsAdministrator()
{
WindowsIdentity current = WindowsIdentity.GetCurrent();
WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current);
bool flag = windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator);
return flag;
}
修改.NET程序的运行权限
在工程中Properties中app.manifest文件中修改下图红色区域部分level字段的值,可修改为下图蓝色框中的值。
其中,asInvoker为普通用户权限;requireAdministrator询问客户是否给予程序管理员权限,大部分软件采用此种方式;highestAvailable赋予程序超级管理员权限。

注意:当软件存在获取管理员权限操作时,装有“安全卫士”等防护软件的电脑上有可能会头弹窗提示不安全,选择“允许访问”即可。

3731

被折叠的 条评论
为什么被折叠?



