943,692 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 14774
  • C# RSS
Oct 23rd, 2007
0

open file in same folder as application

Expand Post »
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.
Similar Threads
Reputation Points: 33
Solved Threads: 1
Junior Poster
Ravenous Wolf is offline Offline
164 posts
since Aug 2007
Oct 23rd, 2007
0

Re: open file in same folder as application

to open a file that's beside your application
C# Syntax (Toggle Plain Text)
  1. Application.StartupPath returns the path where your application starts from
Example to open text file called 1.txt which is beside your .exe
C# Syntax (Toggle Plain Text)
  1. System.Diagnostics.Process.Start(Application.StartupPath + "\\1.txt");
Last edited by Ramy Mahrous; Oct 23rd, 2007 at 9:43 am.
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Oct 26th, 2007
0

Re: open file in same folder as application

if you are doing console, it should work like this:
C# Syntax (Toggle Plain Text)
  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
Reputation Points: 12
Solved Threads: 1
Newbie Poster
tostrinj is offline Offline
20 posts
since Jul 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: How to disable specific key code in keyDown event?
Next Thread in C# Forum Timeline: call cmd processes and keeping them cmd window





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC