Coloryr / ColorMC

全新的Minecraft PC启动器
Apache License 2.0
177 stars 19 forks source link

删除被占用的文件时崩溃 #24

Closed MorningMC closed 9 hours ago

MorningMC commented 12 hours ago

崩溃日志:

Version:A32.20241109
System:Os:Windows Arch:x86_64
SystemName:Microsoft Windows 10.0.22631
Gui Crash
System.IO.IOException: The process cannot access the file 'C:\Program Files\colormc-windows-x64-aot\minecraft\instances\1.20.2 - Fabric\.minecraft\mods\Essential Dependencies.jar' because it is being used by another process.
   at System.IO.FileSystem.DeleteFile(String fullPath)
   at ColorMC.Gui.UIBinding.GameBinding.DeleteMod(ModObj mod) in E:\code\ColorMC\src\ColorMC.Gui\UIBinding\GameBinding.cs:line 883
   at ColorMC.Gui.UI.Model.GameEdit.GameEditModel.DeleteMod(ModDisplayModel item) in E:\code\ColorMC\src\ColorMC.Gui\UI\Model\GameEdit\GameEditTab4Model.cs:line 285
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
   at Avalonia.Threading.SendOrPostCallbackDispatcherOperation.InvokeCore()
   at Avalonia.Threading.DispatcherOperation.Execute()
   at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job)
   at Avalonia.Threading.Dispatcher.ExecuteJobsCore(Boolean fromExplicitBackgroundProcessingCallback)
   at Avalonia.Threading.Dispatcher.Signaled()
   at Avalonia.Win32.Win32Platform.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam)
   at Avalonia.Win32.Interop.UnmanagedMethods.DispatchMessage(MSG& lpmsg)
   at Avalonia.Win32.Win32DispatcherImpl.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)
   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.Start(String[] args)
   at ColorMC.Gui.ColorMCGui.Main(String[] args) in E:\code\ColorMC\src\ColorMC.Gui\ColorMCGui.cs:line 100

修复建议:加一个catch

MorningMC commented 12 hours ago

建议:

public static void DeleteMod(ModObj mod)
{
    try {
        string name = new FileInfo(mod.Local).Name;
        foreach (var item in mod.Game.Mods)
        {
            if (item.Value.File == name)
            {
                mod.Game.Mods.Remove(item.Key);
                mod.Game.SaveModInfo();
                break;
            }
        }
        mod.Delete();
    } catch (Exception e) {
        // do sth like showing a dialog
    }
}

本人不会C#,我是按Java写的