4 Replies - 325 Views - Last Post: 03 March 2010 - 09:07 PM Rate Topic: -----

#1 midouban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-July 09

Get the location of a file

Posted 02 March 2010 - 11:27 PM

I'm doing a project that write text file in some location.
I have writen my code here:

using (StreamWriter sw = new StreamWriter(pathFile, true))
{
      sw.WriteLine("-------------------");
      sw.WriteLine("Start :");
      sw.WriteLine(DateTime.Now);
      sw.WriteLine("");
}


pathfile argument is the location of the *.txt file that is string type.

The problem is I want to locate the *.txt file in the same folder as the *.exe file is. How i get the location of the *.exe file, which is the debug result of my program?

Thanks for your attention and help.

Is This A Good Question/Topic? 0
  • +

Replies To: Get the location of a file

#2 Adkins  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: Get the location of a file

Posted 03 March 2010 - 01:02 AM

Do you know the name of the text file or is it dynamic. Are you only doing this for the one exe file? When you answer these two questions I can kick out some help for you. Till then if you just wanna look into it on your own you are looking for the DirectoryInfo method.
Was This Post Helpful? 1
  • +
  • -

#3 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Get the location of a file

Posted 03 March 2010 - 01:59 AM

If you want your txt file to be located in the same location as your application's exe file, then just don't give this txt file any path, but provide only the name of the file like so:

using (StreamWriter sw = new StreamWriter(myTextFile.txt, true))
{
      sw.WriteLine("-------------------");
      sw.WriteLine("Start :");
      sw.WriteLine(DateTime.Now);
      sw.WriteLine("");
}



Then I believe your text file should be automatically located in the folder where your executing assembly is. Drop a comment if that doesn't help.
Was This Post Helpful? 2
  • +
  • -

#4 MentalFloss  Icon User is offline

  • "ADDICTED"[2:5]
  • member icon

Reputation: 528
  • View blog
  • Posts: 1,397
  • Joined: 02-September 09

Re: Get the location of a file

Posted 03 March 2010 - 10:09 AM

Quote

Then I believe your text file should be automatically located in the folder where your executing assembly is. Drop a comment if that doesn't help.


Yeah, this is correct. Just be sure that if you have multiple projects depending on each other, the binary outputs to a common directory. Otherwise, you're going to run into issues with those other projects finding that file (if they need to) in development/debug phase.
Was This Post Helpful? 0
  • +
  • -

#5 midouban  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 12-July 09

Re: Get the location of a file

Posted 03 March 2010 - 09:07 PM

View PostFlashM, on 03 March 2010 - 12:59 AM, said:

Then I believe your text file should be automatically located in the folder where your executing assembly is. Drop a comment if that doesn't help.


Thanks, But you must be forget that it needs a little modification:

using (StreamWriter sw = new StreamWriter("myTextFile.txt", true))
{
      sw.WriteLine("-------------------");
      sw.WriteLine("Start :");
      sw.WriteLine(DateTime.Now);
      sw.WriteLine("");
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1