.NET Questions (CLOSED)

Questions and Answers on any aspect of .NET. Now closed.

This discussion group is now closed.

Have a question about .NET development? Try stackoverflow.com, a worldwide community of great developers asking and answering questions 24 hours a day.

The archives of .NET Questions contain years of Q&A. Even older .NET Questions are still online, too.

How to get Application Path in C#

how to get the application path in C# i am doing a small project i want to know the path where the user installs my applicaton and i should be able to create a folder there and put some help files and log information
Thanks
MCV
MCV Send private email
Monday, December 04, 2006
 
 
Hey I want the substring of it if the path is
c:/Program files/Test/Add/myproj.exe
I want the Path till
c:/program files/Test
MCV Send private email
Monday, December 04, 2006
 
 
Use Application.ExecutablePath, and strip off the name of the  executable.
Chris Doggett Send private email
Monday, December 04, 2006
 
 
> strip off the name of the  executable
... and the best way to do this is to use classes in the System.IO namespace - Path, FileInfo, DirectoryInfo.

For example you can get the full name of the directory containing your executable as follows:
string executableName = Application.ExecutablePath;
FileInfo executableFileInfo = new FileInfo(executableName);
string executableDirectoryName = executableFileInfo.DirectoryName;

And if you want the parent of this directory (as indicated in the original post: c:/program files/Test rather than c:/Program files/Test/Add) you can use:

string parentName = executableFileInfo.Directory.Parent.FullName;

The directory names returned above will have a trailing path separator character (\).  If you want to use them to build a filename, use Path.Combine so you don't need to worry about path separators:

string myFile = Path.Combine(parentName, "MyFile.Dat");

> ... to know the path where the user installs my applicaton and i should be able to create a folder there

Notwithstanding the above, it is not generally recommended to create files in an application's install directory.  Such files are shared by all users, and may not be writable by non-administrator users.

Usually it's better to write files in the directory specified by Application.UserAppDataPath or Application.LocalUserAppDataPath.
Joe
Monday, December 04, 2006
 
 
Application.StartupPath
dood mcdoogle
Monday, December 04, 2006
 
 
Thankyou joe and all of u guys
MCV
MCV Send private email
Monday, December 04, 2006
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz