57

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");
Richard Ev
  • 52,939
  • 59
  • 191
  • 278
yogeshkmrsoni01
  • 663
  • 1
  • 7
  • 9
  • 4
    put it in the resource folder, and reference it as Properties.Reources.xxx – David Feb 12 '14 at 11:05
  • Have you tried to pack r.cur into your project directory? – hbsrud Feb 12 '14 at 11:07
  • 1
    @David yes i Have tried this by putting the r.cur file in recourse folder but its not showing it in code. its only showing .jpg type of files – yogeshkmrsoni01 Feb 12 '14 at 11:13
  • @hbsrud If i try to pack r.cur in same project directory still i need to give absolute path. This might seems okay. but still it might have dependency if I make setup of the project and send it to another user – yogeshkmrsoni01 Feb 12 '14 at 11:15
  • @yogeshkmrsoni01, did you right click the resource file and put the action as "embedded resource", rather than "none" or "compile"? – David Feb 12 '14 at 11:16
  • 1
    @ David, This new thing i have learnt. but still it is not showing me ..;) suggest any other way – yogeshkmrsoni01 Feb 12 '14 at 11:24
  • @yogeshkmrsoni01 You should try Vignesh Kumars approach, put that ``r.cur`` in your project folder and then do ``p.Cursor = new Cursor(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/r.cur");`` – hbsrud Feb 12 '14 at 11:26
  • @hbsrud its also giving me C:\....\myProjectName\bin\Release Folder. and i want to put my cursor file to C:\....\myProjectName\Cursor\r.cur – yogeshkmrsoni01 Feb 12 '14 at 11:37
  • In that case try ``p.Cursor = new Cursor(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "/Cursor/r.cur");``, but you have to change the Properties of r.cur, so it gets copied. – hbsrud Feb 12 '14 at 11:44
  • @hbsrud if i do this then new path will be: C:/..../MyProj/bin/Release/Cursor/r.cur ; but i want C:/..../MyProj/Cursor/r.cur; – yogeshkmrsoni01 Feb 12 '14 at 12:09
  • possible duplicate of [Get current folder path](http://stackoverflow.com/questions/15653921/get-current-folder-path) –  Aug 14 '14 at 07:21

10 Answers10

78

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);
Pavel Cermak
  • 1,215
  • 10
  • 13
  • 15
    The above code dosen't work in WPF as as it not have System.Windows.Forms namespace. Use the following instead System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) – Avinash Jain May 10 '16 at 11:37
4

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);
BRAHIM Kamel
  • 13,492
  • 1
  • 36
  • 47
  • Its returning the C:\Users\yogeshkmrsoni\Documents\Visual Studio 2010\Projects\ProjectName\bin\Release ; but i want to copy cursor file to C:\Users\yogeshkmrsoni\Documents\Visual Studio 2010\Projects\ProjectName\r.cur, can u suggest any other way – yogeshkmrsoni01 Feb 12 '14 at 11:16
  • 1
    just to note: this is only for WinForms – sonyisda1 Aug 23 '18 at 12:29
4

You can also get by
System.IO.Directory.GetCurrentDirectory();
but it shows bin and debug folder also, if you don't want these folder so you can use that code :

string page = "E:\abccom\Cat\Mouse.aspx"

string name = Path.GetFileName(page );
string nameKey = Path.GetFileNameWithoutExtension(page );
string directory = Path.GetDirectoryName(page );

Console.WriteLine("{0}, {1}, {2}, {3}",
page, name, nameKey, directory);

Output:

GetFileName:                                        Mouse.aspx
GetFileNameWithoutExtension:           Mouse
GetDirectoryName:                              E:\abccom\Cat

Happy Coding :)

3

How about : Environment.CurrentDirectory

See more at : https://learn.microsoft.com/en-us/dotnet/api/system.environment?view=net-5.0

raddevus
  • 8,142
  • 7
  • 66
  • 87
1

Super super late to the party.

But depends on what you mean - if you're after the directory onto which your app code is finally deployed (on your test machine for example), then this works for a regular console app:

//gives something like D:\source\repos\Repo\src\AppName\bin\Debug\net6.0\Appame.dll
var assemblyLocation = Assembly.GetEntryAssembly().Location; 

//gives something like D:\source\repos\Repo\src\AppName\bin\Debug\net6.0
var currentDirectory = Path.GetDirectoryName(assemblyLocation);

//gives something like D:\source\repos\Repo\src\AppName
var appRootFolder = currentDirectory.Split("\\bin")[0];

Console.WriteLine($"The root of the application is here: {appRootFolder}");
basecamp
  • 81
  • 1
  • 5
0

You can get the current working directory by using System.IO.Directory.GetCurrentDirectory(). it will return your current executable path.

Thanks

Srikanth
  • 980
  • 3
  • 16
  • 30
0

Application.StartupPath should give you application path from where your application is running. I would create a directory structure under application folder. e.g If "C:\Program Files\MyApp" is my application folder, then I would create a folder named cursors under it (C:\Program Files\MyApp\Cursors") and put all cursors within this folder.

Sandeep
  • 568
  • 5
  • 7
0

System.IO.Path.GetDirectoryName(System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName) at startup will give you the full path.

After that, if you * really want to find something during development (as your comments in other answers point) *, first find the path using FileInfo(thestringwithfilenamepath).Directory.Name.

ilias iliadis
  • 601
  • 8
  • 15
0

Super late to this party, but this works for me when I'm in unit tests.

var currentDirectory = Directory.GetCurrentDirectory();

If you need the root of the project, and not the bin directory then this:

var currentDirectory = Directory.GetCurrentDirectory();
var basePath = currentDirectory.Split(new string[] { "\\bin" }, StringSplitOptions.None)[0];

It'll be different if you're on a website.

Don Rolling
  • 2,301
  • 4
  • 30
  • 27
0

For .net 6 and .net 7 I use:

var filePath = Path.Combine(AppContext.BaseDirectory, $"someFile.txt");
Tono Nam
  • 34,064
  • 78
  • 298
  • 470