应用程序可能运行在各个windows版:windowsXp,win7-32bit,win7-64bit,win8-32bit,win8-64bit.由于windows各个版本的用户权限的限制不同,同一个开发环境编译出来的应用程序,拿到各个系统中运行的效果是用差别的。其主要原因还是win7、win8加强了对应用程序操作权限的限制。
一、一个明显的例子:
图1.1 Win8 普通用户下的cmd
图1.2 win8管理员下的cmd
图1.3 win7运行CMD界面
上面两个图中,win7 win8都是以Administrator登录的。但是,win7上直接在[运行]->CMD,直接是以管理员权限启动;而Win8直接在[运行]->CMD,进入的是普通权限的CMD界面,必须右键[以管理员身份运行]才能出现图1.2界面。
网上的神人对于这个问题是这样回答的:
图1.4
二、权限限制在程序中使用命令行时中引发的问题
2.1、使用普通用户命令行去开启一些服务
图2.1
2.2、程序中以普通用户形式去开启一些服务:
图2.2
2.1 和2.2的操作都失败了,原因就是应用程序的操作权限级数不够。
这些问题该如何处理?
有些解决方案是在我们调用到CMD的地方,直接使用下面的方式去调用管理员来启动CMD来执行相关命令行的命令:
- ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = "cmd.exe";
- startInfo.Arguments = "/c C:\\Windows\\System32\\cmd.exe";
- startInfo.RedirectStandardInput = true;
- startInfo.RedirectStandardOutput = true;
- startInfo.RedirectStandardError = true;
- startInfo.UseShellExecute =false ;
- startInfo.Verb = "RunAs";
- Process process = new Process();
- process.StartInfo = startInfo;
- process.Start();
- process.StandardInput.WriteLine("bcdedit");
- process.StandardInput.WriteLine("exit");
- string strRst = process.StandardOutput.ReadToEnd();
- Bootinitext.AppendText("\n"+strRst );
- process.WaitForExit();
实践证明,这种方法的在模块所在的应用程序不是以管理员权限启动时它是没什么作用的,它仍然是出现图2.2的结果。故解决方法应该是直接将应用程序的启动权限提升到管理员级别,主要参考下面代码:
在program.cs如下操作(案例)
- static void Main(string[] Args)
- {
- /**
- * 当前用户是管理员的时候,直接启动应用程序
- * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
- */
- //获得当前登录的Windows用户标示
- 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))
- {
- //如果是管理员,则直接运行
- Application.EnableVisualStyles();
- 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);
- //退出
- System.Windows.Forms.Application.Exit();
- }
- }
2.3、使程序以管理员权限启动的实际案例代码如下:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Windows.Forms;
- using System.Runtime.InteropServices;
- using System.IO;
- using System.Text;
- using System.Diagnostics;
- using System.Threading;
- namespace DatabaseCreate
- {
- static class Program
- {
- #region Setting from ini document
- [DllImport("kernel32")]
- private static extern long WritePrivateProfileString(string Section, string key, stringval, string filePath);
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string Section, string key, string def, /*StringBuilder reVal原来用StringBuilder*/byte[] buffer/*现在使用byte串*/, int Usize, string filePath);
- [DllImport("kernel32")]
- private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
- #endregion
- ///
- /// The main entry point for the application.
- ///
- [STAThread]
- static void Main(string[] Args)
- {
- Application.EnableVisualStyles();
- Application.SetCompatibleTextRenderingDefault(false);
- //获取当前登录的Windows用户的标识
- System.Security.Principal.WindowsIdentity wid = System.Security.Principal.WindowsIdentity.GetCurrent();
- System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(wid);
- // 判断当前用户是否是管理员
- if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
- {
- byte[] bufferContent = new byte[255];
- string LanguageInfoPath = Application.StartupPath + "\\DayaSystemInfo.ini";
- FileInfo fileInfo = new FileInfo(LanguageInfoPath);
- string section = "Setting";
- string key = "Language";
- int returnNum = 0;
- /* 读取显示语言及处理 */
- if (fileInfo.Exists)
- returnNum = GetPrivateProfileString(section, key, "", bufferContent, (int)bufferContent.Length, fileInfo.ToString().Trim());
- string lanResult = "";
- int languageIndex = 0;
- if (returnNum != 0)
- {
- lanResult = Encoding.Default.GetString(bufferContent, 0, 1);
- if ((lanResult.Trim().Equals("1")) || (lanResult.Trim().Equals("0")))
- {
- languageIndex = Convert.ToInt32(lanResult);
- }
- }
- Application.Run(new Form1(languageIndex));
- }
- else // 用管理员用户运行
- {
- System.Diagnostics.ProcessStartInfo startInfo = new ProcessStartInfo();
- startInfo.FileName = Application.ExecutablePath;
- startInfo.Arguments = string.Join(" ", Args);
- startInfo.Verb = "runas";
- System.Diagnostics.Process.Start(startInfo);
- System.Windows.Forms.Application.Exit();
- }
- }
- }
- }
参考文档:
C#2010在Windows8—64系统,写的程序可以访问电脑(其他软件)的服务状态,但不能开启或关闭该服务
http://wenwen.soso.com/z/q419403029.htm
C#默认以管理员身份运行程序