Link to home
Create AccountLog in
Avatar of jackjohnson44
jackjohnson44

asked on

c# can you write to a file if the path you give does not exist?

Is there a way to write to a file, creating the path if it does not exist?

say I want to write to c:\program files\mydir\mydir2\test.txt

1.  is there a way to just specify that path and have the code create it if it doesn't exist?
2. if not, how can I create the path, but only if it does not exist?
Avatar of gdupadhyay
gdupadhyay
Flag of United States of America image

You will find all file operation you looking for in this link.

http://www.geekpedia.com/tutorial109_File-operations-IorO.html
 
FileStream fs = new FileStream("file and pathname here", FileMode.OpenOrCreate, FileAccess.ReadWrite);
 
Some More link:
http://www.csharphelp.com/archives/archive24.html
http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=400
I hope you will find good stuff for file operation in above three link. Please let me know if you have any question regarding these link.

Thanks
Ganesh
Avatar of jackjohnson44
jackjohnson44

ASKER

Thanks guys, but I don't think I made myself clear.  The path does not exist and I want to create it without having to do it manually

this works
FileStream fs = new FileStream(@"c:\test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

this does not, and it is what I am trying to do
FileStream fs = new FileStream(@"c:\thisDirDoesNotExistYet\MeEither\test.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);
ASKER CERTIFIED SOLUTION
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
SOLUTION
Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
For more information about directory operation, you can find very good stuff here

http://www.aspfree.com/c/a/C-Sharp/A-Look-at-C-Sharp-Directory-and-DirectoryInfo-Classes/