2025-12-13 16:33:41 +08:00
using NPOI.SS.UserModel ;
2025-08-29 14:57:55 +08:00
using System.Diagnostics ;
2025-09-03 16:16:00 +08:00
using System.IO ;
2025-10-29 17:07:22 +08:00
using System.Net ;
using System.Net.Sockets ;
2025-09-12 18:34:33 +08:00
using System.Security.Cryptography ;
2025-08-29 14:57:55 +08:00
using System.Text ;
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Media ;
2025-09-12 18:34:33 +08:00
using System.Windows.Media.Imaging ;
2025-08-29 14:57:55 +08:00
using System.Windows.Threading ;
namespace Ramitta.lib
{
public static class Basic
{
#region ȫ <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 92 , 40 , 147 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 134 , 27 , 45 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 202 , 81 , 0 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 66 , 164 , 60 ) ) ;
public static SolidColorBrush ͨ ֪ <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 0 , 122 , 204 ) ) ;
public static SolidColorBrush <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 30 , 30 , 30 ) ) ;
public static SolidColorBrush Ĭ <EFBFBD> ϻ <EFBFBD> ɫ = new SolidColorBrush ( Color . FromRgb ( 66 , 66 , 66 ) ) ;
#endregion
#region <EFBFBD> <EFBFBD> <EFBFBD> Ժ <EFBFBD> <EFBFBD> <EFBFBD>
2025-11-18 19:54:13 +08:00
public static void DebugBar ( Label obj , String ? text , SolidColorBrush ? color = null )
2025-08-29 14:57:55 +08:00
{
if ( obj = = null ) return ;
// ͨ<> <CDA8> Dispatcherȷ<72> <C8B7> <EFBFBD> <EFBFBD> UI<55> ߳<EFBFBD> ִ<EFBFBD> <D6B4>
obj . Dispatcher . Invoke ( ( ) = >
{
2025-11-18 19:54:13 +08:00
if ( text ! = null ) obj . Content = text ;
2025-09-12 18:34:33 +08:00
if ( color ! = null ) obj . Background = color ;
2025-10-15 18:53:43 +08:00
// ǿ<> <C7BF> ˢ<EFBFBD> <CBA2> UI
obj . InvalidateVisual ( ) ;
obj . InvalidateArrange ( ) ;
obj . InvalidateMeasure ( ) ;
obj . UpdateLayout ( ) ;
// <20> <> <EFBFBD> <EFBFBD> Dispatcher<65> <72> <EFBFBD> <EFBFBD> <EFBFBD> е <EFBFBD> <D0B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> IJ<EFBFBD> <C4B2> <EFBFBD> <EFBFBD> <EFBFBD> ȷ<EFBFBD> <C8B7> UI<55> <49> <EFBFBD> <EFBFBD>
Dispatcher . CurrentDispatcher . Invoke ( DispatcherPriority . Background , new Action ( delegate { } ) ) ;
2025-12-13 16:33:41 +08:00
2025-08-29 14:57:55 +08:00
} ) ;
}
2025-11-04 11:37:27 +08:00
public static SolidColorBrush GenerateRandomColor ( SolidColorBrush color = null , bool? requireLightColor = null )
2025-10-29 17:07:22 +08:00
{
// <20> <> <EFBFBD> <EFBFBD> color <20> <> <EFBFBD> <EFBFBD> Ϊ null<6C> <6C> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> µ<EFBFBD> <C2B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ
if ( color = = null )
{
Random random = new Random ( ) ;
2025-11-04 11:37:27 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ<EFBFBD> <C9AB> <EFBFBD> <EFBFBD> ɫ
if ( requireLightColor . HasValue )
{
int attempts = 0 ;
const int maxAttempts = 100 ; // <20> <> ֹ<EFBFBD> <D6B9> <EFBFBD> <EFBFBD> ѭ<EFBFBD> <D1AD>
while ( attempts < maxAttempts )
{
byte r = ( byte ) random . Next ( 0 , 256 ) ;
byte g = ( byte ) random . Next ( 0 , 256 ) ;
byte b = ( byte ) random . Next ( 0 , 256 ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> (ʹ <> ó<EFBFBD> <C3B3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȹ<EFBFBD> ʽ )
double brightness = ( 0.299 * r + 0.587 * g + 0.114 * b ) / 255 ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> Ƿ<EFBFBD> <C7B7> <EFBFBD> <EFBFBD> <EFBFBD> Ҫ<EFBFBD> <D2AA>
bool isLight = brightness > 0.5 ;
if ( ( requireLightColor . Value & & isLight ) | | ( ! requireLightColor . Value & & ! isLight ) )
{
color = new SolidColorBrush ( Color . FromRgb ( r , g , b ) ) ;
break ;
}
attempts + + ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> γ <EFBFBD> <CEB3> <EFBFBD> <EFBFBD> <EFBFBD> δ<EFBFBD> ҵ<EFBFBD> <D2B5> <EFBFBD> <EFBFBD> ʵ<EFBFBD> <CAB5> <EFBFBD> ɫ<EFBFBD> <C9AB> ʹ <EFBFBD> <CAB9> Ĭ<EFBFBD> Ϸ<EFBFBD> <CFB7> <EFBFBD>
if ( color = = null )
{
byte r = ( byte ) random . Next ( 0 , 256 ) ;
byte g = ( byte ) random . Next ( 0 , 256 ) ;
byte b = ( byte ) random . Next ( 0 , 256 ) ;
color = new SolidColorBrush ( Color . FromRgb ( r , g , b ) ) ;
}
}
else
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȣ <EFBFBD> ʹ <EFBFBD> <CAB9> ԭ<EFBFBD> <D4AD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
byte r = ( byte ) random . Next ( 0 , 256 ) ;
byte g = ( byte ) random . Next ( 0 , 256 ) ;
byte b = ( byte ) random . Next ( 0 , 256 ) ;
color = new SolidColorBrush ( Color . FromRgb ( r , g , b ) ) ;
}
2025-10-29 17:07:22 +08:00
}
return color ;
}
2025-08-29 14:57:55 +08:00
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> Զ<EFBFBD> <D4B6> <EFBFBD> <EFBFBD> 쳣<EFBFBD> <ECB3A3>
public class DebugbarException : Exception
{
public string Message { get ; }
public SolidColorBrush Brush { get ; }
public DebugbarException ( string message , SolidColorBrush brush )
: base ( message )
{
Message = message ;
Brush = brush ;
}
}
#endregion
2025-09-12 18:34:33 +08:00
#region <EFBFBD> ļ <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2025-12-29 11:48:57 +08:00
public static string? GetExeVersion ( string exePath )
{
try
{
if ( ! File . Exists ( exePath ) )
{
return null ; // <20> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> null
}
var versionInfo = FileVersionInfo . GetVersionInfo ( exePath ) ;
string? version = versionInfo . FileVersion ;
// <20> <> <EFBFBD> <EFBFBD> FileVersionΪ<6E> գ <EFBFBD> <D5A3> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> <CAB9> ProductVersion
if ( string . IsNullOrEmpty ( version ) )
{
version = versionInfo . ProductVersion ;
}
return string . IsNullOrEmpty ( version ) ? null : version ;
}
catch
{
return null ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> κ<EFBFBD> <CEBA> 쳣<EFBFBD> <ECB3A3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> null
}
}
2025-09-12 18:34:33 +08:00
public static bool IsPathExist ( string databasePath )
2025-09-03 16:16:00 +08:00
{
2025-09-12 18:34:33 +08:00
if ( string . IsNullOrWhiteSpace ( databasePath ) )
{
return false ;
}
return Directory . Exists ( databasePath ) ;
}
2025-11-18 19:54:13 +08:00
public static void FileWrite ( string outputPath , string outputText , bool createDirectories = true , bool fileAdd = false )
2025-09-12 18:34:33 +08:00
{
string directory = Path . GetDirectoryName ( outputPath ) ;
if ( createDirectories & & ! Directory . Exists ( directory ) )
{
Directory . CreateDirectory ( directory ) ;
}
// ʹ <> <CAB9> StreamWriter д<> <D0B4> <EFBFBD> ļ<EFBFBD>
2025-09-03 16:16:00 +08:00
using ( StreamWriter writer = new StreamWriter ( outputPath , false ) )
{
Debug . WriteLine ( outputText ) ;
writer . WriteLine ( outputText ) ;
}
}
public static string FileRead ( string outputPath )
{
2025-10-29 17:07:22 +08:00
try
{
2025-11-18 19:54:13 +08:00
return File . ReadAllText ( outputPath ) ;
2025-10-29 17:07:22 +08:00
}
catch
{
throw ;
}
2025-09-03 16:16:00 +08:00
}
2025-09-12 18:34:33 +08:00
// <20> <> ȡ·<C8A1> <C2B7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD> <EFBFBD> ļ<EFBFBD> <C4BC> У <EFBFBD> ֧<EFBFBD> ִ<EFBFBD> <D6B4> <EFBFBD> <EFBFBD> ж<EFBFBD> <D0B6> ļ<EFBFBD> <C4BC> ĺ<EFBFBD> <C4BA> <EFBFBD>
public static List < string > GetFilesAndDirectories ( string path , bool? isDirectory = null , Func < string , bool > fileFilter = null , Func < string , string > outputFormat = null )
{
List < string > result = new List < string > ( ) ;
// <20> <> ȡ<EFBFBD> ļ<EFBFBD> <C4BC> <EFBFBD>
var directories = Directory . GetDirectories ( path ) ;
foreach ( var dir in directories )
{
if ( isDirectory = = null | | isDirectory = = true )
{
// <20> <> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> ø<EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> <CAB9> ԭʼ ·<CABC> <C2B7>
result . Add ( outputFormat = = null ? dir : outputFormat ( dir ) ) ;
}
}
// <20> <> ȡ<EFBFBD> ļ<EFBFBD>
var files = Directory . GetFiles ( path ) ;
foreach ( var file in files )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> fileFilter <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> øú<C3B8> <C3BA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɸѡ
if ( fileFilter = = null | | fileFilter ( file ) )
{
if ( isDirectory = = null | | isDirectory = = false )
{
// <20> <> <EFBFBD> <EFBFBD> ָ<EFBFBD> <D6B8> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> ø<EFBFBD> ʽ <EFBFBD> <CABD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ʹ <EFBFBD> <CAB9> ԭʼ ·<CABC> <C2B7>
result . Add ( outputFormat = = null ? file : outputFormat ( file ) ) ;
}
}
}
return result ;
}
// <20> <> <EFBFBD> ɽ<EFBFBD> ͼ
public static PngBitmapEncoder CaptureWindowScreenshot ( Window window , string? filePath = null )
{
// <20> <> ȡ<EFBFBD> <C8A1> ǰ<EFBFBD> <C7B0> <EFBFBD> ڵ<EFBFBD> DPI <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
var dpiScale = VisualTreeHelper . GetDpi ( window ) ;
// <20> <> ȡ DPI <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӵ<EFBFBD> X <20> <> Y ֵ<> <D6B5> ͨ<EFBFBD> <CDA8> X <20> <> Y Ӧ<> <D3A6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ȵ<EFBFBD>
double dpiX = dpiScale . DpiScaleX ;
double dpiY = dpiScale . DpiScaleY ;
// <20> <> <EFBFBD> <EFBFBD> DPI <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ӵ<EFBFBD> <D3B5> <EFBFBD> <EFBFBD> <EFBFBD> ͼ<EFBFBD> ijߴ <C4B3>
int width = ( int ) ( window . ActualWidth * dpiX ) ; // ʹ <> <CAB9> dpiX
int height = ( int ) ( window . ActualHeight * dpiY ) ; // ʹ <> <CAB9> dpiY
// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> RenderTargetBitmap <20> <> <EFBFBD> <EFBFBD> <F3A3ACB2> ҿ<EFBFBD> <D2BF> <EFBFBD> DPI <20> <> <EFBFBD> <EFBFBD>
RenderTargetBitmap rtb = new RenderTargetBitmap ( width , height , 96 * dpiX , 96 * dpiY , System . Windows . Media . PixelFormats . Pbgra32 ) ;
// ʹ <> <CAB9> VisualTreeHelper <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ⱦ
rtb . Render ( window ) ;
// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> PngBitmapEncoder <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ PNG <20> <> ʽ
PngBitmapEncoder pngEncoder = new PngBitmapEncoder ( ) ;
pngEncoder . Frames . Add ( BitmapFrame . Create ( rtb ) ) ;
if ( ! string . IsNullOrEmpty ( filePath ) )
{
// <20> <> <EFBFBD> <EFBFBD> ͼ<EFBFBD> <CDBC> <EFBFBD> <EFBFBD> <EFBFBD> ļ<EFBFBD>
using ( FileStream fs = new FileStream ( filePath , FileMode . Create ) )
{
pngEncoder . Save ( fs ) ;
}
}
return pngEncoder ;
}
2025-12-13 16:33:41 +08:00
public static void ˢ <EFBFBD> <EFBFBD> <EFBFBD> ӱ <EFBFBD> ( string selectedFile , ComboBox combox )
{
try
{
// ʹ <> <CAB9> NPOI<4F> <49> <EFBFBD> <EFBFBD> Excel<65> ļ<EFBFBD>
using ( FileStream fileStream = new FileStream ( selectedFile , FileMode . Open , FileAccess . Read , FileShare . ReadWrite ) )
{
IWorkbook workbook = WorkbookFactory . Create ( fileStream ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
combox . Items . Clear ( ) ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> sheet<65> <74> <EFBFBD> <EFBFBD> <EFBFBD> ӵ<EFBFBD> <D3B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
for ( int i = 0 ; i < workbook . NumberOfSheets ; i + + )
{
string sheetName = workbook . GetSheetName ( i ) ;
combox . Items . Add ( sheetName ) ;
}
// <20> Զ<EFBFBD> ѡ <EFBFBD> <D1A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> sheet
if ( combox ! = null & & combox . Items ! = null & & combox . Items . Count > 0 )
{
combox . SelectedIndex = combox . Items . Count - 1 ;
}
}
}
catch ( Exception ex )
{
//DebugBar(Debugtag, $"<22> <> ȡExcel<65> ļ<EFBFBD> ʱ<EFBFBD> <CAB1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> {ex.Message}", <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ɫ);
}
}
2025-09-12 18:34:33 +08:00
#endregion
#region <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2025-11-18 19:54:13 +08:00
public static async Task < int > RunExternalCommand ( string? applicationPath = "explorer.exe" , string arguments = "" , bool UseShellExecute = false , bool CreateNoWindow = false )
2025-08-29 14:57:55 +08:00
{
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = applicationPath ,
Arguments = arguments ,
UseShellExecute = UseShellExecute ,
CreateNoWindow = CreateNoWindow
} ;
try
{
Process process = Process . Start ( startInfo ) ? ?
throw new InvalidOperationException ( "Failed to start process." ) ;
if ( UseShellExecute ! = true )
{
await Task . Run ( ( ) = > process . WaitForExit ( ) ) ;
return process . ExitCode ; // <20> <> <EFBFBD> ؽ<EFBFBD> <D8BD> ̵<EFBFBD> <CCB5> ˳<EFBFBD> <CBB3> <EFBFBD> <EFBFBD> <EFBFBD>
}
else
{
return 0 ;
}
}
2025-11-18 19:54:13 +08:00
catch ( Exception )
2025-08-29 14:57:55 +08:00
{
throw ;
}
}
public static async Task < string > RunExternalCommandResult ( string applicationPath , string arguments , bool CreateNoWindow = true )
{
// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> µĽ<C2B5> <C4BD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = applicationPath ,
Arguments = arguments ,
UseShellExecute = false , // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ false <20> Ա<EFBFBD> <D4B1> ܹ<EFBFBD> <DCB9> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
RedirectStandardOutput = true , // <20> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD>
RedirectStandardError = true , // <20> <> ѡ <EFBFBD> <D1A1> Ҳ<EFBFBD> ض<EFBFBD> <D8B6> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
CreateNoWindow = CreateNoWindow // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
} ;
try
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
using ( Process process = new Process ( ) )
{
process . StartInfo = startInfo ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
process . Start ( ) ;
// <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
string output = await process . StandardOutput . ReadToEndAsync ( ) ;
string error = await process . StandardError . ReadToEndAsync ( ) ; // <20> <> ȡ<EFBFBD> <C8A1> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
// <20> ȴ<EFBFBD> <C8B4> <EFBFBD> <EFBFBD> ̽<EFBFBD> <CCBD> <EFBFBD>
await process . WaitForExitAsync ( ) ;
/ *
if ( process . ExitCode ! = 0 )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ˳<EFBFBD> <CBB3> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
throw new Exception ( $"Process exited with code {process.ExitCode}: {error}" ) ;
}
* /
return output ; // <20> <> <EFBFBD> ر<EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
}
}
2025-11-18 19:54:13 +08:00
catch ( Exception )
2025-08-29 14:57:55 +08:00
{
// <20> <> <EFBFBD> Լ<EFBFBD> ¼<EFBFBD> <C2BC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> 쳣
throw ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ׳ <EFBFBD> <D7B3> 쳣<EFBFBD> Ա<EFBFBD> <D4B1> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ߲<EFBFBD> <DFB2> <EFBFBD>
}
}
2025-09-12 18:34:33 +08:00
#endregion
2025-08-29 14:57:55 +08:00
2025-10-29 17:07:22 +08:00
// ͨ<> <CDA8> UDP <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
2025-11-11 16:51:37 +08:00
public static void SendUdpMessage ( string _serverIp , int _serverPort , string message )
2025-10-29 17:07:22 +08:00
{
try
{
using ( UdpClient udpClient = new UdpClient ( ) )
{
2025-11-11 16:51:37 +08:00
IPEndPoint endPoint ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Զ<EFBFBD> ʶ<EFBFBD> <CAB6> <EFBFBD> <EFBFBD> <EFBFBD> ȳ<EFBFBD> <C8B3> Խ<EFBFBD> <D4BD> <EFBFBD> ΪIP<49> <50> ַ<EFBFBD> <D6B7> ʧ<EFBFBD> <CAA7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
if ( IPAddress . TryParse ( _serverIp , out IPAddress ipAddress ) )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> IP<49> <50> ַ
endPoint = new IPEndPoint ( ipAddress , _serverPort ) ;
}
else
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> DNS<4E> <53> <EFBFBD> <EFBFBD>
IPAddress [ ] addresses = Dns . GetHostAddresses ( _serverIp ) ;
if ( addresses . Length = = 0 )
{
Debug . WriteLine ( $"<22> <EFBFBD> <DEB7> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> : {_serverIp}" ) ;
return ;
}
endPoint = new IPEndPoint ( addresses [ 0 ] , _serverPort ) ;
}
2025-10-29 17:07:22 +08:00
byte [ ] messageBytes = Encoding . UTF8 . GetBytes ( message ) ;
udpClient . Send ( messageBytes , messageBytes . Length , endPoint ) ;
}
}
catch ( Exception ex )
{
Debug . WriteLine ( $"<22> <> <EFBFBD> <EFBFBD> UDP <20> <> Ϣʧ<CFA2> <CAA7> : {ex.Message}" ) ;
}
}
// ͨ<> <CDA8> TCP <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϣ
public static void SendTcpMessage ( string _serverIp , int _serverPort , string message )
{
try
{
using ( TcpClient tcpClient = new TcpClient ( _serverIp , _serverPort ) )
using ( NetworkStream networkStream = tcpClient . GetStream ( ) )
{
byte [ ] messageBytes = Encoding . UTF8 . GetBytes ( message ) ;
networkStream . Write ( messageBytes , 0 , messageBytes . Length ) ;
networkStream . Flush ( ) ;
}
}
catch ( Exception ex )
{
Debug . WriteLine ( $"<22> <> <EFBFBD> <EFBFBD> TCP <20> <> Ϣʧ<CFA2> <CAA7> : {ex.Message}" ) ;
}
}
2025-09-12 18:34:33 +08:00
// <20> <> <EFBFBD> ɹ<EFBFBD> ϣ
public static string GenerateHash ( string input )
{
// <20> <> <EFBFBD> <EFBFBD> SHA256<35> <36> <EFBFBD> <EFBFBD>
using ( SHA256 sha256 = SHA256 . Create ( ) )
{
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD> ת<EFBFBD> <D7AA> Ϊ<EFBFBD> ֽ<EFBFBD> <D6BD> <EFBFBD> <EFBFBD> 鲢<EFBFBD> <E9B2A2> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ϣֵ
byte [ ] bytes = sha256 . ComputeHash ( Encoding . UTF8 . GetBytes ( input ) ) ;
2025-08-29 14:57:55 +08:00
2025-09-12 18:34:33 +08:00
// <20> <> <EFBFBD> <EFBFBD> ϣֵת<D6B5> <D7AA> Ϊʮ<CEAA> <CAAE> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD>
StringBuilder stringBuilder = new StringBuilder ( ) ;
foreach ( byte b in bytes )
{
stringBuilder . Append ( b . ToString ( "x2" ) ) ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> յĹ<D5B5> ϣ<EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD>
return stringBuilder . ToString ( ) ;
}
}
2025-11-21 18:17:02 +08:00
/// <summary>
/// <20> <> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> µ<EFBFBD> UUID/GUID
/// </summary>
/// <returns><3E> <> ʽ Ϊ: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx</returns>
public static string GenerateUUID ( )
{
return Guid . NewGuid ( ) . ToString ( ) ;
}
2025-08-29 14:57:55 +08:00
}
}