更新
This commit is contained in:
@@ -6,62 +6,62 @@ namespace ShrlAlgoToolkit.Core.Assists;
|
||||
|
||||
public static class FileEncryptAssist
|
||||
{
|
||||
public static void DecryptFile(string strPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var intLent = strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1; //设置截取的起始位置
|
||||
var intLong = strPath.Length; //设置截取的长度
|
||||
var strName = strPath.Substring(intLent, intLong - intLent); //要加密的文件名称
|
||||
var intTxt = strName.LastIndexOf(".", StringComparison.Ordinal); //设置截取的起始位置
|
||||
strName = strName.Substring(0, intTxt); //获取文件拓展名
|
||||
if (strName.LastIndexOf("Out", StringComparison.Ordinal) != -1)
|
||||
{
|
||||
strName = strName.Substring(0, strName.LastIndexOf("Out", StringComparison.Ordinal));
|
||||
}
|
||||
else
|
||||
{
|
||||
strName += "In";
|
||||
}
|
||||
public static void DecryptFile(string strPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var intLent = strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1; //设置截取的起始位置
|
||||
var intLong = strPath.Length; //设置截取的长度
|
||||
var strName = strPath.Substring(intLent, intLong - intLent); //要加密的文件名称
|
||||
var intTxt = strName.LastIndexOf(".", StringComparison.Ordinal); //设置截取的起始位置
|
||||
strName = strName.Substring(0, intTxt); //获取文件拓展名
|
||||
if (strName.LastIndexOf("Out", StringComparison.Ordinal) != -1)
|
||||
{
|
||||
strName = strName.Substring(0, strName.LastIndexOf("Out", StringComparison.Ordinal));
|
||||
}
|
||||
else
|
||||
{
|
||||
strName += "In";
|
||||
}
|
||||
|
||||
var strInName = strPath.Substring(0, strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1) + strName + ".txt"; //解密后的文件名及路径
|
||||
//解密文件密钥
|
||||
byte[] key =
|
||||
{
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
84,
|
||||
19,
|
||||
37,
|
||||
118,
|
||||
104,
|
||||
85,
|
||||
121,
|
||||
27,
|
||||
93,
|
||||
86,
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
9,
|
||||
2,
|
||||
49,
|
||||
69,
|
||||
73,
|
||||
92
|
||||
};
|
||||
byte[] iv = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
|
||||
var strInName = strPath.Substring(0, strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1) + strName + ".txt"; //解密后的文件名及路径
|
||||
//解密文件密钥
|
||||
byte[] key =
|
||||
{
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
84,
|
||||
19,
|
||||
37,
|
||||
118,
|
||||
104,
|
||||
85,
|
||||
121,
|
||||
27,
|
||||
93,
|
||||
86,
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
9,
|
||||
2,
|
||||
49,
|
||||
69,
|
||||
73,
|
||||
92
|
||||
};
|
||||
byte[] iv = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
|
||||
#if NET8_0
|
||||
var myRijndael = Aes.Create(); //创建RijndaelManaged对象
|
||||
#else
|
||||
@@ -69,95 +69,108 @@ public static class FileEncryptAssist
|
||||
#endif
|
||||
//创建RijndaelManaged对象
|
||||
var fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read); //创建FileStream对象
|
||||
var csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read);
|
||||
var sr = new StreamReader(csDecrypt); //把文件读出来
|
||||
var sw = new StreamWriter(strInName); //解密后写一个新文件
|
||||
sw.Write(sr.ReadToEnd());
|
||||
sw.Flush();
|
||||
sw.Close(); //关闭FileStream对象
|
||||
fsOut.Close(); //关闭FileStream对象
|
||||
if (MessageBox.Show("解密成功!解密后的文件名及路径为:\n" + strInName + ",是否删除文件", "提示信息", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
File.Delete(strPath); //清除指定文件
|
||||
//tbpathname.Text = "";//请空文本文件
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
|
||||
public static void EncryptFile(string strPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var intLent = strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1; //设置截取的起始位置
|
||||
var intLong = strPath.Length; //设置截取的长度
|
||||
var strName = strPath.Substring(intLent, intLong - intLent); //要加密的文件名称
|
||||
var intTxt = strName.LastIndexOf(".", StringComparison.Ordinal); //设置截取的起始位置
|
||||
var intTxtLeng = strName.Length; //设置截取的长度
|
||||
var strTxt = strName.Substring(intTxt, intTxtLeng - intTxt); //取出文件的拓展名
|
||||
strName = strName.Substring(0, intTxt);
|
||||
var strOutName = strPath.Substring(0, strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1) + strName + "Out" + strTxt; //加密后的文件名及路径
|
||||
byte[] key =
|
||||
{
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
84,
|
||||
19,
|
||||
37,
|
||||
118,
|
||||
104,
|
||||
85,
|
||||
121,
|
||||
27,
|
||||
93,
|
||||
86,
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
9,
|
||||
2,
|
||||
49,
|
||||
69,
|
||||
73,
|
||||
92
|
||||
}; //加密文件密钥
|
||||
byte[] iv = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
|
||||
var csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, iv), CryptoStreamMode.Read);
|
||||
var sr = new StreamReader(csDecrypt); //把文件读出来
|
||||
var sw = new StreamWriter(strInName); //解密后写一个新文件
|
||||
sw.Write(sr.ReadToEnd());
|
||||
sw.Flush();
|
||||
sw.Close(); //关闭FileStream对象
|
||||
fsOut.Close(); //关闭FileStream对象
|
||||
if (MessageBox.Show("解密成功!解密后的文件名及路径为:\n" + strInName + ",是否删除文件", "提示信息", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
File.Delete(strPath); //清除指定文件
|
||||
//tbpathname.Text = "";//请空文本文件
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 计算hash256
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
public static string ComputeFileHash(string filePath)
|
||||
{
|
||||
using (SHA256 sha256 = SHA256.Create())
|
||||
using (FileStream stream = File.OpenRead(filePath))
|
||||
{
|
||||
byte[] hashBytes = sha256.ComputeHash(stream);
|
||||
return BitConverter.ToString(hashBytes).Replace("-", string.Empty).ToLower();
|
||||
}
|
||||
}
|
||||
public static void EncryptFile(string strPath)
|
||||
{
|
||||
try
|
||||
{
|
||||
var intLent = strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1; //设置截取的起始位置
|
||||
var intLong = strPath.Length; //设置截取的长度
|
||||
var strName = strPath.Substring(intLent, intLong - intLent); //要加密的文件名称
|
||||
var intTxt = strName.LastIndexOf(".", StringComparison.Ordinal); //设置截取的起始位置
|
||||
var intTxtLeng = strName.Length; //设置截取的长度
|
||||
var strTxt = strName.Substring(intTxt, intTxtLeng - intTxt); //取出文件的拓展名
|
||||
strName = strName.Substring(0, intTxt);
|
||||
var strOutName = strPath.Substring(0, strPath.LastIndexOf("\\", StringComparison.Ordinal) + 1) + strName + "Out" + strTxt; //加密后的文件名及路径
|
||||
byte[] key =
|
||||
{
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
84,
|
||||
19,
|
||||
37,
|
||||
118,
|
||||
104,
|
||||
85,
|
||||
121,
|
||||
27,
|
||||
93,
|
||||
86,
|
||||
24,
|
||||
55,
|
||||
102,
|
||||
24,
|
||||
98,
|
||||
26,
|
||||
67,
|
||||
29,
|
||||
9,
|
||||
2,
|
||||
49,
|
||||
69,
|
||||
73,
|
||||
92
|
||||
}; //加密文件密钥
|
||||
byte[] iv = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 24, 98, 26, 67, 29, 99 };
|
||||
#if NET8_0
|
||||
var myRijndael = Aes.Create();
|
||||
#else
|
||||
var myRijndael = new RijndaelManaged();
|
||||
#endif
|
||||
var fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write);
|
||||
var fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);
|
||||
var csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, iv), CryptoStreamMode.Write); //写入加密文本文件
|
||||
var br = new BinaryReader(fsIn); //创建阅读器来加密文件
|
||||
csDecrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length); //将数据写入加密文件
|
||||
csDecrypt.Close(); //关闭CryptoStream对象
|
||||
fsIn.Close(); //关闭FileStream对象
|
||||
fsOut.Close(); //关闭FileStream对象
|
||||
if (MessageBox.Show("加密成功!加密后的文件名及路径为:\n" + strOutName + ",是否删除文件", "提示信息", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
File.Delete(strPath); //清除指定文件
|
||||
//tbpathname.Text = "";//请空文本文件
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
var fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);
|
||||
var csDecrypt = new CryptoStream(fsOut, myRijndael.CreateDecryptor(key, iv), CryptoStreamMode.Write); //写入加密文本文件
|
||||
var br = new BinaryReader(fsIn); //创建阅读器来加密文件
|
||||
csDecrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length); //将数据写入加密文件
|
||||
csDecrypt.Close(); //关闭CryptoStream对象
|
||||
fsIn.Close(); //关闭FileStream对象
|
||||
fsOut.Close(); //关闭FileStream对象
|
||||
if (MessageBox.Show("加密成功!加密后的文件名及路径为:\n" + strOutName + ",是否删除文件", "提示信息", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
|
||||
{
|
||||
File.Delete(strPath); //清除指定文件
|
||||
//tbpathname.Text = "";//请空文本文件
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show(ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,35 @@ public static class IOAssist
|
||||
var files = Directory.GetFiles($"{path}", "*", SearchOption.AllDirectories); //遍历所有文件夹
|
||||
var list = files.Union(files).OrderBy(s => s);
|
||||
}
|
||||
/// <summary>
|
||||
/// 获取当前文件的副本或备份文件名
|
||||
/// </summary>
|
||||
/// <param name="filePath"></param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="FileNotFoundException"></exception>
|
||||
public static string GetUniqueFilePath(string filePath)
|
||||
{
|
||||
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
|
||||
{
|
||||
throw new FileNotFoundException($"文件'{filePath}'不存在。");
|
||||
}
|
||||
var dir = Path.GetDirectoryName(filePath);
|
||||
var extension = Path.GetExtension(filePath);
|
||||
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
|
||||
var counter = 1;
|
||||
|
||||
while (true)
|
||||
{
|
||||
var candidateFileName = $"{fileNameWithoutExtension}.{counter:D3}{extension}";
|
||||
var fullPath = Path.Combine(dir, candidateFileName);
|
||||
if (!File.Exists(fullPath))
|
||||
{
|
||||
return fullPath;
|
||||
}
|
||||
|
||||
counter++;
|
||||
}
|
||||
}
|
||||
public static string GetRelativePath(string fromPath, string toPath)
|
||||
{
|
||||
Uri uri = new(fromPath);
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Drawing;
|
||||
using System.Drawing.Text;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
@@ -7,9 +11,11 @@ using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Interop;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Media.Imaging;
|
||||
|
||||
namespace ShrlAlgoToolkit.Core.Assists;
|
||||
|
||||
public class ImageAssist
|
||||
{
|
||||
/// <summary>
|
||||
@@ -28,6 +34,54 @@ public class ImageAssist
|
||||
stream.Write(array, 0, count);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// 资源的方式转换
|
||||
///<c>var bitmap16 = ImageAssist.CreateIconBitmapSource(BoxIconFont.BxDownload, FontFamily, 16, Brushes.Black, 16, 16);</c>
|
||||
///<c>var floorFinishesPbd = new PushButtonData(</c>
|
||||
///<c> "房间饰面",</c>
|
||||
///<c> "房间饰面",</c>
|
||||
///<c> Variables.AddInPath,</c>
|
||||
///<c> typeof(FloorFinishesCmd).FullName)</c>
|
||||
///<c>{
|
||||
///<c> Image = bitmap16,</c>
|
||||
///<c> LargeImage = bitmap16,</c>
|
||||
///<c>};</c>
|
||||
/// </summary>
|
||||
/// <param name="iconText"></param>
|
||||
/// <param name="fontFamily"></param>
|
||||
/// <param name="fontSize">字体大小</param>
|
||||
/// <param name="foreground">前景色</param>
|
||||
/// <param name="width">宽度</param>
|
||||
/// <param name="height">高度</param>
|
||||
/// <returns></returns>
|
||||
public static BitmapSource CreateIconBitmapSource(string iconText,
|
||||
System.Windows.Media.FontFamily fontFamily,
|
||||
double fontSize,
|
||||
System.Windows.Media.Brush foreground,
|
||||
double width,
|
||||
double height)
|
||||
{
|
||||
var formattedText = new FormattedText(
|
||||
iconText,
|
||||
CultureInfo.InvariantCulture,
|
||||
FlowDirection.LeftToRight,
|
||||
new Typeface(fontFamily, FontStyles.Normal, FontWeights.Normal, FontStretches.Normal),
|
||||
fontSize,
|
||||
foreground,
|
||||
1.0);
|
||||
|
||||
var drawingVisual = new DrawingVisual();
|
||||
using (var drawingContext = drawingVisual.RenderOpen())
|
||||
{
|
||||
var x = (width - formattedText.Width) / 2;
|
||||
var y = (height - formattedText.Height) / 2;
|
||||
drawingContext.DrawText(formattedText, new System.Windows.Point(x, y));
|
||||
}
|
||||
|
||||
var renderTarget = new RenderTargetBitmap((int)Math.Ceiling(width), (int)Math.Ceiling(height), 96, 96, PixelFormats.Pbgra32);
|
||||
renderTarget.Render(drawingVisual);
|
||||
return renderTarget;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 资源管理器缩略图读取
|
||||
@@ -39,6 +93,59 @@ public class ImageAssist
|
||||
public static BitmapSource LoadFileImage(string filename, int width, int height)
|
||||
{
|
||||
var bm = WindowsThumbnailProvider.GetThumbnail(filename, width, height, ThumbnailOptions.None);
|
||||
return Imaging.CreateBitmapSourceFromHBitmap(bm.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
|
||||
return Imaging.CreateBitmapSourceFromHBitmap(
|
||||
bm.GetHbitmap(),
|
||||
IntPtr.Zero,
|
||||
Int32Rect.Empty,
|
||||
BitmapSizeOptions.FromEmptyOptions());
|
||||
}
|
||||
/// <summary>
|
||||
/// 输出字体文件的方式转换
|
||||
/// </summary>
|
||||
/// <param name="fontPath">tff文件路径,如Fonts/boxicons.tff</param>
|
||||
/// <param name="unicodeChar">图标的unicode字符串</param>
|
||||
/// <param name="width"></param>
|
||||
/// <param name="height"></param>
|
||||
/// <returns></returns>
|
||||
public static Bitmap RenderIconFontToBitmap(string fontPath, string unicodeChar, int width, int height)
|
||||
{
|
||||
// 加载字体
|
||||
PrivateFontCollection fontCollection = new PrivateFontCollection();
|
||||
fontCollection.AddFontFile(fontPath);
|
||||
System.Drawing.FontFamily fontFamily = fontCollection.Families[0];
|
||||
|
||||
// 创建位图
|
||||
Bitmap bitmap = new Bitmap(width, height);
|
||||
using (Graphics g = Graphics.FromImage(bitmap))
|
||||
{
|
||||
g.Clear(System.Drawing.Color.Transparent);
|
||||
using (Font font = new Font(fontFamily, Math.Min(width, height) / 2))
|
||||
{
|
||||
g.TextRenderingHint = TextRenderingHint.AntiAlias;
|
||||
StringFormat format = new StringFormat
|
||||
{
|
||||
Alignment = StringAlignment.Center,
|
||||
LineAlignment = StringAlignment.Center
|
||||
};
|
||||
g.DrawString(unicodeChar, font, System.Drawing.Brushes.Black, new RectangleF(0, 0, width, height), format);
|
||||
}
|
||||
}
|
||||
|
||||
return bitmap;
|
||||
}
|
||||
|
||||
public static ImageSource ConvertBitmapToImageSource(Bitmap bitmap)
|
||||
{
|
||||
using (MemoryStream memory = new MemoryStream())
|
||||
{
|
||||
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
|
||||
memory.Position = 0;
|
||||
BitmapImage bitmapImage = new BitmapImage();
|
||||
bitmapImage.BeginInit();
|
||||
bitmapImage.StreamSource = memory;
|
||||
bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
|
||||
bitmapImage.EndInit();
|
||||
return bitmapImage;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user