Nadav
Nadav

Reputation: 2717

Opening a file without extension

I'm having a problem opening a file in C#.
I got a file which I need to read and when I'm trying to open it using C#, for some reason file cannot be found.
Here is my code:

 string fullpath = Directory.GetCurrentDirectory() + 
                   string.Format(@"\FT933\FT33_1");
 try
 {
      StreamReader reader = new StreamReader(fullpath);
 }
 catch(Exception e)
 {
       Console.WriteLine("The file could not be read:");
       Console.WriteLine(e.Message);
 }

The file I'm trying to open is inside Debug\FT933\FT33_1 and got no extension.
Whenever I'm trying to open a text file from the same directory I manage to do so.

EDIT:

to be more it precise i think that problem that i have is that i dont know how to open a file that has no extentions (if i change the file to have .txt extention i do manage to open it)

Upvotes: 2

Views: 6390

Answers (1)

Marco
Marco

Reputation: 57573

Don't use hardcoded path or directories, but builtin functions to join paths.
Try

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string fullpath = Path.Combine(path, your_filename);

And remember that current directory could not be your app's one!.
More, always include streams in using statement

using(StreamReader reader = new StreamReader(fullpath))
{
    // Do here what you need
}

so you're sure it will be released when necessary not wasting memory!

EDITED after OP comment:
This is my working attempt:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string fullpath = Path.Combine(path, @"FT933\FT33_1");
if (File.Exists(fullpath))
{
    using (StreamReader reader = new StreamReader(fullpath))
    {
        string ret = reader.ReadLine();
    }
}
else
{
    // File does not exists
}

If you fall in // File does not exists section, be sure that file is not where you're searching for!
Are you sure your file does not have an hidden extension?
Are you sure OS or some app is not locking file for some reason?

EDITED again after another comment:
Open a command prompt (using Start->Run->CMD (enter) ) and run this command:

dir "C:\Users\Stern\Documents\Visual Studio 2010\Projects\Engine\ConsoleApplication1\bin\Debug\FT933\*.*" /s

and edit your question showing us result.

Upvotes: 8

Related Questions