Get file name from URI string in C#
You can just make a System.Uri object, and use IsFile to verify it's a file, then Uri.LocalPath to extract the filename.
This is much safer, as it provides you a means to check the validity of the URI as well.
Edit in response to comment:
To get just the full filename, I'd use:
Uri uri = new Uri(hreflink);
if (uri.IsFile) {
string filename = System.IO.Path.GetFileName(uri.LocalPath);
}
This does all of the error checking for you, and is platform-neutral. All of the special cases get handled for you quickly and easily.
How to extract file name from an Uri in C#?
How about
Path.GetFileName(string path);
In your case
Path.GetFileName(new Uri("http://audacity.googlecode.com/files/audacity-win-2.0.exe").AbsolutePath);
Getting Filename from url in C#
use this: Uri.AbsolutePath
Request.Url.AbsolutePath
how to get filename from URL without downloading file c#
WebClient will not support it but with HttpWebRequest you can either try to be nice and send a HEAD request if the server supports it or if it doesn't send a normal GET request and just don't download the data:
The HEAD request:
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(uri);
request.Method = "HEAD";
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string disposition = response.Headers["Content-Disposition"];
string filename = disposition.Substring(disposition.IndexOf("filename=") + 10).Replace("\"", "");
response.close();
If the server doesn't support HEAD, send a normal GET request:
HttpWebRequest request = (HttpWebRequest)System.Net.WebRequest.Create(uri);
HttpWebResponse response = (HttpWebResponse) request.GetResponse();
string disposition = response.Headers["Content-Disposition"];
string filename = disposition.Substring(disposition.IndexOf("filename=") + 9).Replace("\"", "");
response.close();
How to get real filename from a url in C#?
I have found a good solution for it by myself.
public static string getFileName(string url)
{
HttpWebRequest req = RequestSender.SendRequest(url);
HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
string header_contentDisposition = resp.Headers["content-disposition"];
string escaped_filename = new ContentDisposition(header_contentDisposition).FileName;
return Uri.UnescapeDataString(escaped_filename);
}
So finally, the filename is correct. Thanks everyone tried to help.
How to get FileName from url
12-8536_2.jpg does not seem to be a file in that URL. In any case, if the "filename" in the URL will always be in .jpg, you can output the URL to a string (or AS a string) and Regex for it:
string filename = Regex.Match(URL,@"\/([A-Za-z0-9\-._~:?#\[\]@!$%&'()*+,;=]*).jpg").Groups[1].Value
EDIT: I'm thinking this is for a site with different preview sizes for a specific file. You can also specify the different possible extensions as follows (for example):
string filename = Regex.Match(URL,@"\/([A-Za-z0-9\-._~:?#\[\]@!$%&'()*+,;=]*)(.jpg|.JPG|.jpeg|.JPEG)").Groups[1].Value
How To Download HTML File Name As A String?
Explanation about the error:
WebClient.DownloadFileAsync
public void DownloadFileAsync(
Uri address,
string fileName
)
It doesn't return anything (void) and you are passing this to listsext! Ofcourse you will get an Exception
:
Cannot implicitly convert type 'void' to 'string'
What you want to achieve: (my guess, not much information given)
You want to have the filename, you don't need to download it.
Uri u = new Uri("http://rotter.net/scoopscache.html")
filename = Path.GetFileName(u.LocalPath);
See here: Get file name from URI string in C#
Related Topics
Linq Expression to Return Property Value
How to Find Out If a File Exists in C#/.Net
Linq Order by Null Column Where Order Is Ascending and Nulls Should Be Last
How to Get the Current User Directory
Combobox- Selectionchanged Event Has Old Value, Not New Value
Why Explicit Implementation of a Interface Can Not Be Public
Mono Https Webrequest Fails with "The Authentication or Decryption Has Failed"
Asynchronous Task.Whenall with Timeout
Why Isn't My Public Property Serialized by the Xmlserializer
Obtain Sha-256 String of a String
Command to Close an Application of Console
Wpf C# Path: How to Get from a String with Path Data to Geometry in Code (Not in Xaml)
How to Serialize an Exception Object in C#
How to Make a Hyperlink Work in a Richtextbox
Pulling Data from a Webpage, Parsing It for Specific Pieces, and Displaying It
How to Merge 2 List<T> and Removing Duplicate Values from It in C#
How to Convert Datetime To/From Specific String Format (Both Ways, E.G. Given Format Is "Yyyymmdd")