Sei sulla pagina 1di 4

Questions Job Board Documentation Tags Users Search...

Log In Sign Up
BETA

x Dismiss

Join the Stack Overflow Community

Stack Overflow is a community of 7.3 million


programmers, just like you, helping each other.
Join them it only takes a minute:

Sign up

Best way to get application folder path


Ask Question

asked 6 years ago

viewed 317,707 times

active 2 months ago

I see that there are some ways to get the application folder path:
BLOG
275 1. Application.StartupPath
New Kids on the Block: Understanding
2. System.IO.Path.GetDirectoryName( Developers Entering the Workforce
System.Reflection.Assembly.GetExecutingAssembly().Location) Today
3. AppDomain.CurrentDomain.BaseDirectory
99 4. System.IO.Directory.GetCurrentDirectory()
5. Environment.CurrentDirectory
6. System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)

7. System.IO.Path.GetDirectory(Application.ExecutablePath)

What is the best way depending on the situation?

c# .net

share improve this question edited Jul 19 '15 at 11:50 asked May 18 '11 at 7:35
Peter Mortensen Leo Vo
11.2k 16 76 109 2,494 9 42 68

Linked
6 Why we have a lot ways to get application' path. I think there is a reason for each way. Leo Vo May 18 '11
at 7:43 13 How to get application folder when program
is started by Windows Task Scheduler
There is a error in #6: should read: System.Reflection.Assembly.GetExecutingAssembly().GetName().
CodeBase), System.IO.Path.GetDirectoryName(Application.ExecutablePath) BillW Nov 6 '14 at 15:33 Loading an XML file path in C#
4

4 Environment.CurrentDirectory vs
System.IO.Directory.GetCurrentDirectory
7 Answers active oldest votes
6 How to get executing assembly location?

3 Creating and using c# Application Folder


AppDomain.CurrentDomain.BaseDirectory is probably the most useful for accessing files whose
location is relative to the application install directory. 0 System.IO.Path.GetDirectoryName makes
path invalide
302
In an ASP.NET application, this will be the application root directory, not the bin subfolder - which is
0 Hard Coding a Relative Path in C#
probably what you usually want. In a client application, it will be the directory containing the main
executable. 4 Alternative to FileStream in C# .NET

In a VSTO 2005 application, it will be the directory containing the VSTO managed assemblies for your 1 How do I use a relative path instead of an
application, not, say, the path to the Excel executable. absolute path

The others may return different directories depending on your environment - for example see 3 How do I assign a relative path of an embed
@Vimvq1987's answer. image to a
ActiveSheet.PageSetup.LeftHeaderPicture.
CodeBase is the place where a file was found and can be a URL beginning with http://. In which case FileName?
Location will probably be the assembly download cache. CodeBase is not guaranteed to be set for see more linked questions
assemblies in the GAC.
Related
share improve this answer edited Jul 19 '15 at 11:51 answered May 18 '11 at 7:55
Peter Mortensen Joe 474 What is the correct way to create a single-
11.2k 16 76 109 88.4k 21 130 254 instance application?

552 How do I get the path of the assembly the


1 When testing in Windows XP 32bit, it returns where the shortcut started. Joshua Son Dec 7 '13 at 2:37 code is in?

This also worked on the Mac with Xamarin. Jared Updike Jan 29 '14 at 20:29 1510 What is the best way to iterate over a
Dictionary in C#?
+1 @Joe and For VSTO document-level add-in see THIS user2140173 Apr 3 '14 at 9:15
489 Best way to repeat a character in C#
1 Be aware that this returns a path with a backslash at the end. This caused me problems when formatting a
string with the result to pass as a process argument. avenmore Nov 6 '15 at 12:18 1706 How do I get a consistent byte
representation of strings in C# without
6 @avenmore - If you're formatting a string to build a path, consider using Path.Combine instead. This will
manually specifying an encoding?
take care of the trailing backslash for you. Joe Nov 6 '15 at 13:09
733 Best way to parse command line
show 1 more comment arguments in C#?

695 How can I get the application's path in a


.NET console application?

48 What is the best way to get the executing


exe's path in .NET?

417 How to delete all files and folders in a


directory?
Note that not all of these methods will return the same value. In some cases, they can return the same
782 Why not inherit from List<T>?
value, but be careful, their purposes are different:
36
Application.StartupPath Hot Network Questions
How can I buy end-user bandwidth for my
returns the StartupPath parameter (can be set when run the application) customers?

System.IO.Directory.GetCurrentDirectory() What's the purpose of `id` function in the


FSharp.Core?

returns the current directory, which may or may not be the folder where the application is located. The Globally defined macro changes numerical value
same goes for Environment.CurrentDirectory . In case you are using this in a DLL file, it will return
Rigorous nature of combinatorics
the path of where the process is running (this is especially true in ASP.NET).
How was so much wildfire made and then
share improve this answer edited Jul 19 '15 at 11:54 answered May 18 '11 at 7:45 transported underneath the cathedral?

Peter Mortensen Vimvq1987 Strange mud-like formation on ceiling


11.2k 16 76 109 6,061 21 77 119
Filtering a large (50gb+) JSON lines file matching
CIDR's

A poison that only works if you know it is there

Obfuscating event location on map


1. Application.StartupPath and 7.
System.IO.Path.GetDirectoryName(Application.ExecutablePath) - Is only going to work for What is the logic behind a 50% of X% 401(k)
20 Windows Forms application contribution match?

Why does it take so long to develop modern


2. System.IO.Path.GetDirectoryName(
military jets?
System.Reflection.Assembly.GetExecutingAssembly().Location)
Is it acceptable to do non-work-related things
Is going to give you something like: during my last few weeks with no work assigned
to me?
"C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\Temporary ASP.NET Files\\legal
services\\e84f415e\\96c98009\\assembly\\dl3\\42aaba80\\bcf9fd83_4b63d101" which is where In Honor of Adam West
the page that you are running is. Much Ado About a Sequence
3. AppDomain.CurrentDomain.BaseDirectory for web application could be useful and will return
Usage of "Hab" during conversations
something like "C:\\hg\\Services\\Services\\Services.Website\\" which is base directory and
is quite useful. PXE-E61: Media test failure, check cable and
PXE-MOF: Exiting PXE ROM
4. System.IO.Directory.GetCurrentDirectory() and 5. Environment.CurrentDirectory
Which Sitecore Experience Accelerator version
should be used with Sitecore 8.2 Update 2?
will get you location of where the process got fired from - so for web app running in debug mode from
Visual Studio something like "C:\\Program Files (x86)\\IIS Express" Microsoft just blocked handling HTML markup in
SharePoint calculated fields - how to get the
6. System.IO.Path.GetDirectoryName( same functionality back

System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) What home printer would allow for exact


alignment of front and back?
will get you location where .dll that is running the code is, for web app that could be Grep: unexpected results when searching for
"file:\\C:\\hg\\Services\\Services\\Services.Website\\bin" UPPERCASE words

Is it ok to place my DSLR in a box and just cut a


Now in case of for example console app points 2-6 will be directory where .exe file is.
hole for the lens?

Hope this saves you some time. DM advice for a very specific type of one-off

My first 401k, starting late-ish - should I stick with


share improve this answer edited Feb 8 at 10:31 answered Feb 9 '16 at 15:17 target date?
legend1337 Matas Vaitkevicius
Is it bad practice to not delete redundant files
8 5 21.3k 10 102 125
right away from VCS but instead mark them as
"To be deleted" with comments first?

For a web application, to get the current web application root directory, generally call by web page for
the current incoming request:
2
HttpContext.Current.Server.MapPath();

System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath;

Above code description

share improve this answer edited Jul 19 '15 at 11:55 answered Jun 6 '15 at 6:24
Peter Mortensen Raj kumar
11.2k 16 76 109 199 2 7
I started a process from a Windows Service over the Win32 API in the session from the user which is
actually logged in (in Task Manager session 1 not 0). In this was we can get to know, which variable is
0 the best.

For all 7 cases from the question above, the following are the results:

Path1: C:\Program Files (x86)\MyProgram


Path2: C:\Program Files (x86)\MyProgram
Path3: C:\Program Files (x86)\MyProgram\
Path4: C:\Windows\system32
Path5: C:\Windows\system32
Path6: file:\C:\Program Files (x86)\MyProgram
Path7: C:\Program Files (x86)\MyProgram

Perhaps it's helpful for some of you, doing the same stuff, when you search the best variable for your
case.

share improve this answer edited Feb 20 at 19:36 answered Feb 10 at 13:51
Bhargav Rao Beetee
25.4k 16 65 101 53 2 9

I have used this one successfully

0 System.IO.Path.GetDirectoryName(Process.GetCurrentProcess).MainModule.FileName)

It works even inside linqpad.

share improve this answer answered Mar 17 at 1:57


camilohe
419 4 9

this one "System.IO.Path.GetDirectory(Application.ExecutablePath)" changed to


System.IO.Path.GetDirectoryName(Application.ExecutablePath)
0
share improve this answer answered Apr 8 at 20:31
nazim hatipoglu
401 6 16

Your Answer

Sign up or log in Post as a guest

Sign up using Google Name

Sign up using Facebook


Email

required, but never shown


Sign up using Email and Password

Post Your Answer

By posting your answer, you agree to the privacy policy and terms of service.
Not the answer you're looking for? Browse other questions tagged c# .net or ask your own question.

question feed

about us tour help blog chat data legal privacy policy work here advertising info developer jobs directory mobile contact us feedback

TECHNOLOGY LIFE / ARTS CULTURE / RECREATION SCIENCE OTHER

Stack Overflow Geographic Code Review Photography English Language & MathOverflow Meta Stack Exchange
Information Systems Usage
Server Fault Magento Science Fiction & Mathematics Stack Apps
Electrical Engineering Fantasy Skeptics
Super User Signal Processing Cross Validated (stats) Area 51
Android Enthusiasts Graphic Design Mi Yodeya (Judaism)
Web Applications Raspberry Pi Theoretical Computer Stack Overflow Talent
Information Security Movies & TV Travel Science
Ask Ubuntu Programming Puzzles
Database & Code Golf Music: Practice & Christianity Physics
Webmasters Administrators Theory
more (7) English Language Chemistry
Game Development Drupal Answers Seasoned Advice Learners
(cooking) Biology
TeX - LaTeX SharePoint Japanese Language
Home Improvement Computer Science
Software Engineering User Experience Arqade (gaming)
Personal Finance & Philosophy
Unix & Linux Mathematica Bicycles
Money more (3)
Ask Different (Apple) Salesforce Role-playing Games
Academia
WordPress ExpressionEngine Anime & Manga
Development more (8)
Answers
Motor Vehicle
Cryptography Maintenance & Repair

more (17)

site design / logo 2017 Stack Exchange Inc user contributions licensed under cc by-sa 3.0 with attribution required

Potrebbero piacerti anche