* 重构公共类
This commit is contained in:
parent
cfa40a5381
commit
47d1057945
@ -19,10 +19,7 @@
|
||||
* 2020-01-01: V2.2.0 增加文件说明
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace Sunny.UI
|
||||
@ -32,48 +29,6 @@ namespace Sunny.UI
|
||||
/// </summary>
|
||||
public static class DirEx
|
||||
{
|
||||
/// <summary>
|
||||
/// 创建一个目录
|
||||
/// </summary>
|
||||
/// <param name="dir">目录的绝对路径</param>
|
||||
public static bool CreateDir(string dir)
|
||||
{
|
||||
if (dir.IsNullOrEmpty())
|
||||
{
|
||||
throw new ArgumentNullException(dir, "目录不能为空。");
|
||||
}
|
||||
|
||||
if (!Path.IsPathRooted(dir))
|
||||
{
|
||||
throw new ArgumentException(dir, "不包含根路径。");
|
||||
}
|
||||
|
||||
//
|
||||
//
|
||||
try
|
||||
{
|
||||
if (!Directory.Exists(dir))
|
||||
{
|
||||
Directory.CreateDirectory(dir);
|
||||
}
|
||||
|
||||
return Directory.Exists(dir);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Console.WriteLine(e.Message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 返回当前基目录
|
||||
/// </summary>
|
||||
/// <returns>基目录</returns>
|
||||
public static string CurrentDir()
|
||||
{
|
||||
return AppDomain.CurrentDomain.BaseDirectory.DealPath();
|
||||
}
|
||||
/// <summary>
|
||||
/// 选择文件夹
|
||||
/// </summary>
|
||||
@ -119,163 +74,5 @@ namespace Sunny.UI
|
||||
System.Diagnostics.Process.Start("Explorer.exe", dir);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定目录中的匹配项(文件或目录)
|
||||
/// </summary>
|
||||
/// <param name="dir">要搜索的目录</param>
|
||||
/// <param name="regexPattern">项名模式(正则)。null表示忽略模式匹配,返回所有项</param>
|
||||
/// <param name="depth">递归深度。负数表示不限,0表示仅顶级</param>
|
||||
/// <param name="throwEx">是否抛异常</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string[] GetFileSystemEntries(string dir, string regexPattern = null, int depth = 0, bool throwEx = false)
|
||||
{
|
||||
List<string> lst = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (string item in Directory.GetFileSystemEntries(dir))
|
||||
{
|
||||
try
|
||||
{
|
||||
string filename = Path.GetFileName(item);
|
||||
|
||||
if (regexPattern == null || Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))
|
||||
{
|
||||
lst.Add(item);
|
||||
}
|
||||
|
||||
//递归
|
||||
if (depth != 0 && (File.GetAttributes(item) & FileAttributes.Directory) == FileAttributes.Directory)
|
||||
{
|
||||
lst.AddRange(GetFileSystemEntries(item, regexPattern, depth - 1, throwEx));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwEx)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwEx)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return lst.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定目录中的匹配文件
|
||||
/// </summary>
|
||||
/// <param name="dir">要搜索的目录</param>
|
||||
/// <param name="regexPattern">文件名模式(正则)。null表示忽略模式匹配,返回所有文件</param>
|
||||
/// <param name="depth">递归深度。负数表示不限,0表示仅顶级</param>
|
||||
/// <param name="throwEx">是否抛异常</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string[] GetFiles(string dir, string regexPattern = null, int depth = 0, bool throwEx = false)
|
||||
{
|
||||
List<string> lst = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (string item in Directory.GetFileSystemEntries(dir))
|
||||
{
|
||||
try
|
||||
{
|
||||
bool isFile = (File.GetAttributes(item) & FileAttributes.Directory) != FileAttributes.Directory;
|
||||
string filename = Path.GetFileName(item);
|
||||
|
||||
if (isFile && (regexPattern == null || Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline)))
|
||||
{
|
||||
lst.Add(item);
|
||||
}
|
||||
|
||||
//递归
|
||||
if (depth != 0 && !isFile)
|
||||
{
|
||||
lst.AddRange(GetFiles(item, regexPattern, depth - 1, throwEx));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwEx)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwEx)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return lst.ToArray();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取指定目录中的匹配目录
|
||||
/// </summary>
|
||||
/// <param name="dir">要搜索的目录</param>
|
||||
/// <param name="regexPattern">目fu录名模式(正则)。null表示忽略模式匹配,返回所有目录</param>
|
||||
/// <param name="depth">递归深度。负数表示不限,0表示仅顶级</param>
|
||||
/// <param name="throwEx">是否抛异常</param>
|
||||
/// <returns>结果</returns>
|
||||
public static string[] GetDirectories(string dir, string regexPattern = null, int depth = 0, bool throwEx = false)
|
||||
{
|
||||
List<string> lst = new List<string>();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (string item in Directory.GetDirectories(dir))
|
||||
{
|
||||
try
|
||||
{
|
||||
string filename = Path.GetFileName(item);
|
||||
if (filename == null)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if (regexPattern == null || Regex.IsMatch(filename, regexPattern, RegexOptions.IgnoreCase | RegexOptions.Multiline))
|
||||
{
|
||||
lst.Add(item);
|
||||
}
|
||||
|
||||
//递归
|
||||
if (depth != 0)
|
||||
{
|
||||
lst.AddRange(GetDirectories(item, regexPattern, depth - 1, throwEx));
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwEx)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
if (throwEx)
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
return lst.ToArray();
|
||||
}
|
||||
}
|
||||
}
|
@ -53,15 +53,6 @@ namespace Sunny.UI
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 调用WINAPI删除文件
|
||||
/// </summary>
|
||||
/// <param name="lpFileName">文件</param>
|
||||
public static int DeleteFile(string lpFileName)
|
||||
{
|
||||
return Win32.Kernel.DeleteFile(lpFileName);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开文件夹对话框
|
||||
/// </summary>
|
||||
|
@ -90,7 +90,7 @@ namespace Sunny.UI
|
||||
{
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
{
|
||||
filename = DirEx.CurrentDir() + ConfigFile;
|
||||
filename = Dir.CurrentDir() + ConfigFile;
|
||||
}
|
||||
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
@ -148,7 +148,7 @@ namespace Sunny.UI
|
||||
{
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
{
|
||||
filename = DirEx.CurrentDir() + ConfigFile;
|
||||
filename = Dir.CurrentDir() + ConfigFile;
|
||||
}
|
||||
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
@ -217,7 +217,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
listidents.Clear();
|
||||
DirEx.CreateDir(Path.GetDirectoryName(filename));
|
||||
Dir.CreateDir(Path.GetDirectoryName(filename));
|
||||
string filetmp = filename + "." + RandomEx.RandomPureChar(3);
|
||||
File.Delete(filetmp);
|
||||
StreamWriter sw = new StreamWriter(filetmp, false, IniEncoding);
|
||||
@ -283,7 +283,7 @@ namespace Sunny.UI
|
||||
{
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
{
|
||||
filename = DirEx.CurrentDir() + ConfigFile;
|
||||
filename = Dir.CurrentDir() + ConfigFile;
|
||||
}
|
||||
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
@ -341,7 +341,7 @@ namespace Sunny.UI
|
||||
{
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
{
|
||||
filename = DirEx.CurrentDir() + ConfigFile;
|
||||
filename = Dir.CurrentDir() + ConfigFile;
|
||||
}
|
||||
|
||||
if (filename.IsNullOrWhiteSpace())
|
||||
@ -410,7 +410,7 @@ namespace Sunny.UI
|
||||
}
|
||||
|
||||
listidents.Clear();
|
||||
DirEx.CreateDir(Path.GetDirectoryName(filename));
|
||||
Dir.CreateDir(Path.GetDirectoryName(filename));
|
||||
string filetmp = filename + "." + RandomEx.RandomPureChar(3);
|
||||
File.Delete(filetmp);
|
||||
StreamWriter sw = new StreamWriter(filetmp, false, Encoding.UTF8);
|
||||
|
@ -65,7 +65,7 @@ namespace Sunny.UI
|
||||
FileName = fileName;
|
||||
|
||||
FileInfo fi = new FileInfo(FileName);
|
||||
DirEx.CreateDir(fi.DirectoryName);
|
||||
Dir.CreateDir(fi.DirectoryName);
|
||||
|
||||
if (!Directory.Exists(fi.DirectoryName))
|
||||
{
|
||||
|
@ -97,8 +97,10 @@ namespace Sunny.UI
|
||||
string jsonStr = Serialize(obj);
|
||||
try
|
||||
{
|
||||
DirEx.CreateDir(Path.GetDirectoryName(filename));
|
||||
File.WriteAllText(filename, jsonStr, encoding);
|
||||
StreamWriter streamWriter = new StreamWriter(filename, false, encoding);
|
||||
streamWriter.Write(jsonStr);
|
||||
streamWriter.Flush();
|
||||
streamWriter.Close();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
|
@ -169,7 +169,7 @@ namespace Sunny.UI
|
||||
{
|
||||
Process process = new Process();
|
||||
process.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //隐藏压缩窗口
|
||||
process.StartInfo.FileName = DirEx.CurrentDir() + "7z.exe";
|
||||
process.StartInfo.FileName = Dir.CurrentDir() + "7z.exe";
|
||||
process.StartInfo.CreateNoWindow = false;
|
||||
process.StartInfo.Arguments = arguments;
|
||||
process.Start();
|
||||
|
@ -1,189 +0,0 @@
|
||||
/******************************************************************************
|
||||
* SunnyUI 开源控件库、工具类库、扩展类库、多页面开发框架。
|
||||
* CopyRight (C) 2012-2024 ShenYongHua(沈永华).
|
||||
* QQ群:56829229 QQ:17612584 EMail:SunnyUI@QQ.Com
|
||||
*
|
||||
* Blog: https://www.cnblogs.com/yhuse
|
||||
* Gitee: https://gitee.com/yhuse/SunnyUI
|
||||
* GitHub: https://github.com/yhuse/SunnyUI
|
||||
*
|
||||
* SunnyUI.dll can be used for free under the GPL-3.0 license.
|
||||
* If you use this code, please keep this note.
|
||||
* 如果您使用此代码,请保留此说明。
|
||||
******************************************************************************
|
||||
* 文件名称: USuspendCtrlAltDel.cs
|
||||
* 文件说明: 通过挂起winlogon线程来屏蔽Ctrl+Alt+Delete
|
||||
* 当前版本: V3.1
|
||||
* 创建日期: 2020-01-01
|
||||
*
|
||||
* 2020-09-17: V2.2.7 增加文件说明
|
||||
******************************************************************************/
|
||||
|
||||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Sunny.UI
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过挂起winlogon.exe线程来屏蔽Ctrl+Alt+Delete
|
||||
/// 程序需要管理员权限
|
||||
/// 需要提升进程权限为SE_PRIVILEGE_ENABLED权限
|
||||
/// winlogon管理着开关机登录界面等功能
|
||||
/// 调用Suspend挂起winlogon.exe,用完后需要Resume,否则无法调用关机界面
|
||||
/// </summary>
|
||||
public static class SuspendCtrlAltDelete
|
||||
{
|
||||
/// <summary>
|
||||
/// 获取权限
|
||||
/// </summary>
|
||||
public static void GetSeDebugPrivilege()
|
||||
{
|
||||
IntPtr hToken;
|
||||
LUID luidSEDebugNameValue = new LUID();
|
||||
|
||||
|
||||
if (!OpenProcessToken(Process.GetCurrentProcess().Handle, TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out hToken))
|
||||
{
|
||||
Console.WriteLine("OpenProcessToken() failed, error = {0} . SeDebugPrivilege is not available", Marshal.GetLastWin32Error());
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("OpenProcessToken() successfully");
|
||||
}
|
||||
|
||||
if (!LookupPrivilegeValue(null, SE_DEBUG_NAME, ref luidSEDebugNameValue))
|
||||
{
|
||||
Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeDebugPrivilege is not available", Marshal.GetLastWin32Error());
|
||||
CloseHandle(hToken);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("LookupPrivilegeValue() successfully");
|
||||
}
|
||||
|
||||
TOKEN_PRIVILEGES tkpPrivileges = new TOKEN_PRIVILEGES();
|
||||
tkpPrivileges.PrivilegeCount = 1;
|
||||
tkpPrivileges.Privilege.Luid = luidSEDebugNameValue;
|
||||
tkpPrivileges.Privilege.Attributes = SE_PRIVILEGE_ENABLED;
|
||||
|
||||
if (!AdjustTokenPrivileges(hToken, false, ref tkpPrivileges, 0, IntPtr.Zero, 0))
|
||||
{
|
||||
Console.WriteLine("LookupPrivilegeValue() failed, error = {0} .SeDebugPrivilege is not available", Marshal.GetLastWin32Error());
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("SeDebugPrivilege is now available");
|
||||
}
|
||||
|
||||
CloseHandle(hToken);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 挂起
|
||||
/// </summary>
|
||||
public static void Suspend()
|
||||
{
|
||||
Process[] processes = Process.GetProcesses();
|
||||
foreach (Process process in processes)
|
||||
{
|
||||
if (process.ProcessName == "winlogon")
|
||||
{
|
||||
|
||||
IntPtr p = OpenThread(PROCESS_ALL_ACCESS, false, (IntPtr)process.Threads[0].Id);
|
||||
SuspendThread(p);
|
||||
CloseHandle(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 恢复
|
||||
/// </summary>
|
||||
public static void Resume()
|
||||
{
|
||||
Process[] processes = Process.GetProcesses();
|
||||
foreach (Process process in processes)
|
||||
{
|
||||
if (process.ProcessName == "winlogon")
|
||||
{
|
||||
IntPtr p = OpenThread(PROCESS_ALL_ACCESS, false, (IntPtr)process.Threads[0].Id);
|
||||
ResumeThread(p);
|
||||
CloseHandle(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const int PROCESS_ALL_ACCESS = 0x001F03FF;
|
||||
const string SE_DEBUG_NAME = "SeDebugPrivilege";
|
||||
const uint SE_PRIVILEGE_ENABLED = 0x00000002;
|
||||
const uint STANDARD_RIGHTS_REQUIRED = 0x000F0000;
|
||||
const uint STANDARD_RIGHTS_READ = 0x00020000;
|
||||
const uint TOKEN_ASSIGN_PRIMARY = 0x0001;
|
||||
const uint TOKEN_DUPLICATE = 0x0002;
|
||||
const uint TOKEN_IMPERSONATE = 0x0004;
|
||||
const uint TOKEN_QUERY = 0x0008;
|
||||
const uint TOKEN_QUERY_SOURCE = 0x0010;
|
||||
const uint TOKEN_ADJUST_PRIVILEGES = 0x0020;
|
||||
const uint TOKEN_ADJUST_GROUPS = 0x0040;
|
||||
const uint TOKEN_ADJUST_DEFAULT = 0x0080;
|
||||
const uint TOKEN_ADJUST_SESSIONID = 0x0100;
|
||||
const uint TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY;
|
||||
const uint TOKEN_ALL_ACCESS = (STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY |
|
||||
TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY | TOKEN_QUERY_SOURCE |
|
||||
TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT |
|
||||
TOKEN_ADJUST_SESSIONID);
|
||||
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern IntPtr OpenThread(int dwDesiredAccess, bool bInheritHandle, IntPtr dwThreadId);
|
||||
|
||||
[DllImport("kernel32.dll")]
|
||||
private static extern bool CloseHandle(IntPtr hObject);
|
||||
|
||||
[DllImport("kernel32")]
|
||||
private static extern int SuspendThread(IntPtr hThread);
|
||||
|
||||
[DllImport("kernel32")]
|
||||
private static extern int ResumeThread(IntPtr hThread);
|
||||
|
||||
[DllImport("advapi32.dll", SetLastError = true)]
|
||||
private static extern bool OpenProcessToken(IntPtr processHandle, uint desiredAccess, out IntPtr tokenHandle);
|
||||
|
||||
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool LookupPrivilegeValue(string lpSystemName, string lpName,
|
||||
[MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
|
||||
|
||||
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
||||
[return: MarshalAs(UnmanagedType.Bool)]
|
||||
private static extern bool AdjustTokenPrivileges(IntPtr TokenHandle,
|
||||
[MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges,
|
||||
[MarshalAs(UnmanagedType.Struct)] ref TOKEN_PRIVILEGES NewState,
|
||||
uint BufferLength, IntPtr PreviousState, uint ReturnLength);
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct LUID
|
||||
{
|
||||
internal int LowPart;
|
||||
internal uint HighPart;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct TOKEN_PRIVILEGES
|
||||
{
|
||||
internal int PrivilegeCount;
|
||||
internal LUID_AND_ATTRIBUTES Privilege;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
||||
private struct LUID_AND_ATTRIBUTES
|
||||
{
|
||||
internal LUID Luid;
|
||||
internal uint Attributes;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user