Get file paths from a folder - c#

I am looking to get all the file paths from my images folder.
The file path in relation to my root is image/picture.jpg
I have seen the example of
foreach (var path in Directory.GetFiles(#"\image\"))
But this looks for a direct path, I am unsure as to how to use this code.
Can anyone suggest what I should be using?

Use Server.MapPath as so:
foreach (var path in Directory.GetFiles(Server.MapPath( "~/image")))
{
}
Server.MapPath returns the physical file path that corresponds to the specified virtual path on the Web server.

Related

Directory.EnumerateFiles not retrieving the actual path

Am trying to loop through a folder and read all the XML files within it.
Am using the Directory.EnumerateFiles to read from the path.
My .cs file and the XML files folder lies in same path
"C:\User\Documents\Projects\TestTool"
Am using the below code to get read the files.
string path = #"..\TestCases\";
foreach(string file in Directory.EnumerateFiles(path, "*.xml"))
{
}
Using this am getting exception stating
Could not find path "C:\Program Files(x86)\Common Files\Microsoft
Shared\DevServer\TestCases"
This is pointing to the WebDev.WebServer20 path instead of the actual path.
Not sure why it is pointing to a completely different folder
I tried string path = #"\TestCases\"; But when I try like this it is throwing an exceptiopn stating
Could not find path "C:\TestCases"
What is the mistake am making? Please help
Try it:
string path = "..\\TestCases\\";
foreach (FileInfo file in new DirectoryInfo(path).EnumerateFiles("*.xml"))
{
string filePath = file.FullName;
}
Since it is running under WebDev server it will using WebDev.WebServer20.exe's path for all relative paths.
You must use: Server.MapPath("~") to get to your own root.
string path = Server.MapPath("~") + #"\TestCases\";
foreach(string file in Directory.EnumerateFiles(path, "*.xml"))
{
}
Please note if Server.MapPath("~") points to bin, then use it like Directory.GetParent(Server.MapPath("~")) + #"\TestCases\";
Assuming this is your target directory C:\User\Documents\Projects\TestTool\TestCases

How to locate relative file path when using GetFiles() in C#?

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.*");

How to get a file full path in c# with Path.GetFullPath

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.

File.Delete isn't working to delete image from sub folder

In our ASP.NET program a user can upload an image to a folder. The location of the image (including the name of the upload folder which is in the root directory) is stored as a variable called "path", ie. "Uploads/fileName.jpg".
To remove the image:
if (File.Exists("~/" + path))
{
File.Delete("~/" + path);
}
However, it fails to run because it can't verify that the file exists. Through some testing we noticed it's looking for "path" in the "system32" directory. Why would this be?
You need to use Server.Map path to ensure that the Tilde is resolved correctly.
MSDN Article is here -> http://msdn.microsoft.com/en-us/library/ms524632(v=vs.90).aspx
Your code would become
var fixedPath = Server.MapPath("~/" + path);
if (File.Exists(fixedPath))
{
File.Delete(fixedPath);
}
The File class is not aware of the IIS directory mapping, so it won't understand ~ correctly. You have to first use a method to map the app relative path to a local path with Server.MapPath

Upload files to a specific folder in dropbox

I was using this example to upload files to dropbox:
https://github.com/geersch/DropboxRESTApi/blob/master/src/part-5/README.md
Which is:
public FileSystemInfo UploadFile(string root, string path, string file)
{
//...
}
The method has 3 parameters:
root: The root relative to which the path is specified. Valid values
are sandbox and dropbox.
path: The path to the file you want to
upload.
file: Absolute path to the local file you want to upload
Call this method as follows:
var file = api.UploadFile("dropbox", "photo.jpg", #"C:\Pictures\photo.jpg");
Dropbox API says exactly the same as the example:
https://www.dropbox.com/developers/core/docs#files-POST
So no mentioning of "parent folder"..
How can I upload files under a specific folder instead of the root?
cant you just do
var file = api.UploadFile("dropbox", #"myfolder\photo.jpg", #"C:\Pictures\photo.jpg");
I have the same issue.
I use this method for combining paths as the target directory
public static string CombinePath(string folder, string filename) => folder == "/" ? $"/{filename}" : $"{folder}/{filename}";
If you need to upload your file into the root directory the only parameter is / to identify the root.

Categories

Resources