By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,949 Members | 890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

How to access a text file within VS2008 project folder?

100+
P: 132
Hi there.

I have a folder within my asp.net (c#) project in VS2008 called Files. Within this folder is a text file called files.txt.

Is there a way to access this folder from the C# code please?

What I am looking for is to invoke a StreamReader (or other suitable reader) and read in the contents of this folder, so to populate string values in the code.

An example of the code would look something like:

Expand|Select|Wrap|Line Numbers
  1. StreamReader sr = new StreamReader("~\\files.txt");
  2.  
Where the path is hence pointing at the folder within the project.

The above example does not work but any help will be so apprecaited please?

Thank you.

M :o)
Feb 18 '09 #1
Share this Question
Share on Google+
1 Reply


tlhintoq
Expert 2.5K+
P: 3,525
Doesn't this approach mean that your program will be hard coded to only work on your machine, and only so long as you keep the project folder? If it is looking for files inside your project folder, which is inside your documents, under your user name... {fragile} Then the program will fail in the real world.

A better approach might be to obtain the designation of the boot drive (generally C:), then look for a file at {bootdrive}\{application.company}\{application.tit le}\files.txt

This will give you a consistent scheme that automatically updates location based on the properties of this, and the next, application you your right.

C:\\SmithProgramming\\MyFirstProgram\\files.txt
G:\\SmithProgramming\\MySecondProgram\\file.txt
etc.
Feb 19 '09 #2

Post your reply

Sign in to post your reply or Sign up for a free account.