C# 应用程序单例(禁止多开) 获取.net版本号 以及 管理员权限

Mutex不仅提供跨线程的服务,还提供跨进程的服务。当在构造函数中为Mutex指定名
称时,则会创建一个命名了的Mutex。其他线程创建Mutex时,如果指定的名称相同,则返
回同一个互斥体,不论该线程位于哪个进程或者应用程序域中。

使用命名互斥体的一个例子是创建Singleton应用程序,即只能打开一个实例的应用程
序。显然,我们创建的控制台应用程序是可以同时打开多个实例的。下面的代码使用Mutex
实现了只能同时开启一个Console控制台程序。



string asm = Assembly.GetExecutingAssembly().GetName().Name; Mutex mtx = new Mutex(false, asm); if (mtx.WaitOne(TimeSpan.FromMilliseconds(100)) ? false : true;) { MessageBox.Show("采集程序已经运行在此电脑上", "登录重复提示", MessageBoxButtons.OK, MessageBoxIcon.Warning); //逻辑操作处理// Thread.CurrentThread.Abort(); }

 获取farmwork版本号

  string farmWorkVersion = "4.5.2";
private static bool GetDotNetVersion(string version) { string oldname = "0"; using (RegistryKey ndpKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, ""). OpenSubKey(@"SOFTWARE\Microsoft\NET Framework Setup\NDP\")) { foreach (string versionKeyName in ndpKey.GetSubKeyNames()) { if (versionKeyName.StartsWith("v")) { RegistryKey versionKey = ndpKey.OpenSubKey(versionKeyName); string newname = (string)versionKey.GetValue("Version", ""); if (string.Compare(newname, oldname) > 0) { oldname = newname; } if (newname != "") { continue; } foreach (string subKeyName in versionKey.GetSubKeyNames()) { RegistryKey subKey = versionKey.OpenSubKey(subKeyName); newname = (string)subKey.GetValue("Version", ""); if (string.Compare(newname, oldname) > 0) { oldname = newname; } } } } } return string.Compare(oldname, version) > 0 ? true : false; }

获取管理员权限

  private static void ProcessStart()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
        
            /**
            * 当前用户是管理员的时候,直接启动应用程序
            * 如果不是管理员,则使用启动对象启动程序,以确保使用管理员身份运行
            */
            //获得当前登录的Windows用户标示
            System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
            System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
            //判断当前登录用户是否为管理员
            if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator))
            {
                //如果是管理员,则直接运行
                Application.Run(new FrmLogin());
            }
            else
            {
                //创建启动对象
                System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
                startInfo.UseShellExecute = true;
                startInfo.WorkingDirectory = Environment.CurrentDirectory;
                startInfo.FileName = Application.ExecutablePath;
                //设置启动动作,确保以管理员身份运行
                startInfo.Verb = "runas";
                try
                {
                    System.Diagnostics.Process.Start(startInfo);
                }
                catch
                {
                    return;
                }
                //退出
                Application.Exit();
            }
        }

猜你喜欢

转载自www.cnblogs.com/wangcl-8645/p/10614282.html
今日推荐