0

I have an api (c#) that parses a project files and gives me an array of the files it has to compile. I am just trying to create files in my custom directory similar to the csharp project file's folder structure...

var csProjDir = Path.GetDirectoryName(csProjFile);
var files = api.GetFiles(csProjFile);
var currentDir = Path.GetFullPath(".");
foreach(string f in files)
{
    var content = api.generateContent(Path.Combine(csProjDir, f));
    File.WriteText(Path.Combine(currentDir, f), content); // throws error
}

Typically files array will have values as follows:

fldr1\Class1.cs
fldr2\Class1.cs
fldr3\foo\Class1.cs
fldr3\foo\Class2.cs

So Path.Combine(currentDir, f) refers to something that doesn't exist in the file system yet, because, say, the curretDir doesn't have folder fldr3 in its path. It does not exist.

But is there some library that appropriately writes to the file system post creating the correct folder tree (if it does not exist), or otherwise?

1

The best way is check first if directory exist and if not create. After this write file

if(!Directory.Exists(path))
    {
        Directory.CreateDirectory(path);
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.