Reputation: 2808
Is this file path valid?:
'C:sample.txt'
(this string is input from a user)
It is confusing since there's no error returned. I've used Directory.Exists(path.DirectoryName). But I cannot find the file created in C:.
Upvotes: 0
Views: 569
Reputation: 4933
path must be as follows,
string filePath=args[0]
if (!File.Exists(filePath))
{
File.Create(filePath);
}
Upvotes: 0
Reputation: 1679
it is created in your current directory
string filepath = "c:sample.txt";
StreamWriter sw = File.CreateText(filepath);
sw.WriteLine("hello");
sw.Close();
string s = Directory.GetCurrentDirectory();
Console.WriteLine(s);
Upvotes: 4
Reputation: 992787
Yes, C:sample.txt
is a valid file path, and refers to whatever the process current directory for drive C:
is (since no specific directory is provided in the path).
Upvotes: 2
Reputation: 2085
It indeed is a corret path, Windows doesnt bother with a \.
Also try and use File.Exists().
Upvotes: 1