Firstly ensure that you DO NOT have that file under that directory in your actual project. It won't work correctly. Create a Data
folder in your project and place the file there.
\SolutionDirectory
\ProjectDirectory
\Data
topic6.txt
Program.cs
Anything under the bin
folder will get wiped out when you rebuild or clean your solution. This is for outputs only.
Next right click the file in Solution Explorer and set the Build Action to Content
. This will cause the file and folder to be copied to the output directory when you build your code. The build system needs to do this, not you.
Finally note that Application.StartupPath
may or may not be where your binary runs from. It is the working directory when the app starts and that doesn't have to be your binary directory. Since you are assuming that it is then you can just use a relative path to find the file.
using (var str = File.OpenText(@"Data\topic6.txt))
If you absolutely must use a full path AND the working directory may be changed then you need to get the path to the binary file. There are several ways to get this information, here's one.
string dirLoc = Path.Combine(Path.GetDirectoryName(Environment.ProcessPath), @"Data\topic6.txt");
This works for executables that you created but wouldn't work for a web app.