open file in same folder as application

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2007
Posts: 164
Reputation: Ravenous Wolf is an unknown quantity at this point 
Solved Threads: 1
Ravenous Wolf Ravenous Wolf is offline Offline
Junior Poster

open file in same folder as application

 
0
  #1
Oct 23rd, 2007
can anyone tell me if it is possible in C# to open a file which is located in the same folder as the application without specifying the path to that file?

if both file and program is located for example on C:/containfileandprog can i open the file from within a program without writing C:/containfileandprog but just the filename.dat?

i have no control on where the file or the program is going to be installed so i cant specify them in advance. but i can make sure that they both end up in the same folder where ever the enduser might want to put them.
... what society overwhelmingly asks for is snake oil. Of course, the snake oil has the most impressive names —otherwise you would be selling nothing— like "Structured Analysis and Design", "Software Engineering", "Maturity Models", "IPSE", "MIS", "OO", "BPRE".... by Edsger W. Dijkstra
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: open file in same folder as application

 
0
  #2
Oct 23rd, 2007
to open a file that's beside your application
  1. Application.StartupPath returns the path where your application starts from
Example to open text file called 1.txt which is beside your .exe
  1. System.Diagnostics.Process.Start(Application.StartupPath + "\\1.txt");
Last edited by Ramy Mahrous; Oct 23rd, 2007 at 9:43 am.
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 20
Reputation: tostrinj is an unknown quantity at this point 
Solved Threads: 1
tostrinj's Avatar
tostrinj tostrinj is offline Offline
Newbie Poster

Re: open file in same folder as application

 
0
  #3
Oct 26th, 2007
if you are doing console, it should work like this:
  1. string _sAppPathName = Process.GetCurrentProcess().MainModule.FileName;
  2.  
  3. string _sDataFilePath = _sAppPath.Substring(0, _sAppPath.Length - (_sAppPath.Length - _sAppPath.LastIndexOf(@"\"))) + @"\yourfilename.yourextension";
Last edited by tostrinj; Oct 26th, 2007 at 11:00 pm. Reason: code bit looks like crap (formatting
===========================
can you repeat the part of the stuff where you said all about the things?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 8102 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC