Reputation: 639
How do I convert a path from my drive to an application path? Currently I am creating and accessing files on my hard disk (eg: "D:\MyFolder\MyDoc.doc"). I want this path inside my console application folder. I know I can use Server.MapPath
for ASP.NET applications. What about console applications?
Upvotes: 2
Views: 1457
Reputation: 11144
EDITED
If you want to read file from your current folder
System.IO.FileStream stream = System.Reflection.Assembly.GetExecutingAssembly().GetFile("filename");
or you want to get directory path
string path = System.Reflection.Assembly.GetExecutingAssembly().Location;
var directory = System.IO.Path.GetDirectoryName(path);
var parentdir = System.IO.Directory.GetParent(directory);
Upvotes: 3
Reputation: 75326
To avoid hardcoding path file, put MyDoc.doc
in execution folder, then you can get execution folder by using Directory.GetCurrentDirectory()
:
string directory = Directory.GetCurrentDirectory();
string fileName = Path.Combine(directory, "MyDoc.doc");
Other alternative:
string path = Assembly.GetExecutingAssembly().Location;
string directory = Path.GetDirectoryName(path);
Or:
string directory = AppDomain.CurrentDomain.BaseDirectory;
Or:
string directory = Path.GetDirectoryName(Application.ExecutablePath);
To get bin
folder:
var bin = Directory.GetParent(directory ).Parent.FullName;
Upvotes: 2