Get current folder path
You should not use Directory.GetCurrentDirectory()
in your case, as the current directory may differ from the execution folder, especially when you execute the program through a shortcut.
It's better to use Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
for your purpose. This returns the pathname where the currently executing assembly resides.
While my suggested approach allows you to differentiate between the executing assembly, the entry assembly or any other loaded assembly, as Soner Gönül said in his answer,
System.IO.Path.GetDirectoryName(Application.ExecutablePath);
may also be sufficient. This would be equal to
System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
How do I get the full path of the current file's directory?
The special variable __file__
contains the path to the current file. From that we can get the directory using either pathlib
or the os.path
module.
Python 3
For the directory of the script being run:
import pathlib
pathlib.Path(__file__).parent.resolve()
For the current working directory:
import pathlib
pathlib.Path().resolve()
Python 2 and 3
For the directory of the script being run:
import os
os.path.dirname(os.path.abspath(__file__))
If you mean the current working directory:
import os
os.path.abspath(os.getcwd())
Note that before and after file
is two underscores, not just one.
Also note that if you are running interactively or have loaded code from something other than a file (eg: a database or online resource), __file__
may not be set since there is no notion of "current file". The above answer assumes the most common scenario of running a python script that is in a file.
References
- pathlib in the python documentation.
- os.path - Python 2.7, os.path - Python 3
- os.getcwd - Python 2.7, os.getcwd - Python 3
- what does the __file__ variable mean/do?
Find the current directory and file's directory
To get the full path to the directory a Python file is contained in, write this in that file:
import os
dir_path = os.path.dirname(os.path.realpath(__file__))
(Note that the incantation above won't work if you've already used os.chdir()
to change your current working directory, since the value of the __file__
constant is relative to the current working directory and is not changed by an os.chdir()
call.)
To get the current working directory use
import os
cwd = os.getcwd()
Documentation references for the modules, constants and functions used above:
- The
os
andos.path
modules. - The
__file__
constant os.path.realpath(path)
(returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")os.path.dirname(path)
(returns "the directory name of pathnamepath
")os.getcwd()
(returns "a string representing the current working directory")os.chdir(path)
("change the current working directory topath
")
How to set relative path to current folder?
Just dot is working. The doctype makes a difference however as sometimes the ./ is fine as well.
<a href=".">Link to this folder</a>
C# How to get current directory path
I will answer my own question, It's not possible to get current path from library project. You'll have to get it from installer. There is no other way.
Thank you.
How to get current working directory path c#?
You can use static Directory
class - however current directory is distinct from the original directory, which is the one from which the process was started.
System.IO.Directory.GetCurrentDirectory();
So you can use the following to get the directory path of the application executable:
System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath);
PHP : Get current directory name
You just want the last element of an array:
$page_name = dirname(__FILE__);
$each_page_name = explode('/', $page_name);
echo end($each_page_name);
PHP Get name of current directory
getcwd();
or
dirname(__FILE__);
or (PHP5)
basename(__DIR__)
http://php.net/manual/en/function.getcwd.php
http://php.net/manual/en/function.dirname.php
You can use basename()
to get the trailing part of the path :)
In your case, I'd say you are most likely looking to use getcwd()
, dirname(__FILE__)
is more useful when you have a file that needs to include another library and is included in another library.
Eg:
main.php
libs/common.php
libs/images/editor.php
In your common.php
you need to use functions in editor.php
, so you use
common.php
:
require_once dirname(__FILE__) . '/images/editor.php';
main.php
:
require_once libs/common.php
That way when common.php is require'd
in main.php
, the call of require_once
in common.php
will correctly includes editor.php
in images/editor.php
instead of trying to look in current directory where main.php
is run.
Related Topics
How to Rethrow an Exception in C#
Difference Between a Regular String and a Verbatim String
Json.Net Serialization of Type With Polymorphic Child Object
Line Delimited Json Serializing and De-Serializing
Passing Arguments to C# Generic New() of Templated Type
How to Access Random Item in List
Access the Current Httpcontext in ASP.NET Core
Mocking Extension Methods With Moq
How to Modify a CSS Style in the Code Behind File for Divs in ASP.NET
Pass Extra Parameters to an Event Handler
Using Parameters Inserting Data into Access Database
How to Retrieve Id of Inserted Entity Using Entity Framework
How to Do Constructor Chaining in C#