user12017098
user12017098

Reputation:

Trying to access a file

Using C# I keep getting an error message when trying to access a file that I know exists and I know the file-path is correct. This is the first of a few times I need to access the file and they all fail to locate the file.

FYI, i'm still learning/new to C# so it could be something simple I just don't know.

input[2] = query.txt //this is actually from a user input in the program

string docPath = @"C:\Users\Steve\Documents\";
string datafile = docPath + input[2];

int inputlinecount = System.IO.File.ReadLines(inputfile).Count();

The error message that keeps coming up:

System.IO.FileNotFoundException: 'Could not find file 'C:\Users\Steve\Documents\query.txt'.'

Upvotes: 1

Views: 107

Answers (2)

Vlam
Vlam

Reputation: 1798

Your system is hiding file extensions. So your actual filename could be query.txt.txt.

Upvotes: 2

M.JiHong
M.JiHong

Reputation: 1

Did you tried this code?

this code access a file and read file data

string docPath = @"C:\Users\Steve\Documents\";
string datafile = docPath + input[2];

// make a filestream
FileStream fs = new FileStream(datafile, FileMode.OpenOrCreate);
StreamReader reader = new StreamReader(fs);

// read file and print to console
while(!reader.EndOfStream)
{
    Console.WriteLine(reader.ReadLine());
}

reader.Close();

Upvotes: 0

Related Questions