Reputation: 17
I figured how to create a directory in c# wpf
. But i do not know how to do it to the current drive folder. Where current drive is the drive windows installed. I used:
Code UPDATED
String cur = Environment.CurrentDirectory;
cur = cur.Substring(0, 2);
string path1 = @""+cur+"\temp";
if(!Directory.Exists(path1))
Directory.CreateDirectory(path1);
But it is giving error saying invalid characters in path. How can i create a folder to another drive?
Thanks!
Upvotes: 1
Views: 1666
Reputation: 66439
I'd use the methods available in System.IO.Path
. They handle the directory separator for you.
Use Path.GetPathRoot
to get the root drive (i.e. c:\\
)
var root = Path.GetPathRoot(Environment.CurrentDirectory);
Use Path.Combine
to combine two paths into a single directory path:
var temp = Path.Combine(root, "temp");
If all you need is a place to store temporary files, you could consider using:
Path.GetTempPath()
Upvotes: 2