0

I want to make a simple code that moves specific files from "Downloads" to another directory. If i run this program it should move this specific pdf-file to "PDFs"-directory:

"Downloads"-directory

Here is the code:


 public const string path = @"C:\Users\eduar\Downloads\";
    public const string newDic = @"C:\Users\eduar\Downloads\PDFs";

    public static void Main()
    {
        if (!Directory.Exists(newDic))
        {
            Directory.CreateDirectory(newDic);
        }

        Greetings();

        Console.ReadKey();
    }

    public static void Greetings()
    {
        Console.WriteLine("Hello, please press any key to sort all files in Downloads dictionary  \n");
        Console.ReadKey();

        var files = Directory.GetFiles(path);

        foreach (var file in files)

            if (Path.GetExtension(file) == ".pdf")
            {
                try
                {
                    File.Move(file, newDic, true);
                }
                catch (Exception ex)
                {

                    Console.WriteLine(ex.ToString());
                }
            }
    }

Here is the ERROR-code:

enter image description here

PS: im new to Stackoverflow didnt notice that part. The whole question is above.

frankM_DN
  • 365
  • 7
ENKO
  • 1
  • 2
  • I closed this is a duplicate of following question, since it seems to be the same issue and cause: https://stackoverflow.com/questions/46894806/access-to-path-is-denied-file-move-fails-but-file-delete-works So you need to add the full file-path not only the directory `newDic`. – Tim Schmelter Oct 28 '22 at 14:15

0 Answers0