CMA
CMA

Reputation: 2808

valid file path

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

Answers (5)

DevT
DevT

Reputation: 4933

path must be as follows,

       string filePath=args[0]
        if (!File.Exists(filePath))
        {
            File.Create(filePath);
        }

Upvotes: 0

DayDayHappy
DayDayHappy

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

Greg Hewgill
Greg Hewgill

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

Abhinav
Abhinav

Reputation: 2085

It indeed is a corret path, Windows doesnt bother with a \.

Also try and use File.Exists().

Upvotes: 1

MMS
MMS

Reputation: 404

It must be changed to

C:\sample.txt

Upvotes: -2

Related Questions