Hi, I am writing a windows app with c#. Have 2 folders for pdf and text files under the project. How can I open or have access to the files under these folders??? I use System.IO.StreamReader objReader = new System.IO.StreamReader("/jrn/test.txt"), but it looks for the file in the following path: 'C:Documents and SettingsmeMyDocumentsVisual Studio 2005ProjectsCDProgramCDProgrambinDebugjrntest.txt.
Any ideas???
Thanks,
Elh
Software/Hardware used:
ASKED:
March 25, 2010 2:53 PM
UPDATED:
March 30, 2010 2:51 PM
I am not sure what you are looking for. The path you quote (apart from missing “” symbols) is certainly “below” the project while developing. You should probably, in the final release, use the System.IO.Path class to combine the string provided by the System.Windows.Forms.Application.StartupPath property with your desired path information to provide an absolute path to use with your StreamReader.
Are these both folders are under same folder?
If they are then you can store the path in “app.config” file of your application.
While giving the path in code, you can get the path from config file and concatenate the folder name for pdf files and text files.
You can get the path from app.config file as follows:
Configuration.ConfigurationSettings.AppSettings(“Attribute Name”)
Regards,
Saurabh
I am looking for the current path. I am writing an application which is going to be run from a CD (publish to a CD). Added folders under the solution/project. Like we have bin folders in asp.net.
When I run the program string fileName = System.Windows.Forms.Application.StartupPath + “\jrn\mayo_stmt_jrn.txt”; fileName gets the following value:
“C:\Documents and Settings\elham\My Documents\Visual Studio 2005\Projects\CDProgram\CDProgram\bin\Debug\jrn\mayo_stmt_jrn.txt”
I am wondering why it adds \bin\debug to the path? How can I get the current path???
Thanks,
Ellie
Hi Ellie.
It doesn’t add \bin\debug to the path. When you run your application from the IDE, it in fact is running from either \bin\debug or \bin\release (depending on your active configuration).
Try copying your exe to another location, and run it from there.
it doesn’t even let me run the program from the application. I get an error that says ‘could not find a part of the path” when I am trying to read the file
System.IO.StreamReader objReader = new System.IO.StreamReader(fileName);
So I am not able to trace the program!
Thanks,
Ellie
What’s in your ‘fileName’ variable ?
That error occurs when you are looking for a file in path that doesn’t exist.
Here is how I populate variable fileName:
fileName = System.Windows.Forms.Application.StartupPath + “jrnmayo_stmt_jrn.txt”;
file mayo_stmt_jrn.txt in under folder jrn under the solution.
When I trace the program fileName gets the following value. I could send you a snapshot of the program if I had your email address. I don’t know if I could do it here on this site!
“C:Documents and SettingselhamMy DocumentsVisual Studio 2005ProjectsCDProgramCDProgrambinDebugjrnmayo_stmt_jrn.txt”
Thanks,
Ellie
Ellie, to summarize:
If you are constructing your fileName variable this way:
fileName = System.Windows.Forms.Application.StartupPath + “jrn\mayo_stmt_jrn.txt”
Then the “\jrn\” subdirectory must exist in the path where you are running the application from, so, if you run your application from the IDE, it will run from either \bin\debug or \bin\release, so you have to create your \jrn\ subdirectory under those paths. If you are going to copy your executable program to another path, then you have to create the \jrn\ subdirectory under the path where your application has been copied to.