51

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
  • 51,030
  • 56
  • 187
  • 275
yogeshkmrsoni01
  • 603
  • 1
  • 6
  • 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
  • @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
  • @ 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

8 Answers8

65

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,066
  • 9
  • 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) – Neha 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,117
  • 31
  • 46
  • 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 :)

1

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,208
  • 4
  • 32
  • 25
0

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

Thanks

Srikanth
  • 983
  • 3
  • 16
  • 28
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
  • 528
  • 4
  • 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
  • 551
  • 5
  • 14
0

How about : Environment.CurrentDirectory

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

raddevus
  • 7,113
  • 5
  • 61
  • 73