Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deletion of unmanaged files #3865

Merged
merged 1 commit into from
Jul 24, 2023

Conversation

HebaruSan
Copy link
Member

Problem

If you open the Unmanaged Files tab from #3833 and try to delete a file, an exception is thrown:

System.IO.IOException: The directory name is invalid.

   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileSystemEnumerableIterator`1.CommonInit()
   at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
   at System.IO.Directory.EnumerateFileSystemEntries(String path, String searchPattern, SearchOption searchOption)
   at CKAN.GameInstance.HasManagedFiles(Registry registry, String absPath)
   at CKAN.GUI.UnmanagedFiles.DeleteButton_Click(Object sender, EventArgs e)
   at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
   at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
   at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
   at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ToolStrip.WndProc(Message& m)
   at System.Windows.Forms.MenuStrip.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

(Deleting a directory works fine.)

Cause

GameInstance.HasManagedFiles is passing its parameter to Directory.EnumerateFileSystemEntries even if it's a file. Directory.EnumerateFileSystemEntries hates that.

Changes

  • Now GameInstance.HasManagedFiles only calls Directory.EnumerateFileSystemEntries if the given path exists and is a directory.
  • While fixing this I realized that File.Delete and Directory.Delete might throw exceptions if the user doesn't have access to the file. Now these will be caught and displayed.

@HebaruSan HebaruSan added Bug Something is not working as intended Easy This is easy to fix GUI Issues affecting the interactive GUI Core (ckan.dll) Issues affecting the core part of CKAN labels Jul 22, 2023
@HebaruSan HebaruSan requested a review from techman83 July 22, 2023 16:57
Copy link
Member

@techman83 techman83 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great, this gave me a right chuckle!

GameInstance.HasManagedFiles is passing its parameter to Directory.EnumerateFileSystemEntries even if it's a file. Directory.EnumerateFileSystemEntries hates that.

@HebaruSan HebaruSan merged commit 812119b into KSP-CKAN:master Jul 24, 2023
10 checks passed
@HebaruSan HebaruSan deleted the fix/del-unmanaged-file branch July 24, 2023 06:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something is not working as intended Core (ckan.dll) Issues affecting the core part of CKAN Easy This is easy to fix GUI Issues affecting the interactive GUI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants