locked
Directory does exist but getting DirectoryNotFoundException: Could not find a part of the path RRS feed

  • Question

  • User-220572584 posted

    Im trying to reach a file on my server it does exist but when i try to copy it it returns DirectoryNotFoundException: Could not find a part of the path

    the funny part is this actually 

    that I tried to check the file if does that exist by code like

       if (!System.IO.Directory.Exists(itempath))
                            System.IO.Directory.CreateDirectory(itempath);

    and it says that no such file exists and goes in the body but when triying to execute the create line it returns  System.IO.IOException: 'Cannot create 'F:\PersianGems\Book\Book\wwwroot\Logs\LF_20210130.txt' because a file or directory with the same name already exists.'

    any thoughts please?

    Friday, January 29, 2021 11:53 PM

Answers

  • User753101303 posted

    Hi,

    Seems a confusion between the full file name and the directory name.

    The directory is not found (as you are testinfgthe full name of a file) and tstill you can't create a directory named this way as you already have a F:\PersianGems\Book\Book\wwwroot\Logs\LF_20210130.txt file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 30, 2021 12:56 PM

All replies

  • User-1545767719 posted

    the funny part is this actually 

    that I tried to check the file if does that exist by code like

       if (!System.IO.Directory.Exists(itempath))
                            System.IO.Directory.CreateDirectory(itempath);

    and it says that no such file exists and goes in the body but when triying to execute the create line it returns  System.IO.IOException: 'Cannot create 'F:\PersianGems\Book\Book\wwwroot\Logs\LF_20210130.txt' because a file or directory with the same name already exists.'

    IOException means "The directory specified by path is a file." or "The network name is not known." See the following Microsoft document for details.

    https://docs.microsoft.com/en-us/dotnet/api/system.io.directory.createdirectory?view=netframework-4.8

    I suggest that you change:

    System.IO.Directory.CreateDirectory(itempath);

    to

    System.IO.Directory.CreateDirectory(Path.GetDirectoryName(itempath));

    Saturday, January 30, 2021 12:39 AM
  • User-220572584 posted

    Creating the file is not my problem accessing it and reading it is my problem please read the question carefully! thanks for your reply anyways

    Saturday, January 30, 2021 6:28 AM
  • User-1545767719 posted

    Please write your problem ONLY in detail with the code you are using to access and read the file, physical path to the file and the access right of worker process to the directory and file. 

    Saturday, January 30, 2021 9:03 AM
  • User753101303 posted

    Hi,

    Seems a confusion between the full file name and the directory name.

    The directory is not found (as you are testinfgthe full name of a file) and tstill you can't create a directory named this way as you already have a F:\PersianGems\Book\Book\wwwroot\Logs\LF_20210130.txt file.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 30, 2021 12:56 PM