Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net FileStream Create throws error if file doesn't exist

I have a file path that might exist or might not exist.

I want to create / override the file, and i have this code:

string filePath = GetFilePath();
using (FileStream file = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
{
    xDoc.Save(file);
}

When i call using (FileStream file ...) and the file doesn't exist, it throws an Could not find a part of the path... error.

I am doing something wrong? shouldn't it create the file if doesn't exist?

like image 451
Catalin Avatar asked Oct 23 '25 04:10

Catalin


1 Answers

FileStream can't create intermediate directories that don't exist. This question should help you.

like image 77
davogones Avatar answered Oct 24 '25 20:10

davogones



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!