Reputation: 693
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");
Upvotes: 60
Views: 148305
Reputation: 36048
For .net 6 and .net 7 I use:
var filePath = Path.Combine(AppContext.BaseDirectory, $"someFile.txt");
Upvotes: 2
Reputation: 131
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}");
Upvotes: 1
Reputation: 9077
How about : Environment.CurrentDirectory
See more at : https://learn.microsoft.com/en-us/dotnet/api/system.environment?view=net-5.0
Upvotes: 4
Reputation: 2339
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.
Upvotes: 0
Reputation: 631
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
.
Upvotes: 0
Reputation: 103
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 :)
Upvotes: 4
Reputation: 13755
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);
Upvotes: 4
Reputation: 1285
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);
Upvotes: 84
Reputation: 608
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.
Upvotes: 0
Reputation: 1010
You can get the current working directory by using System.IO.Directory.GetCurrentDirectory()
. it will return your current executable path.
Thanks
Upvotes: 0