1

I've been writing a simple console application as a part of exercise in project. Tasks are rather straightforward:

2nd method has to create nested directory tree where every folder name is Guid.

3rd method has to put empty file in chosen directory tree at specific level.

My main problem lies within 3rd method. Because while it works fine and creates file 'till third level of any directory, beyond that point it always throw "System.IO.DirectoryNotFoundException" - as it "can't find part of the path".

I use string as a container for path, but since it's few Guid set together it gets pretty long. I had similar problem with creating directory, but in order to work I had to simply put @"\?\" prefix behind the path. So is there any way to make it work, or maybe get around that?

Here are method that fails. Specifically it's

File.Create(PathToFile + @"\blank.txt").Dispose();

And part of code which makes string and invokes it:

string ChosenDirectoryPath = currDir.FullName + @"\";

for (int i = 0; i <= Position; i++)
{
   ChosenDirectoryPath += ListsList[WhichList][i];
}
if (!File.Exists(ChosenDirectoryPath + @"\blank.txt"))
{
   FileMaker(ref ChosenDirectoryPath);
}

Edit:

To be specific, directories are made by method:

public List<string> DirectoryList = new List<string>();
internal static List<List<string>> ListsList = new List<List<string>>();
private static DirectoryInfo currDir = new DirectoryInfo(".");
private string FolderName;
private static string DirectoryPath;

public void DeepDive(List<string> DirectoryList, int countdown)
{
   FolderName = GuidMaker();
   DirectoryList.Add(FolderName + @"\");

   if (countdown <= 1)
   {
      foreach (string element in DirectoryList)
      {
         DirectoryPath += element;
      }

      Directory.CreateDirectory(@"\\?\" + currDir.FullName + @"\" + DirectoryPath);
      Console.WriteLine("Folders were nested at directory {0} under folder {1}\n", currDir.FullName, DirectoryList[0]);

      ListsList.Add(DirectoryList);
      DirectoryPath = null;

      return;
   }
   DeepDive(DirectoryList, countdown-1);
}

Which is pretty messy because of recursion (iteration would be better but i wanted to do it this way to learn something). The point is that directories are made and stored in list of lists.

Creating files works properly but only for the first three nested folders. So the problem is that it is somehow loosing it's path to file in 4th and 5th level, and can't even make those manually. Could it be too long path? And how to fix this.

Here is exception that throws out:

System.IO.DirectoryNotFoundException: „Can't find part of the path „C:\Some\More\Folders\1b0c7715-ee01-4df8-9079-82ea7990030f\c6c806b0-b69d-4a3a-88d0-1bd8a0e31eb2\9671f2b3-3041-42d5-b631-4719d36c2ac5\6406f00f-7750-4b5a-a45d-cebcecb0b70e\bcacef2b-e391-4799-b84e-f2bc55605d40\blank.txt”.”

So it throws full path to file and yet says that it can't find it.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Raczy
  • 13
  • 5
  • 1
    currDir.FullName + @"\" + @"\blank.txt" wouldnt that give you two \ on your path?. You shoud be using Path.Combine(path1,path2) to go aroudn merging paths and the "\" that can be duplicated if both paths contain the "\" – npo Dec 30 '18 at 20:48
  • Thanks for pointing out, as soon as i figure out how to use it i'll try to, but for now I'm concerned more about the thing that the way I implemented it works, but only for directories which are not deeper than 3rd folder. – Raczy Dec 30 '18 at 22:50

1 Answers1

4

You problem is that File.Create doesn't create the corresponding directories for you, instead it throws a System.IO.DirectoryNotFoundException.

You have to create those directories yourself, by using System.IO.Directory.CreateDirectory()


If this exception occurs because of a too long path, you can still use the "long path syntax (\\?\)" like you did when creating your directories.

See also this question: How to deal with files with a name longer than 259 characters? there is also a good article linked

nosale
  • 808
  • 6
  • 14
  • Thank you for answer, yet these directories are created earlier by another method which i didn't post here. This method uses `System.IO.Directory.CreateDirectory()` as you advise, and I actually can create some files, but only in directories which are not nested deeper than at 3rd level. While I get Exception it shows me accurate path to the place where I want this file to be made, except that it doesn't create it. Even as i give path manually it still throws that error. – Raczy Dec 30 '18 at 22:48
  • @Raczy Thats weird cause the path you mentioned doesn't exceed any limit. I tried `File.Create` with the exact same path and everything works fine for me. Are you 100% sure that all the directories exists? – nosale Dec 31 '18 at 14:41
  • Once again I carefully looked over whole path and everything seemed fine.I decided to do it manually, creating every file step by step. So again for the first three nested files everything worked fine. Was doing it via copy-paste and checking it with file explorer to be sure that path is the same, and again the same problem took place. Exception for not complete path is thrown at the 4th and 5th nested directory (VS is in admin mode). I think I'll just pass it. Maybe it's just my personal problem. Yet it seems pretty strange. Anyway thanks for the efford – Raczy Dec 31 '18 at 18:42
  • @Raczy I looked into your github repository and found your source code there, based on that I have edit my answer, which hopefully helps you – nosale Dec 31 '18 at 20:04
  • That's a real hero right there, thank you very much for your efford! – Raczy Dec 31 '18 at 20:26
  • @Raczy Oh my, this could have been so much faster cause I thought you have already tried that^^ – nosale Dec 31 '18 at 21:26
  • I tried, that was actually one of the first solutions I figured out, but somehow it couldn't compile. Maybe I did It in another place or in the wrong manner. But i really though it couldn't be done this way. As it turns out, problem was trivial and I overlooked simplest solution. – Raczy Dec 31 '18 at 22:53