@@ -1190,23 +1190,32 @@ private void LinkSingleFile(string assignedDriveLetter, string sourceName, strin
11901190 string guestRepo = Path . Combine ( assignedDriveLetter , "Windows" , "System32" , "HostDriverStore" , "FileRepository" ) ;
11911191 string hostDestDir = Path . Combine ( assignedDriveLetter , "Windows" , targetSubDir ) ;
11921192
1193- // 1. 如果目标子目录不存在,先创建它
11941193 if ( ! Directory . Exists ( hostDestDir ) )
11951194 {
11961195 Directory . CreateDirectory ( hostDestDir ) ;
11971196 }
1198-
1199- // --- 新增:针对 SyChpe32 等系统目录的脱权处理 ---
1200- // 只有当路径包含 SyChpe32 或 System32 时才触发,避免大范围修改权限
1201- if ( targetSubDir . Contains ( "SyChpe32" , StringComparison . OrdinalIgnoreCase ) ||
1197+ if ( targetSubDir . Equals ( "System32" , StringComparison . OrdinalIgnoreCase ) ||
1198+ targetSubDir . Contains ( "SyChpe32" , StringComparison . OrdinalIgnoreCase ) ||
12021199 targetSubDir . Contains ( "SysWOW64" , StringComparison . OrdinalIgnoreCase ) )
12031200 {
1204- // 获取所有权 (Administrators 组)
12051201 ExecuteCommand ( $ "cmd /c takeown /f \" { hostDestDir } \" /a") ;
1206- // 授予完全控制权限 (WELL-KNOWN SID S-1-5-32-544 代表管理员组)
12071202 ExecuteCommand ( $ "cmd /c icacls \" { hostDestDir } \" /grant *S-1-5-32-544:F") ;
12081203 }
12091204
1205+ string hostLinkPath = Path . Combine ( hostDestDir , targetName ) ;
1206+ if ( System . IO . File . Exists ( hostLinkPath ) || System . IO . Directory . Exists ( hostLinkPath ) )
1207+ {
1208+ return ;
1209+ }
1210+ try
1211+ {
1212+ if ( System . IO . File . GetAttributes ( hostLinkPath ) != ( System . IO . FileAttributes ) ( - 1 ) )
1213+ {
1214+ ExecuteCommand ( $ "cmd /c del /f /q \" { hostLinkPath } \" ") ;
1215+ }
1216+ }
1217+ catch { }
1218+
12101219 var foundFiles = new DirectoryInfo ( guestRepo )
12111220 . GetFiles ( sourceName , SearchOption . AllDirectories )
12121221 . OrderByDescending ( f => f . LastWriteTime )
@@ -1216,17 +1225,7 @@ private void LinkSingleFile(string assignedDriveLetter, string sourceName, strin
12161225
12171226 string hostSourceFile = foundFiles [ 0 ] . FullName ;
12181227 string guestInternalTarget = hostSourceFile . Replace ( assignedDriveLetter , "C:" ) ;
1219- string hostLinkPath = Path . Combine ( hostDestDir , targetName ) ;
1220-
1221- // --- 新增:针对单个目标文件的脱权(如果文件已存在) ---
1222- if ( File . Exists ( hostLinkPath ) )
1223- {
1224- ExecuteCommand ( $ "cmd /c takeown /f \" { hostLinkPath } \" /a") ;
1225- ExecuteCommand ( $ "cmd /c icacls \" { hostLinkPath } \" /grant *S-1-5-32-544:F") ;
1226- File . Delete ( hostLinkPath ) ;
1227- }
12281228
1229- // 执行 mklink
12301229 ExecuteCommand ( $ "cmd /c mklink \" { hostLinkPath } \" \" { guestInternalTarget } \" ") ;
12311230 }
12321231 catch ( Exception ex )
0 commit comments