Cannot Modify Files on Desktop and Some Other Directories Through C#

MT 1 Reputation point
2022-05-25T21:27:22.617+00:00

I have a weird issue with my computer.

I cannot use my C# apps to rename or delete files on my desktop and in other specific directories on my computer.
There's no real pattern and the information is below:

using System;
using System.IO;

namespace justATest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            string path = @"C:\Users\USERNAME\Desktop\";
            string nameBefore = Path.Combine(path, "test.txt");
            string nameAfter = Path.Combine(path, "something.txt");

            File.Move(nameBefore, nameAfter); // Fails
        }
    }
}

The exception:

System.UnauthorizedAccessException
  HResult=0x80070005
  Message=Access to the path is denied.
  Source=System.IO.FileSystem
  StackTrace:
   at System.IO.FileSystem.MoveFile(String sourceFullPath, String destFullPath, Boolean overwrite)
   at System.IO.File.Move(String sourceFileName, String destFileName, Boolean overwrite)
   at System.IO.File.Move(String sourceFileName, String destFileName)
   at justATest.Program.Main(String[] args) in C:\Users\USERNAME\source\repos\justATest\justATest\Program.cs:line 16

What I tried:

  1. I updated Windows to the latest version
  2. I checked the file's owner
  3. I checked the file's permissions
  4. I ran my app as an administrator
  5. I ran Visual Studio as an administrator
  6. I ran SFC /ScanNow
  7. I did some kind of a repair to my Windows installation (through Microsoft Support)
  8. I created multiple files and tried it on old and new files
  9. I created nested folders and tried to rename files within said folders 10. I tried renaming files in other directories (that works) 11. I tried the method of copying the file into a new file (with the new name) and then deleting the old file. This works until I try to delete the old file, which fails 12. I tried doing the same tasks manually and it works just fine 13. I tried turning off my anti virus 14. I tried to repair Visual Studio

I mainly want to rename files, but the same goes for deleting files and the like.

OS: Windows 10 Pro 21H2
IDE: Visual Studio 2019 16.11.15
Number of users on machine: 1 (just me)

If you need any more information, let me know.

P.S.: I was sent here after the Microsoft chat support could not solve my issue through various methods (they even made me repair my Windows installation).
I'm honestly clueless.

Windows 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,623 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,946 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 54,401 Reputation points
    2022-05-25T21:45:59.977+00:00

    Off the top of my head I'd wonder about anti-virus software. At least for the desktop, dropping a file onto it will trigger an AV scan every time the desktop is touched. Dropping files onto the desktop is known to kill performance. If this happens only with the desktop then that is where I'd place the blame. This would be especially true if it might work one time but fail the next. The larger the file the more likely it is to fail.

    Taking a step back I'd say you shouldn't be hard coding the desktop directory in your app as it is not a fixed location. There are methods to get the current user's desktop directory.


  2. Limitless Technology 44,351 Reputation points
    2022-05-31T07:21:07.823+00:00

    Hi MT-2769,

    Please take a look through the following article about System.IO:

    https://learn.microsoft.com/en-us/dotnet/api/system.io?view=net-6.0

    There may be some C# prerequisites that you're missing from your system:

    The following options are for dotnet by itself. For example, dotnet −−info. They print out information about the environment if not installed it will throw error.

    −−info

    Prints out detailed information about a .NET Core installation and the machine environment, such as the current operating system, and commit SHA of the .NET Core version.

    −−version

    Prints out the version of the .NET Core SDK in use.

    −−list−runtimes

    Prints out a list of the installed .NET Core runtimes. An x86 version of the SDK lists only x86 runtimes, and an x64 version of the SDK lists only x64 runtimes.

    −−list−−sdks

    Prints out a list of the installed .NET Core SDKs.

    −h|−−help

    Prints out a list of available commands.

    ----------------------------------------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.