1 using System.Runtime.InteropServices;
  2 using Microsoft.Win32;
  3 using System.Drawing;
  4 using System.Net;
  5 using System.IO;
  6 using System.Drawing.Imaging;
  7 
  8 namespace Cnic
  9 {
 10     public class RegeditHelper
 11     {
 12 
 13         public static string GetDefaultBrowserPath()
 14         {
 15             RegistryKey key = Registry.ClassesRoot.OpenSubKey(@"HTTP\shell\open\command", false);
 16             string value = key.GetValue("").ToString();
 17             if (value != null && value.Length > 1 && value[0] == '"')
 18             {
 19                 return value.Substring(1, value.IndexOf("\"", 1));
 20             }
 21             return null;
 22         }
 23         public static bool SetAutoRun(string vKeyName, string vExeFullPath)
 24         {
 25             try
 26             {
 27                 RegistryKey hkml = Registry.LocalMachine;
 28                 RegistryKey key = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
 29                 if (key != null)
 30                 {
 31                     key.SetValue(vKeyName, vExeFullPath);
 32                     key.Close();
 33                     return true;
 34                 }
 35             }
 36             catch { }
 37             return false;
 38         }
 39         public static bool IsAutoRun(string vKeyName, string vValue)
 40         {
 41             try
 42             {
 43                 RegistryKey hkml = Registry.LocalMachine;
 44                 RegistryKey key = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", false);
 45                 if (key != null)
 46                 {
 47                     object value=key.GetValue(vKeyName);
 48                     return (value != null && value.ToString().ToString() == vValue);
 49                 }
 50             }
 51             catch { }
 52             return false;
 53         }
 54         public static bool DelAutoRun(string vKeyName)
 55         {
 56             try
 57             {
 58                 RegistryKey hkml = Registry.LocalMachine;
 59                 RegistryKey key = hkml.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true);
 60                 if (key != null)
 61                 {
 62                     key.DeleteValue(vKeyName, false);
 63                     return true;
 64                 }
 65             }
 66             catch { }
 67             return false;
 68         }
 69     }
 70     public class ImageHelper
 71     {
 72         public static void CopyFromScreen(Image vBuffer)
 73         {
 74             WriteScreen(vBuffer, 0, 0, vBuffer.Width, vBuffer.Height);
 75         }
 76         public static void WriteScreen(Image vBuffer, int ScrX, int ScrY)
 77         {
 78             WriteScreen(vBuffer, ScrX, ScrY, vBuffer.Width, vBuffer.Height);
 79         }
 80         public static void WriteScreen(Image vBuffer, int ScrX, int ScrY, int ScrWidth, int ScrHeight)
 81         {
 82             IntPtr DwHdcPtr = SafeNativeMethods.GetDesktopWindow();
 83             IntPtr DcHdcPtr = SafeNativeMethods.GetDC(DwHdcPtr);
 84             using (Graphics g = Graphics.FromImage(vBuffer))
 85             {
 86                 //SafeNativeMethods.BitBlt(g.GetHdc(), 0, 0, ScrWidth, ScrHeight, DcHdcPtr, ScrX, ScrY, 0x00CC0020);
 87                 SafeNativeMethods.StretchBlt(g.GetHdc(), 0, 0, ScrWidth, ScrHeight, DcHdcPtr, ScrX, ScrY, ScrWidth, ScrHeight, 0x40CC0020);
 88                 g.ReleaseHdc();
 89             }
 90             SafeNativeMethods.ReleaseDC(DwHdcPtr, DcHdcPtr);
 91         }
 92 
 93         public static void Save(Image vImage, string vPath)
 94         {
 95             string ext = new FileInfo(vPath).Extension;
 96             ImageFormat format = ImageFormat.Png;
 97             switch (ext)
 98             {
 99                 case ".BMP":
100                     format = ImageFormat.Bmp;
101                     break;
102                 case ".EMF":
103                     format = ImageFormat.Emf;
104                     break;
105                 case ".EXIF":
106                     format = ImageFormat.Exif;
107                     break;
108                 case ".GIF":
109                     format = ImageFormat.Gif;
110                     break;
111                 case ".ICO":
112                     format = ImageFormat.Icon;
113                     break;
114                 case ".JPG":
115                 case ".JPEG":
116                     format = ImageFormat.Jpeg;
117                     break;
118                 case "TIFF":
119                     format = ImageFormat.Tiff;
120                     break;
121                 case "WMF":
122                     format = ImageFormat.Wmf;
123                     break;
124             }
125             vImage.Save(vPath, format);
126         }
127 
128     }
129     public class ConvertHelper
130     {
131         public static int BytesToInt(byte[] value)
132         {
133             return (((value[0] | (value[1] << 8)) | (value[2] << 0x10)) | (value[3] << 0x18));
134         }
135         public static byte[] IntToBytes(int value)
136         {
137             byte[] rtun = new byte[4];
138             rtun[0] = (byte)value;
139             rtun[1] = (byte)(value >> 8);
140             rtun[2] = (byte)(value >> 0x10);
141             rtun[3] = (byte)(value >> 0x18);
142             return rtun;
143         }
144         public static uint BytesToUInt(byte[] value)
145         {
146             return (uint)(((value[0] | (value[1] << 8)) | (value[2] << 0x10)) | (value[3] << 0x18));
147         }
148         public static byte[] UIntToBytes(uint value)
149         {
150             byte[] rtun = new byte[4];
151             rtun[10] = (byte)value;
152             rtun[11] = (byte)(value >> 8);
153             rtun[12] = (byte)(value >> 0x10);
154             rtun[13] = (byte)(value >> 0x18);
155             return rtun;
156         }
157         public static long BytesToLong(byte[] value)
158         {
159             long rtun = (long)value[7];
160             rtun = rtun << 0x8 | value[6];
161             rtun = rtun << 0x8 | value[5];
162             rtun = rtun << 0x8 | value[4];
163             rtun = rtun << 0x8 | value[3];
164             rtun = rtun << 0x8 | value[2];
165             rtun = rtun << 0x8 | value[1];
166             rtun = rtun << 0x8 | value[0];
167             return rtun;
168         }
169         public byte[] LongToBytes(long value)
170         {
171             byte[] _buffer = new byte[8];
172             _buffer[0] = (byte)value;
173             _buffer[1] = (byte)(value >> 8);
174             _buffer[2] = (byte)(value >> 0x10);
175             _buffer[3] = (byte)(value >> 0x18);
176             _buffer[4] = (byte)(value >> 0x20);
177             _buffer[5] = (byte)(value >> 40);
178             _buffer[6] = (byte)(value >> 0x30);
179             _buffer[7] = (byte)(value >> 0x38);
180             return _buffer;
181         }
182     }
183     public class RegexHelper
184     {
185 
186     }
187     public class SecurityHelper
188     {
189 
190     }
191     public class HttpHelper
192     {
193         /// <summary>
194         ///  下载大文件
195         /// </summary>
196         /// <param name="vUrl"></param>
197         /// <param name="vFilePath"></param>
198         /// <param name="vCover"></param>
199         public static void DownLoad(string vUrl, string vFilePath, bool vCover)
200         {
201             FileStream fs = null;
202             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(vUrl);
203             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
204             Stream stream = response.GetResponseStream();
205             FileInfo f = new FileInfo(vFilePath);
206             if (f.Exists)
207             {
208                 if (vCover == false) { return; }
209                 else
210                 {
211                     fs = new FileStream(vFilePath, FileMode.Create, FileAccess.Write);
212                 }
213             }
214             else
215             {
216                 if (f.Directory.Exists == false)
217                 {
218                     f.Directory.Create();
219                 }
220                 fs = f.Create();
221             }
222             int bufferL = 1024;
223             byte[] buffer = new byte[bufferL];
224             int readL = stream.Read(buffer, 0, bufferL);
225             while (readL > 0)
226             {
227                 fs.Write(buffer, 0, readL);
228                 readL = stream.Read(buffer, 0, bufferL);
229             }
230             stream.Close();
231             response.Close();
232             fs.Close();
233         }
234         /// <summary>
235         /// 下载单个小文件
236         /// </summary>
237         /// <param name="vUrl">url路径</param>
238         /// <param name="vFilePath">文件路径</param>
239         /// <param name="vCover">是否覆盖</param>
240         /// <returns></returns>
241         public static byte[] DownLoad(string vUrl, string vFilePath)
242         {
243             byte[] buffer = Cnic.HttpHelper.LoadToByte(vUrl);
244             FileStream fs = null;
245             FileInfo f = new FileInfo(vFilePath);
246             if (f.Directory.Exists == false)
247             {
248                 f.Directory.Create();
249             }
250             if (f.Exists == false)
251             {
252                 fs = f.Create();
253             }
254             else
255             {
256                 fs = new FileStream(vFilePath, FileMode.Open, FileAccess.Write);
257             }
258             fs.Write(buffer, 0, buffer.Length);
259             fs.Flush();
260             fs.Close();
261             return buffer;
262         }
263 
264         /// <summary>
265         /// 下载多个Url到一个文件(小文件)
266         /// </summary>
267         /// <param name="urls">需要下载的url列表</param>
268         /// <param name="vFilePath">文件路径</param>
269         /// <returns>返回出错的url索引</returns>
270         public static int DownLoad(List<string> urls, string vFilePath)
271         {
272             FileStream fs = new FileStream(vFilePath, FileMode.Create, FileAccess.Write);
273             int i = 0;
274             for (i = 0; i < urls.Count; i++)
275             {
276                 try
277                 {
278                     byte[] buffer = LoadToByte(urls[i]);
279                     fs.Write(buffer, 0, buffer.Length);
280                     fs.Flush();
281                 }
282                 catch
283                 {
284                     break;
285                 }
286             }
287             fs.Close();
288             return i;
289         }
290 
291         /// <summary>
292         /// 下载多个Url(小文件)
293         /// </summary>
294         /// <param name="urls">需要下载的url列表</param>
295         /// <param name="vFilePaths">文件路径</param>
296         /// <returns>返回出错的url索引,或者返回-1</returns>
297         public static int DownLoad(List<string> urls, List<string> vFilePaths)
298         {
299             if (urls.Count != vFilePaths.Count) { return -1; }
300             int rtun = -1;
301             for (int i = 0; i < urls.Count; i++)
302             {
303                 try
304                 {
305                     byte[] buffer = LoadToByte(urls[i]);
306                     FileStream fs = new FileStream(vFilePaths[i], FileMode.Create, FileAccess.Write);
307                     fs.Write(buffer, 0, buffer.Length);
308                     fs.Flush();
309                     fs.Close();
310                 }
311                 catch
312                 {
313                     rtun = i;
314                     break;
315                 }
316             }
317             return rtun;
318         }
319 
320 
321         /// <summary>
322         /// 加载数据
323         /// </summary>
324         /// <param name="vImgUrl">图片的Url</param>
325         /// <returns></returns>
326         public static byte[] LoadToByte(string vUrl)
327         {
328             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(vUrl);
329             request.ReadWriteTimeout = 30000;
330             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
331 
332             Stream stream = response.GetResponseStream();
333             int bufferL = 1024;
334             byte[] buffer = new byte[bufferL];
335             int readL = stream.Read(buffer, 0, bufferL);
336             MemoryStream ms = new MemoryStream();
337             while (readL > 0)
338             {
339                 ms.Write(buffer, 0, readL);
340                 readL = stream.Read(buffer, 0, bufferL);
341             }
342             stream.Close();
343             response.Close();
344             byte[] rbtn = ms.ToArray();
345             ms.Close();
346             return rbtn;
347         }
348 
349         public static string LoadToString(string vUrl)
350         {
351             HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(vUrl);
352             HttpWebResponse response = (HttpWebResponse)request.GetResponse();
353             Stream stream = response.GetResponseStream();
354             StreamReader reader = new StreamReader(stream);
355             return reader.ReadToEnd();
356         }
357     }
358     public class SystemHelper
359     {
360         public static bool PowerOffSystem()
361         {
362             return ExitSystem(Cnic.SafeNativeMethods.EWX_POWEROFF | Cnic.SafeNativeMethods.EWX_FORCE,false);
363         }
364 
365         public static bool RebootSystem()
366         {
367             return ExitSystem(Cnic.SafeNativeMethods.EWX_REBOOT | Cnic.SafeNativeMethods.EWX_FORCE, false);
368         }
369 
370         public static bool LogoffSystem()
371         {
372             return ExitSystem(Cnic.SafeNativeMethods.EWX_LOGOFF | Cnic.SafeNativeMethods.EWX_FORCE, false);
373         }
374         public static bool ShutdownSystem()
375         {
376             return ExitSystem(Cnic.SafeNativeMethods.EWX_SHUTDOWN | Cnic.SafeNativeMethods.EWX_FORCE, false);
377         }
378         public static bool LockSystem()
379         {
380             return Cnic.SafeNativeMethods.LockWorkStation();
381         }
382         public static bool ExitSystem(int ExitCode, bool vTipFlags)
383         {
384             IntPtr hToken = IntPtr.Zero;
385             Cnic.SafeNativeMethods.TOKEN_PRIVILEGES tkp;
386             if (!Cnic.SafeNativeMethods.OpenProcessToken(Cnic.SafeNativeMethods.GetCurrentProcess(), Cnic.SafeNativeMethods.TOKEN_ADJUST_PRIVILEGES | Cnic.SafeNativeMethods.TOKEN_QUERY, ref hToken))
387             {
388                 if (vTipFlags)
389                 {
390                     return false;
391                 }
392             }
393             tkp.PrivilegeCount = 1;
394             tkp.Luid = 0;
395             tkp.Attributes = Cnic.SafeNativeMethods.SE_PRIVILEGE_ENABLED;
396 
397             Cnic.SafeNativeMethods.LookupPrivilegeValue(null, Cnic.SafeNativeMethods.SE_SHUTDOWN_NAME, ref tkp.Luid);
398             Cnic.SafeNativeMethods.AdjustTokenPrivileges(hToken, false, ref tkp, 0, IntPtr.Zero, IntPtr.Zero);
399             if (Cnic.SafeNativeMethods.GetLastError() != Cnic.SafeNativeMethods.ERROR_SUCCESS)
400             {
401                 if (vTipFlags)
402                 {
403                     return false;
404                 }
405             }
406             if (!Cnic.SafeNativeMethods.ExitWindowsEx(ExitCode, 0)) { return false; }
407             return true;
408         }
409 
410         public static bool ExitSystem2()
411         {
412             Cnic.SafeNativeMethods.TOKEN_PRIVILEGES tp;
413             //注意:这里用的是GetCurrentThread,而不是GetCurrentProcessIntPtr
414             IntPtr hproc = Cnic.SafeNativeMethods.GetCurrentThread();
415             IntPtr htok = IntPtr.Zero;
416             //注意:这里用的是OpenThreadToken(打开线程令牌),而不是OpenProcessToken(打开进程令牌)
417             if (Cnic.SafeNativeMethods.OpenThreadToken(hproc, Cnic.SafeNativeMethods.TOKEN_ADJUST_PRIVILEGES | Cnic.SafeNativeMethods.TOKEN_QUERY, true, ref htok))
418             {
419                 tp.PrivilegeCount = 1;
420                 tp.Luid = 0;
421                 tp.Attributes = Cnic.SafeNativeMethods.SE_PRIVILEGE_ENABLED;
422                 if (Cnic.SafeNativeMethods.LookupPrivilegeValue(null, Cnic.SafeNativeMethods.SE_SHUTDOWN_NAME, ref tp.Luid))
423                 {
424                     if (Cnic.SafeNativeMethods.AdjustTokenPrivileges(htok, false, ref tp, 0, IntPtr.Zero, IntPtr.Zero))
425                     {
426                         if (Cnic.SafeNativeMethods.InitiateSystemShutdown("", "", 60, true, false))
427                         {
428                             return true;
429                         }
430                     }
431                 }
432             }
433             return false;
434         }
435 
436 
437         public static bool SetMonitorPower(IntPtr vHandle,bool vOff)
438         {
439             return Cnic.SafeNativeMethods.SendMessage(vHandle, 0x0112, 0xF170, 2) == 0;
440             //return Cnic.SafeNativeMethods.SendMessage(vHandle, Cnic.SafeNativeMethods.WM_SYSCOMMAND, Cnic.SafeNativeMethods.SC_MONITORPOWER, vOff ?2 : 1)==0;
441         }
442     }
443 }
View Code

 

posted on 2013-10-31 19:28  CodeBase  阅读(487)  评论(0编辑  收藏  举报