码迷,mamicode.com
首页 > Windows程序 > 详细

C#操作Windows

时间:2015-03-05 10:35:07      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

1.读写注册表

技术分享
 1 class Program
 2 {
 3 
 4         static void Main(string[] args)
 5         {
 6             WriteRegisterKey("UserName","Paul Huang");
 7 
 8             object value = ReadRegisterKey("UserName");
 9 
10             if(value != null)
11             {
12                 Console.WriteLine(value.ToString());
13                 //成功打印字符
14             }
15 
16             Console.ReadKey();
17         }
18 
19         public static void WriteRegisterKey(string key,string value)
20         {
21             try
22             {
23                 //OpenSubKey第二个参数writable需要设置为true,否则当写值时会抛出没有权限的异常
24                 RegistryKey licenseRegistryKey = Registry.LocalMachine.OpenSubKey("SoftWare\\IPP PCL");
25 
26                 if (licenseRegistryKey == null)
27                 {
28                    licenseRegistryKey =  Registry.LocalMachine.CreateSubKey("SoftWare\\IPP PCL");
29                 }
30 
31                 licenseRegistryKey.SetValue(key, value);
32 
33                 licenseRegistryKey.Close();
34             }
35             catch (Exception ex)
36             {
37                 throw ex;
38             }
39         }
40 
41         public static object ReadRegisterKey(string userName)
42         {
43             object retValue = null;
44 
45             try
46             {
47                 RegistryKey licenseRegistryKey = Registry.LocalMachine.OpenSubKey("SoftWare\\IPP PCL");
48 
49                 if (licenseRegistryKey != null)
50                 {
51                     retValue = licenseRegistryKey.GetValue("UserName");
52 
53                     licenseRegistryKey.Close();
54                 }
55 
56                 return retValue;
57             }
58             catch (Exception ex)
59             {
60                 throw ex;
61             }
62         }
63     }
View Code

2.判断登录用户权限

技术分享
 1  class Program
 2   {
 3 
 4         static void Main(string[] args)
 5         {
 6             if (IsAdministrator())
 7             {
 8                 Console.WriteLine("Is Administrator");
 9             }
10             else
11             {
12                 Console.WriteLine("Normal User");
13             }
14 
15             Console.ReadKey();
16         }
17 
18         public static bool IsAdministrator()
19         {
20             WindowsIdentity ident = WindowsIdentity.GetCurrent();
21             WindowsPrincipal principal = new WindowsPrincipal(ident);
22             return principal.IsInRole(WindowsBuiltInRole.Administrator);
23         }
24     }
View Code

 

C#操作Windows

标签:

原文地址:http://www.cnblogs.com/JustYong/p/4314998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!