Hi,

I am an old VB6 programmer converting to C#, so I hope you forgive stupid questions.

I am developing a C# app to run on Windows Mobile and Windows CE (handheld terminals). I have figured out how to read data from text files but what is screwing me up is how to tell the program where the text file is on the handheld. Here are some details;

1. In Solution Explorer I created a directory called "Text files"
2. In that directory is a file called "sample.txt"
3. In the properties for sample.txt, under 'Copy to output directory', I chose 'Copy if newer'

So far, so good. When I compile, the text file is copied to a directory under the application directory called 'Text files'.

The problem is when I try to use Streamreader to open the file, it wants to find it at the root directory.

Regardless of where the application is running, I should be able to use a relative path to get to Text files\sample.txt, but it doesn't work that way.

I have this so far;

using (StreamReader sr = new StreamReader("Text files\\sample.txt"))

but it can't find the file. What am I doing wrong? It seems wierd to me that I can't just start in the application directory and path from there.

Scott


How do i tell the application how to find the text file?

Your relative path is incorrect because it is checking from the bin folder where the app is running from. Try "..\\..\\Test files\\sample.txt" to get to the root of your project.

Actually, now that I think about it, if you are using the option to copy to output directory, you don't need any path information. Just pass the file name: "sample.txt".

On the terminal, the executable is under My Device\Program Files\SmartDeviceProject1 and the sample.txt file is in My Device\Program Files\SmartDeviceProject1\Text files. Shouldn't I assume the starting path is My Device\Program Files\SmartDeviceProject1 ? I changed the line to

using (StreamReader sr = new StreamReader("..\\..\\Text Files\\sample.txt"))

but it still fails.

On the terminal, the executable is under My Device\Program Files\SmartDeviceProject1 and the sample.txt file is in My Device\Program Files\SmartDeviceProject1\Text files. Shouldn't I assume the starting path is My Device\Program Files\SmartDeviceProject1 ? I changed the line to

using (StreamReader sr = new StreamReader("..\\..\\Text Files\\sample.txt"))

but it still fails.

Sorry, see my other post. You don't need to specify a path at all...

Visual Studio automatically created the directory 'Text files' on the terminal. If I move the text file out of that directory in Solution Explorer, the file gets copied to the same directory on the terminal as the exe, which should make it super easy to access, but Streamreader adds one backslash in front of the filename.

The IDE creates a copy of the file in your "Text files" folder you added to the project. The "Copy to output directory" option says to copy a version of that one to the bin folder where the app.exe is placed. So, you shouldn't need to specify a path with the filename.

When you run your exe, the StreamReader should look for it in that folder if you omit the path from the filename. In other words, it will find the file in the current working directory. So, if you are running the exe from a different directory, it will NOT find it.

If you are still having trouble and you just want StreamReader to find the file in the app.exe folder, you can also obtain the exe's path via: Application.ExecutablePath .

Or, you would probably rather access:

Application.StartupPath

, which excludes the exe's filename...

I put the text file in the app directory and have confirmed that it is in the application directory on the terminal. Then I changed the line to

using (StreamReader sr = new StreamReader("sample.txt"))

The error I get is "Could not find file '\sample.txt'

I don't understand bin folder.

I am not getting th eoptions for Application that I should. These are my includes;

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PsionTeklogix.Imager;
using System.IO;
using System.Text.RegularExpressions;

but when I type string appPath = Application. the choices are DoEvents, Equals, Exit, ReferenceEquals and Run.

I am not getting th eoptions for Application that I should. These are my includes;

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PsionTeklogix.Imager;
using System.IO;
using System.Text.RegularExpressions;

but when I type string appPath = Application. the choices are DoEvents, Equals, Exit, ReferenceEquals and Run.

Not sure why you don't get the Intellisense completion. The Application class is part of the System.Windows.Forms namespace. I assume you have the System.Windows.Forms.dll reference added too.

Have you defined your own Application class or namespace or something? Zip up your project and I'll take a look.

This has something to do with it being an app that runs on WindowsMobile. I creates a plain old Windows Forms app and

string appPath = Application.StartupPath;

works just fine.

This article has been dead for over six months. Start a new discussion instead.