Get the image file path - c#

HI,
I am trying to retrive the filname of the image file from a file path in my code.
My filepath: c:\mydocuments\pictures\image.jpg
which method can i use in c# to get he filename of the above mentioned path.
Like String file = image.jpg
I have used the system.drawing to get he path, but it returns null.
my code:
string file = System.drawing.image.fromfile(filepath,true);
Is this the right way to get the image file name or is there any other inbuilt method in c#.
Pls help me on this?

System.IO.FileInfo info = new System.IO.FileInfo(filepath);
string fileName = info.Name;

Use Path.GetFileName

Image.FromFile returns an Image object, not the filename.
If you just want to get the filename portion of a path, use System.IO.Path.GetFileName(path)

Related

Could not find a part of the path while using File.Copy c# .net 4.7.2

I'm using the following code just to copy a file from a smaller filepath into a longer file path (> 260 characters).
string dbCDataPath = Path.Combine(DYRECTORY_WITH_GUARANTEE_ACCESS,Path.GetFileName(pathFileName));
string targetFile = Path.Combine(Path.GetDirectoryName(pathFileName), Path.GetFileName(pathFileName));
File.Copy(dbCDataPath, targetFile, true);
I'm getting Could not find a part of the path error and I don't know why I have double checked both the source and the destination folders, both exists.
Any help will be highly appreciated.
You need to call File.Copy like this:
File.Copy(targetFile, dbCDataPath, true);
The first parameter of this method is sourceFile you want to copy, the second parameter is destination path.

Get path without file name save dialog

I would like to extract the selected path from Save dialog, excluding the file name.
The following code retrieves the full path + the file name with its extension.
placeToSaveDocument = Path.GetFullPath(saveFileDialog.FileName);
Please do not suggest to use Folder Browser Dialog instead, because I have a reason why not to use it.
Any ideas?
You're probably looking for Path.GetDirectoryName(saveFileDialog.FileName).
Example:
string filePath = #"C:\MyDir\MySubDir\myfile.ext";
string directoryPath = Path.GetDirectoryName(filePath);
//directoryPath = "C:\MyDir\MySubDir"

Unsupported format while using the correct format

Im getting a "NotSupportedExeption was unhandled by user code - the specified path format is not supported" error, even thou I use a string as is requered.
string path = folder + "/" + filename;
fileByte = File.ReadAllBytes(path); // error here
any idea to what the problem is?
edited the code to this
string path = Path.Combine(folder, filename);
fileByte = File.ReadAllBytes(path);
the path is "F:\Web\Opgaver\Skirmer\Hjemmesiden\BETA\Skirmer 17-04-2012\Skirmer 17-04-2012\Billeder\Galleri\F:\Web\Opgaver\Skirmer\Hjemmesiden\BETA\Skirmer 17-04-2012\Skirmer 17-04-2012\Billeder\Galleri\2011\Vingsted\DSC_0001.JPG"
Error still happens. What I see is that ReadAllBytes requeres a string that shows the path, that I got, but it still shows error
You should not use / in the path, as the slash is an invalid character in Windows. Use Path.Combine to create it instead:
string path = Path.Combine(folder, filename);
I think that you want to use a backslash, or rather the property Path.DirectorySeparatorChar that returns the correct separator regardless of the file system:
string path = folder + Path.DirectorySeparatorChar.ToString() + filename;
Or you can use the Path.Combine method:
string path = Path.Combine(folder, filename);
What is the exact value of path variable?
Additionally, you should use Path.Combine to concatenate path parts into a full path.
As the documentation for File.ReadAllBytes states:
NotSupportedException - path is in an invalid format.
Your path is not in the correct format:
NotSupportedException path is in an invalid format.
MSDN: system.io.file.readallbytes
If the path you posted in your edited question really is the path you are trying read from then the reason you are getting the exception is because you have two colons in the path. The drive letter is repeated twice (F:\...F:\...).
The reason you end up with that path depends exactly on the contents of folder and filename in your call to Path.Combine(). It is unlikely that both folder and filename both start with the full path since Path.Combine() will return filename as the combined path in that case. It is most likely that your folder variable already contains two copies of the base path, with two drive letters and hence two colons and therefore causing the NotSupportedExeption, before you call Path.Combine().

how to get path of the file to be deleted from the server

i want to delete a file present in directory on the server. I have tried following code but code inside the file.exist never runs. It always skips it showing me that file does not exist. But file is present. can smone please help me. Thanx in advance
string filename = "Template\\copy.jpg";
if(System.IO.File.Exists(filename))
{
System.IO.File.Delete(filename);
}
Try
string fileName = Server.MapPath(#"/Template/copy.jpg");
You can use Server.MapPath to get the path. e.g.
string filename = Server.MapPath("~/Template/copy.jpg");
System.IO.File.Delete(filename);

what is the servlet equivalent of Server.MapPath?

I've got a folder in my Web Application, fonts. I'd like to get the path for each of those files in that directory. How do I do that? In asp.net I'd do something like:
System.IO.Directory.GetFiles(Server.MapPath("/fonts"))
String path = ServletContext.getRealPath("/fonts");
Javadoc.
You can use method getResourcePaths(String path) from ServletContext class for this purpose. It will return Set with directory-style listing of resources for specified (web-application mapped) path.
If you want to read content of file specified by mapped path, you can use method getResourceAsStream() from ServletContext returning InputStream for specified resource.
java.io.File dir = new java.io.File("/fonts");
String[] files = dir.list();

Categories