* 增加几个通用函数

This commit is contained in:
Sunny 2022-07-13 11:41:53 +08:00
parent 6507a361d5
commit a1b1b91049
4 changed files with 118 additions and 1 deletions

View File

@ -32,6 +32,28 @@ namespace Sunny.UI
/// </summary>
public static class DirEx
{
/// <summary>
/// 调用WINAPI删除文件夹
/// </summary>
/// <param name="directory">文件夹</param>
public static void Delete(this string directory)
{
if (Directory.Exists(directory))
{
var files = Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories);
foreach (string file in files)
{
Win32.Kernel.DeleteFile(file);
}
var directories = Directory.EnumerateDirectories(directory, "*", SearchOption.AllDirectories);
foreach (string dir in directories)
{
Directory.Delete(dir, true);
}
}
}
/// <summary>
/// 当前可执行文件路径,末尾包括文件夹分隔符(windows为\linux为/)
/// </summary>

View File

@ -36,6 +36,32 @@ namespace Sunny.UI
/// </summary>
public static class FileEx
{
/// <summary>
/// 调用WINAPIf复制文件
/// </summary>
/// <param name="sourceFileName"></param>
/// <param name="targetFileName"></param>
/// <param name="bFailIfExists"></param>
/// <returns></returns>
public static bool Copy(this string sourceFileName, string targetFileName, bool bFailIfExists = true)
{
if (File.Exists(sourceFileName))
{
return Win32.Kernel.CopyFile(sourceFileName, targetFileName, bFailIfExists);
}
return false;
}
/// <summary>
/// 调用WINAPI删除文件
/// </summary>
/// <param name="lpFileName">文件</param>
public static int DeleteFile(string lpFileName)
{
return Win32.Kernel.DeleteFile(lpFileName);
}
/// <summary>
/// 打开文件夹对话框
/// </summary>

View File

@ -1,4 +1,8 @@
namespace Sunny.UI
using Sunny.UI.Win32;
using System.Collections.Generic;
using System.Linq;
namespace Sunny.UI
{
/// <summary>
/// 其他
@ -64,5 +68,21 @@
{
return d.IsNan() || d.IsInfinity();
}
/// <summary>
/// 自然排序
/// </summary>
/// <param name="strs">字符串列表</param>
/// <returns>自然排序结果</returns>
/// var names = new [] { "2.log", "10.log", "1.log" };
/// 排序结果:
/// 1.log
/// 2.log
/// 10.log
public static IOrderedEnumerable<string> NatualOrdering(this IEnumerable<string> strs)
{
if (strs == null) return null;
return strs.OrderBy(s => s, new NatualOrderingComparer());
}
}
}

View File

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Runtime.InteropServices;
namespace Sunny.UI.Win32
@ -161,6 +163,10 @@ namespace Sunny.UI.Win32
public partial class Kernel
{
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern Int32 CompareStringEx(string localeName, int flags, string str1, int count1, string str2,
int count2, IntPtr versionInformation, IntPtr reserved, int param);
[DllImport("kernel32.dll")]
public static extern IntPtr OpenThread(int dwDesiredAccess, bool bInheritHandle, IntPtr dwThreadId);
@ -177,6 +183,49 @@ namespace Sunny.UI.Win32
//从指定内存中读取字节集数据
[DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]
public static extern bool ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, IntPtr lpNumberOfBytesRead);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CopyFile(string lpExistingFileName, string lpNewFileName, bool bFailIfExists);
}
internal class NatualOrderingComparer : IComparer<string>
{
static readonly Int32 NORM_IGNORECASE = 0x00000001;
static readonly Int32 NORM_IGNORENONSPACE = 0x00000002;
static readonly Int32 NORM_IGNORESYMBOLS = 0x00000004;
static readonly Int32 LINGUISTIC_IGNORECASE = 0x00000010;
static readonly Int32 LINGUISTIC_IGNOREDIACRITIC = 0x00000020;
static readonly Int32 NORM_IGNOREKANATYPE = 0x00010000;
static readonly Int32 NORM_IGNOREWIDTH = 0x00020000;
static readonly Int32 NORM_LINGUISTIC_CASING = 0x08000000;
static readonly Int32 SORT_STRINGSORT = 0x00001000;
static readonly Int32 SORT_DIGITSASNUMBERS = 0x00000008;
static readonly String LOCALE_NAME_USER_DEFAULT = null;
static readonly String LOCALE_NAME_INVARIANT = String.Empty;
static readonly String LOCALE_NAME_SYSTEM_DEFAULT = "!sys-default-locale";
readonly String locale;
public NatualOrderingComparer() : this(CultureInfo.CurrentCulture)
{
}
public NatualOrderingComparer(CultureInfo cultureInfo)
{
if (cultureInfo.IsNeutralCulture)
this.locale = LOCALE_NAME_INVARIANT;
else
this.locale = cultureInfo.Name;
}
public Int32 Compare(String x, String y)
{
// CompareStringEx return 1, 2, or 3. Subtract 2 to get the return value.
return Kernel.CompareStringEx(this.locale, SORT_DIGITSASNUMBERS, // Add other flags if required.
x, x.Length, y, y.Length, IntPtr.Zero, IntPtr.Zero, 0) - 2;
}
}
public partial class WinMM