Get the path in c#

145 pts.
Tags:
C#
C# .NET
Development Environments
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

Answer Wiki

Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Discuss This Question: 8  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • BobBeechey
    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.
    750 pointsBadges:
    report
  • Saurabhdamle
    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
    25 pointsBadges:
    report
  • Ellie
    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
    145 pointsBadges:
    report
  • carlosdl
    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.
    69,175 pointsBadges:
    report
  • Ellie
    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
    145 pointsBadges:
    report
  • carlosdl
    What's in your 'fileName' variable ? That error occurs when you are looking for a file in path that doesn't exist.
    69,175 pointsBadges:
    report
  • Ellie
    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
    145 pointsBadges:
    report
  • carlosdl
    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.
    69,175 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following