Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current working directory path c#?

I have a cursor file in project. I have given the absolute path in code i.e

F:/r.cur   

the problem is this is hard-coded path And i Want relative path so that if i move my solution to another system the code should not effect.

please suggest how to set relative path

//current code i am using   p.Cursor = new Cursor("F:/r.cur"); 
like image 699
yogeshkmrsoni01 Avatar asked Feb 12 '14 11:02

yogeshkmrsoni01


People also ask

How do I find the current directory path?

To determine the exact location of your current directory within the file system, go to a shell prompt and type the command pwd. This tells you that you are in the user sam's directory, which is in the /home directory. The command pwd stands for print working directory.

What is current working directory in C?

Alternatively referred to as the working directory or current working directory (CWD), the current directory is the directory or folder where you are currently working. The following sections contain some common use examples involving the current working directory. Windows current directory.

How do I get the current directory in C Windows?

On Windows, you need to use the equivalent functionality provided by the Windows API, ie GetCurrentDirectory() , declared in windows. h .


2 Answers

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); 
like image 143
Pavel Cermak Avatar answered Sep 28 '22 15:09

Pavel Cermak


use Application.StartupPath returns path for the executable file that started the application.

        string pathCur = Path.Combine(Application.StartupPath, @"..\..\r.cur");         Cursor = new Cursor(pathCur); 
like image 26
BRAHIM Kamel Avatar answered Sep 28 '22 16:09

BRAHIM Kamel