user3262659
user3262659

Reputation: 17

Create a folder in Documents C# WPF

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

Answers (1)

Grant Winney
Grant Winney

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

Related Questions