@@ -105,66 +105,72 @@ public static Task<bool> MoveToTrashAsync(string dir)
105105 {
106106 try
107107 {
108- if ( SystemInfo . Os == OsType . Windows )
108+ switch ( SystemInfo . Os )
109109 {
110- return Win32 . MoveToTrash ( dir ) ;
111- }
112- else if ( SystemInfo . Os == OsType . Linux )
113- {
114- string trashFilesPath = GetTrashFilesPath ( ) ;
115- string trashInfoPath = GetTrashInfoPath ( ) ;
116-
117- string fileName = Path . GetFileName ( dir ) ;
118- string destPath = Path . Combine ( trashFilesPath , fileName ) ;
119- string trashInfoFile = Path . Combine ( trashInfoPath , fileName + ".trashinfo" ) ;
120-
121- // 处理文件名冲突
122- int counter = 1 ;
123- while ( File . Exists ( destPath ) || Directory . Exists ( destPath ) )
110+ case OsType . Windows :
111+ return Win32 . MoveToTrash ( dir ) ;
112+ case OsType . Linux :
124113 {
125- string nameWithoutExt = Path . GetFileNameWithoutExtension ( fileName ) ;
126- string ext = Path . GetExtension ( fileName ) ;
127- destPath = Path . Combine ( trashFilesPath , $ "{ nameWithoutExt } _{ counter } { ext } ") ;
128- trashInfoFile = Path . Combine ( trashInfoPath , $ "{ nameWithoutExt } _{ counter } { ext } .trashinfo") ;
129- counter ++ ;
114+ string trashFilesPath = GetTrashFilesPath ( ) ;
115+ string trashInfoPath = GetTrashInfoPath ( ) ;
116+
117+ string fileName = Path . GetFileName ( dir ) ;
118+ string destPath = Path . Combine ( trashFilesPath , fileName ) ;
119+ string trashInfoFile = Path . Combine ( trashInfoPath , fileName + ".trashinfo" ) ;
120+
121+ // 处理文件名冲突
122+ int counter = 1 ;
123+ while ( File . Exists ( destPath ) || Directory . Exists ( destPath ) )
124+ {
125+ string nameWithoutExt = Path . GetFileNameWithoutExtension ( fileName ) ;
126+ string ext = Path . GetExtension ( fileName ) ;
127+ destPath = Path . Combine ( trashFilesPath , $ "{ nameWithoutExt } _{ counter } { ext } ") ;
128+ trashInfoFile = Path . Combine ( trashInfoPath , $ "{ nameWithoutExt } _{ counter } { ext } .trashinfo") ;
129+ counter ++ ;
130+ }
131+
132+ // 创建 .trashinfo 文件
133+ string trashInfoContent = GenerateTrashInfoContent ( dir , DateTime . Now ) ;
134+ File . WriteAllText ( trashInfoFile , trashInfoContent ) ;
135+
136+ if ( File . Exists ( dir ) )
137+ {
138+ File . Move ( dir , destPath ) ;
139+ }
140+ else if ( Directory . Exists ( dir ) )
141+ {
142+ Directory . Move ( dir , destPath ) ;
143+ }
144+
145+ return true ;
130146 }
131-
132- // 创建 .trashinfo 文件
133- string trashInfoContent = GenerateTrashInfoContent ( dir , DateTime . Now ) ;
134- File . WriteAllText ( trashInfoFile , trashInfoContent ) ;
135-
136- if ( File . Exists ( dir ) )
147+ case OsType . MacOs :
137148 {
138- File . Move ( dir , destPath ) ;
149+ dir = dir . Replace ( "\\ " , @"\\" )
150+ . Replace ( "\" " , "\\ \" " )
151+ . Replace ( "\n " , "\\ n" )
152+ . Replace ( "\r " , "\\ r" )
153+ . Replace ( "\t " , "\\ t" ) ;
154+
155+ // AppleScript命令
156+ string appleScriptCommand = $ "tell application \" Finder\" to delete POSIX file \" { dir } \" ";
157+
158+ // 使用bash执行AppleScript
159+ var processInfo = new ProcessStartInfo ( "osascript" , [ "-e" , appleScriptCommand ] )
160+ {
161+ RedirectStandardOutput = true ,
162+ RedirectStandardError = true ,
163+ CreateNoWindow = true
164+ } ;
165+
166+ using var process = new Process { StartInfo = processInfo } ;
167+ process . Start ( ) ;
168+ string output = process . StandardOutput . ReadToEnd ( ) ;
169+ string error = process . StandardError . ReadToEnd ( ) ;
170+ process . WaitForExit ( ) ;
171+
172+ return process . ExitCode == 0 ;
139173 }
140- else if ( Directory . Exists ( dir ) )
141- {
142- Directory . Move ( dir , destPath ) ;
143- }
144-
145- return true ;
146- }
147- else if ( SystemInfo . Os == OsType . MacOS )
148- {
149- // AppleScript命令
150- string appleScriptCommand = $ "tell application \" Finder\" to delete POSIX file \" { dir } \" ";
151-
152- // 使用bash执行AppleScript
153- var processInfo = new ProcessStartInfo ( "bash" , $ "-c \" osascript -e '{ appleScriptCommand } '\" ")
154- {
155- RedirectStandardOutput = true ,
156- RedirectStandardError = true ,
157- UseShellExecute = false ,
158- CreateNoWindow = true
159- } ;
160-
161- using var process = new Process { StartInfo = processInfo } ;
162- process . Start ( ) ;
163- string output = process . StandardOutput . ReadToEnd ( ) ;
164- string error = process . StandardError . ReadToEnd ( ) ;
165- process . WaitForExit ( ) ;
166-
167- return process . ExitCode == 0 ;
168174 }
169175 }
170176 catch
0 commit comments