Writing some code in C#, I was wondering if there was a way to get the correct path of a directoryinfo object?
Currently I have, for example, a directory such as:
DirectoryInfo dirInfo = new DirectoryInfo(pathToDirectory);
The issue is that if I want to get the path of that specific dirInfo object, it always returns the debug path (bin folder). If the original dirInfo object is referencing a directory in the D:\testDirectory path, then I want a way to get that path again somewhere else in the code instead of getting \bin\debug\testDirectory
Is there any way to do this?
Currently I am trying to get the path of dirInfo using Path:
Console.WriteLine("Path: " + Path.GetFullPath(dirInfo.ToString()));
Try this.
string pathToDirctory = "D:\\testDirectory";
DirectoryInfo dirInfo = new DirectoryInfo(pathToDirctory);
string path = dirInfo.FullName;
Console.WriteLine(path);
A DirectoryInfo represents a particular directory. When you create it, what directory it represents is dependent on the path you give it. If you give it an absolute path like c:\foo\bar\baz\bat, that's the directory you get. If, on the other hand, you give it a relative path, like foo\bar\baz\bat, the path is relative to the process' current working directory. By default, that is inherited from the process that spawned the current process. Visual Studio starts a debug session and sets the CWD of the process being debugged to its bin directory. So if you create a DirectoryInfo and give it a path like testDirectory, you will get a DirectoryInfo about [project-root]\bin\Debug\testDirectory.
If you want an absolute path, you'll have to specify that absolute path. There aren't any shortcuts.
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've found a few related questions but they're not working that well. The image name is a modified GUID like 3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0 but the extension isn't known (.jpg, .gif, ..etc). The GUID will be coming from a gridview so it's not a static string. Below is what I have but I'm having a difficult time getting the path to work correctly.
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(#"/Images");
MessageBox.Show(filePath.ToString());
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
Keep getting issues with the directory being invalid. Currently the files are stored on my c: drive.
How can I get the relative path without hardcoding it in? I was using DirectoryInfo(Server.MapPath("Images")); which worked temporarily then started giving this error System.ArgumentException: Second path fragment must not be a drive or UNC name. which seems to be from the path having the drive "C:" This doesn't seem to be a permanent solution once the site is launched though.
The actual path is C:\Website\Name\Images\3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0.jpg
Thanks!
You've used filePath as the first parameter to GetFiles, just use the wildcard and invoke the overload of GetFiles with one parameter.
filePath.GetFiles("_0.*");
The problem is that you are getting DirectoryInfo for "C:\Images".
You want to use Server.MapPath to get the physical path to the folder that is in your website (which could be anywhere on any drive).
Using the ~ means to start from the root of the running website.
So this should do the trick:
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(Server.MapPath("~/Images"));
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
I am trying to get the full path a file by its name only.
I have tried to use :
string fullPath = Path.GetFullPath("excelTest");
but it returns me an incorrect path (something with my project path).
I have read somewhere here a comment which says to do the following:
var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "excelTest.csv");
but I do not know where the file is saved , therefore I do not know its environment.
can someone help me how to get the full path of a file only by its name?
The first snippet (with Path.GetFullPath) does exactly what you want. It returns something with your project path because the program EXE file is located in the project\Bin\Debug path, which is therefore the "current directory".
If you want to search for a file on a drive, you can use Directory.GetFiles, which will recursively search for a file in a directory given a name pattern.
This returns all xml-files recursively :
var allFiles = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories);
http://msdn.microsoft.com/en-us/library/ms143316%28v=vs.100%29.aspx
http://msdn.microsoft.com/en-us/library/ms143448.aspx#Y252
https://stackoverflow.com/a/9830162/2196124
I guess you're trying to find file (like in windows search), right ?
I'd look into this question - you will find all files that has that string in their filename, and from there you can return full filepath.
var fileList = new DirectoryInfo(#"c:\").GetFiles("*excelTest*", SearchOption.AllDirectories);
And then just use foreach to do you manipulations, e.g.
foreach(string file in fileList)
{
// MessageBox.Show(file);
}
What you're looking for is Directory.GetFiles(), you can read up on it here. The gist of it is, you'll pass in the file path and the file name, and you'll get a string array back. In this instance, you can assume top level with C:\. It should be noted, that if nothing is found, the string array will be empty.
You have passed a relative file name to Path.GetFullPath. Microsoft documentation states:
If path is a relative path, GetFullPath returns a fully qualified path that can be based on the current drive and current directory. The current drive and current directory can change at any time as an application executes. As a result, the path returned by this overload cannot be determined in advance.
You cannot get the same full path name from a relative path unless your current directory is the same each time you invoke the function.
Hello I am attempting to copy a directory and its subdirectories from my D drive to a shared network but I continue getting the error as
Exception:
Could not find a part of the path '/Projects/08.ASP.NETProjects/ProjectName/'.
My C# copy code:
System.IO.DirectoryInfo directory = new System.IO.DirectoryInfo(D drive path);
var destinationpath = "file:///BZ0025BZV43/Projects/08.ASP.NETProjects/ProjectName/";
var uri = new Uri(destinationpath);
var destinationurl = uri.AbsolutePath;
foreach (System.IO.FileInfo mydirectory in directory.GetFiles())
mydirectory .CopyTo(destinationurl);
I am new to FileHandlers. Please help.
Try to use uri.LocalPath instead of uri.AbsolutePath.
Also, be aware that you are trying to copy a file into a directory. But you have to copy the file into another file of that directory. (Directories are files too, basically).
So to do that check if the target directory exists and create it when necessary. Then replace .copyTo(destinationUrl) with .copyTo(Path.Combine(uri.LocalPath, mydirectory.Name)) and you should be good to go. And please rename the variable mydirectory to file or something similar.
Also note that you are not copying the directory tree recursively. So if you want to recurse the tree, you have to check for subdirectories and copy the files in that hierarchy, too.
You can also check out this example: http://msdn.microsoft.com/de-de/library/bb762914(v=vs.110).aspx
iam trying to fill dropdown box feteching folder name from server but this code showing error.its working in local .but not working in server.can any one help on this
DirectoryInfo dirInfo = new DirectoryInfo(Server.MapPath(#"~\\*.***.***.**\Flextronics\Common\Surendra"));
// DirectoryInfo dirInfo = new DirectoryInfo("D:\\New Folder");
ddlModel.DataSource = dirInfo.GetDirectories();
ddlModel.DataBind();
Server.MapPath(#"~\\*.***.***.**\Flex... return virtual path and it works for you on local because you have physical path "D:\...."
So you will have to use Request.MapPath("~/....."); because it Maps the specified virtual path to a physical path.
the ~ symbol looks to the parent folder of your code file on your local machine
To access a UNC on a network drive, you need something similar to:
Server.MapPath("\\\\servername\\folder\\desiredfile.ext");
The "\\\\" escapes the characters and you need to do that to navigate correctly.
Directory does not like Server.MapPath. Hard code it in:
...new DirectoryInfo("stringUNCtoLoadFilesFrom");