'Error: Error finishing download: System.UnauthorizedAccessException: Access to the path ' ' is denied. Hi, I need help. I keep getting this Error. C#

I need help with this error:

Error finishing download: System.UnauthorizedAccessException: Access to the path 'M: Applications VisualStudio 2019 Repos GameLauncherForNoSymatry GameLauncher GameLauncher bin\Debug\Build' is denied. at System.IO.__Error. WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalDelete(String path, Boolean checkHost) at System.IO.File.Delete(String path) at GameLauncher. MainWindow.DownloadGameCompletedCallback(Objec t sender, AsyncCompletedEventArgs e) in M: Applications VisualStudio 2019 Repos GameLauncherForNoSymatry GameLauncher GameLauncher MainWindow.xam.cs:line 133<

I'm trying to make a game launcher using Microsoft WPF but I keep getting the ERROR above when trying to delete the previously installed version of the game.

These are the important parts of my code:

public MainWindow()
    {
        InitializeComponent();

        rootPath = Directory.GetCurrentDirectory();
        versionFile = Path.Combine(rootPath, "Version.txt");
        gameZip = Path.Combine(rootPath, "Build.zip");
        gameExe = Path.Combine(rootPath, "Build", "Asymetric Multyplayer.exe");
        build = Path.Combine(rootPath, "Build");
    }

private void DownloadGameCompletedCallback(object sender, AsyncCompletedEventArgs e)
    {
        try
        {
            string onlineVersion = ((Version)e.UserState).ToString();
            if (Directory.Exists(build))
            {
                File.Delete(build);
                MessageBox.Show($"File Deleted!");

            }
            ZipFile.ExtractToDirectory(gameZip, rootPath);
            File.Delete(gameZip);

            File.WriteAllText(versionFile, onlineVersion);

            VersionText.Text = onlineVersion;
            Status = LauncherStatus.ready;
        }
        catch (Exception ex)
        {
            Status = LauncherStatus.failed;
            MessageBox.Show($"Error finishing download: {ex}");
        }
    }

The problem I had originally was that when I was extracting the zip download after the game had already been installed once the ERROR below would pop up.

Error finishing download: System.IO.IOException: The file "M: Applications VisualStudio2019 Repos GameLauncherForNoSymatry GameLauncher GameLauncher bin Debug Build Asymetric Multyplayer.exe' already exists. at System.IO._Error. WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, File Access access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFrom Proxy, Boolean useLongPath, Boolean checkHost) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share) at System.Io.Compression.ZipFileExtensions.ExtractToFile(ZipArchiveEntry source, String destination FileName, Boolean overwrite) at System.Io.Compression.ZipFileExtensions.ExtractToDirectory(Zip Archive source, String destination DirectoryName) at System.IO. Compression.ZipFile.ExtractToDirectory(String source ArchiveFileName, String destination DirectoryName, Encoding entryNameEncoding) at System.IO.Compression.Zip File.Extract To Directory(String source ArchiveFileName, String destination DirectoryName) GameLauncher.MainWindow.DownloadGameCompletedCallback(Objec t sender, AsyncCompletedEventArgs e) in M: Applications VisualStudio 2019 Repos\GameLauncherForNoSymatry GameLauncher GameLauncher MainWindow.xaml.cs:line 132<

This used to have a simple fix where all one had to do was add a simple bool at the end as seen below:

ZipFile.ExtractToDirectory(FileSource, FileDestination, DoOverWritePreviousFile);

But now they have seemed to have removed that bool and that's the real problem I'm having. But I've been trying for the past few days to make a workaround but failed constantly the latest one is the one you are seeing here where I'm getting this ERROR.

If you have any further questions or hopefully answers I would love to hear them!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source