I currently have a statement as follows:
string dir = "C:\\Users\\Limited\\Desktop\\";
Although I would like it to be specified as a directory within the work directroy e.g.
workingpath/myfolder
Can this be done?
I assumed you could just use a relative path, i.e. "myfolder", but you can get and use the application path and append the subdirectory:
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
http://www.csharp-examples.net/get-application-directory/
Just use the relative path to the application.
Unless your path begins with a (drive letter or back)slash¹, it is interpreted as relative to the current working directory. So "myfolder\\" would be a relative directory.
¹In MS-DOS, and emulated by cmd.exe, it's possible to have a path relative to the current directory on another drive.
const string subDir = "test_dir";
string appPath = Path.GetDirectoryName(Application.ExecutablePath);
string targetPath = Path.Combine(appPath, subDir);
Related
I am trying to open a Help.txt file in windows Forms using a linkLabel. However unable to convert from absolute to relative path.
First, I try to get the absolute path of the exe file. Which is successful.
Second, get only directory of the exe file. Which is successful.
Third, I am trying to combine the directory with the relative path of the Help.txt file. Which is unsuccessful.
Exe file lives in -> \Project\bin\Debug folder, However the Help.txt file lives in \Project\Help folder. This is my code:-
string exeFile = (new System.Uri(Assembly.GetEntryAssembly().CodeBase)).AbsolutePath;
string Dir = Uri.UnescapeDataString(Path.GetDirectoryName(exeFile));
string path = Path.Combine(Dir, #"..\..\Help\Help.txt");
System.Diagnostics.Process.Start(path);
The result of my path is -> \Project\bin\Debug....\Help\Help.txt
You need to use Path.GetFullPath() to have the upper directory "../../" taken into account, see below :
string exeFile = new System.Uri(Assembly.GetEntryAssembly().CodeBase).AbsolutePath;
string Dir = Path.GetDirectoryName(exeFile);
string path = Path.GetFullPath(Path.Combine(Dir, #"..\..\Help\Help.txt"));
System.Diagnostics.Process.Start(path);
Per the MSDN of GetFullPath : Returns the absolute path for the specified path string.
Whereas Path.Combine Combines strings into a path.
I am trying to get a folder path in my C drive that I did not want to hard code with my program.
I know that I can use string path = System.AppDomain.CurrentDomain.BaseDirectory; However its give me : C:\FACE\Camera\Camera\bin\Debug . Which I want to only have C:\FACE\ as I want to initialise something in the FACE folder but outside of my Camera folder. I do not wish to hardcode the file path. Is it possible to do it? Thanks for the help!
You could use the method Directory.GetParent for this purpose:
Directory.GetParent("here you will pass you path");
Update
For your case, you could take that you want as below:
string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
string solutionPath = Directory.GetParent(projectPath).Parent.FullName;
string basePath = Directory.GetParent(solutionPath).FullName;
The variable basePath contains that you want.
System.AppDomain.CurrentDomain.BaseDirectory always returns the path of executable assembly.You can use DTE if you are using web application
I need to identify first is this a UNCPath if so get the file directory
Is there a method to identify if a path is a UNC Path?
How do I get the file's parent.Parent.directory?
\\MyServer\\MySharedDrive\\MyDirectory\\MySubDirectory\\Myfile.csv
Wanted result and should work however deep
\\MyServer\\MySharedDrive\\MyDirectory
so that I can save another file to the above directory.
I guess I cannot do
Path.Combine("\\MyServer\\MySharedDrive\\MyDirectory",myNewFile.csv)
Any Suggestions?
Many thanks
To get parent directory and then creating a new path you can do:
string path = "\\MyServer\\MySharedDrive\\MyDirectory\\MySubDirectory\\Myfile.csv";
DirectoryInfo directory = new DirectoryInfo(Path.GetDirectoryName(path));
string finalPath = Path.Combine(directory.Parent.FullName, "myNewFile.csv"
To check if the path is UNC check this post.
From MSDN
Most members of the Path class do not interact with the file system
and do not verify the existence of the file specified by a path string
Again from MSDN on Path class
In members that accept a path, the path can refer to a file or just a
directory. The specified path can also refer to a relative path or a
Universal Naming Convention (UNC) path for a server and share name
Said that, you could write
string myUNCPath = #"\\MyServer\MySharedDrive\MyDirectory\MySubDirectory\Myfile.csv";
string myParent = Path.GetDirectoryName(Path.GetDirectoryName(myUNCPath));
string finalFile = Path.Combine(#"\\MyServer\MySharedDrive\MyDirectory","myNewFile.csv");
As a safety check you should execute two separate calls to Path.GetDirectoryName because if you have only one level deep of subdirectory then the result of Path.GetDirectoryName will be null
For example, if the initial UNC path is
string myUNCPath = #"\\MyServer\MySharedDrive\MyDirectory\Myfile.csv";
string myParent = Path.GetDirectoryName(myUNCPath);
if(!string.IsNullOrWhiteSpace(myParent))
{
myParent = Path.GetDirectoryName(myParent);
if(!string.IsNullOrWhiteSpace(myParent))
{
string finalFile = Path.Combine(myParent, "myNewFile.csv");
.....
}
}
For the part relative to your first question, the discovery of an UNC path is relatively easy.
See this article on Windows Dev Center about Paths and Files
Console.WriteLine(IsUNCPath(myUNCPath));
......
bool IsUNCPath(string pathToCheck)
{
return pathToCheck.StartsWith(#"\\");
}
I have a C# application which assume it runs from bin directory
string current_directory = Directory.GetCurrentDirectory(); //get current directory, this is the bin dir
string parent_dir = Directory.GetParent(current_directory).ToString();// this is parent of bin dir
string _Config1 = parent_dir + "\\config\\x.cfg";
string _Config2 = parent_dir + "\\config\\y.cfg";
string _Log = parent_dir + "\\log\\z.log";
The problem is for some reasons the user can not go to the bin directory and run the app (just type "application_name"). He has to run it using path (ie d:\blah\1\blah\2\blah\3\bin\application_name)
when he does this he gets
System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location)
so either I have to capture the path he uses to run the program and use it in my program or somehow make my application to be able to run using path.
You can use AppDomain.CurrentDomain.BaseDirectory.
Use the Application.StartupPath for WinForms.
You can use reflection:
var path = System.IO.Path.GetDirectoryName( System.Reflection.Assembly.GetExecutingAssembly().GetName().GetCodebase );
StreamReader content1 = File.OpenText("../DATA/heading.txt");
I have a txt file in a subfolder called DATA, I am trying to access this file from code but the code goes to the .net runtime directitory and not the application directory, thanks for the help
string filePath = Server.MapPath("/Data/heading.txt");
StreamReader content1 = File.OpenText(filePath);
Try using the Application's Entry assembly to get your text file path like this.
Assembly asm = Assembly.GetEntryAssembly();
string appDir = Path.GetDirectoryName(asm.Location);
string filePath = Path.Combine(appDir, "../DATA/heading.txt");
StreamReader content1 = File.OpenText(filePath);
This will work for any application that starts as an exe.
Since you marked this as asp.net are you looking on the server from asp.net? If so try Server.MapPath http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
From MSDN - http://msdn.microsoft.com/en-us/library/system.io.file.opentext.aspx
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
http://msdn.microsoft.com/en-us/library/system.io.directory.getcurrentdirectory.aspx
So your current directory is not set to your application directory.