This question already has answers here:
How to find the extension of a file in C#?
(14 answers)
Closed 6 years ago.
I need to know the extension of different documents and depending of the extensions take some decisions within my code: for example I can have this FileNames:
commands to save.docx
Old Word.doc
rel.txt
test.pdf
I know that using the Contains() method that comes with Linq I could do something but I'm afraid that using Contains() it will take a .doc extension even if is a .docx extension because .doc is a substring of .docx
I think that a better approach would be maybe a regular expression for this... Any suggestion?
I've done something like this but maybe a RegEx should be a better approach:
if (fileName.Contains(".pdf"))
{
Response.AddHeader("Content-Type", "application/pdf; Content-Disposition, inline" + fileName);
}
You will find what you seek here, using Path.GetExtension
You may use the MimeMapping.GetMimeMapping method the mime type of the document. With that,you do not really need to get the file extension and write those if condition for all the different types.
var fileName = Path.GetFileName("SomeFileNameWithLongPath.pdf");
string mimeType= MimeMapping.GetMimeMapping(fileName );
If you really want the extension, you can use the Path.GetExtension method
var extension = Path.GetExtension("SomeFileNameWithLongPath.pdf");
Related
This question already has answers here:
What is the advantage of using Path.Combine over concatenating strings with '+'?
(5 answers)
Closed 5 years ago.
I recently learned about Path.Combine which combines two strings into a path, but I wonder what is, if any advantage of using Path.Combine compared to what we currently use in production which is something like the following:
var path = #":\somepath\";
var filename = postedFile.FileName;
var fullPath = path + filename;
Is it better going forwards to use Path.Combine(path, fileName)
Thanks
may be the question is a little bit academic but valid in my opinion, I think the designers/architects of .NET System.IO namespace wanted to provide the functionality of combining paths because it belongs to the logic of the IO namespace, also the combine hides the use of the '\' path control character, if .NET runs on another system where e.g. '|' is the path separator then your code will not work
Path.Combine uses the Path.PathSeparator and it checks whether the first path has already a separator at the end so it will not duplicate the separators. Additionally, it checks whether the path elements to combine have invalid chars.
Reference link: What is the advantage of using Path.Combine over concatenating strings with '+'?
To Answer your question is the shortest way possible: Yes.
The MSDN article is a good start to understand what Path.Combine actually does and doesn't do.
The mostly interesting part of Combine is that it will try to add separators when needed:
string disk = "c:";
string file= "text.txt";
string result= Path.Combine(disk,file);
//result will be c:\text.txt
This question already has answers here:
Best way to replace tokens in a large text template
(10 answers)
Closed 6 years ago.
I am dilemma to decide which one to use, either to use Regex.Replace or to use Regex.Matches if you have to perform some logic on each matches to generate the replaced value.
Scenario: Reading a file (which can vary in the size) and then using the Regular expression to replace the matches. replaced value for each match is different and is generated by some logic.
Approach 1: Read the complete file, then find all the matches and then I do the foreach or for loop and replace them one by one.
Approach 2: Read the complete file, then uses the Regex.Replace method with the MatchEvaluator, where MatchEvaluator function performs the logic and returns the replaced value.
There is an article I would like to link here, which somehow gives me a feeling to not use, Regex.Replace. Link: https://blogs.msdn.microsoft.com/debuggingtoolbox/2008/04/02/comparing-regex-replace-string-replace-and-stringbuilder-replace-which-has-better-performance/
Approach 1:
This would read entire file. (Check out for memory consumption.)
foreach loop on large data, (more time consuming.)
Approach 2:
This also would read entire file.
MatchEvaluator(pretty sure takes more time)
Approach 3:
Read the file line by line. MDSN Link
Do string.replace() as checked by the link you provided.
Append each result to result file at the same time.
This question already has answers here:
Directory.GetFiles of certain extension
(3 answers)
Closed 8 years ago.
I need to get all ASP files in a folder, so I wrote a code like this:
string[] files = Directory.GetFiles(#"C:\Folder", "*.asp", SearchOption.AllDirectories);
However, it also returns files with extension "aspx".
Is there a way to specify the end of the extension?
Sorry for my english and thanks in advance.
Is there a way to specify the end of the extension?
There isn't a way to do this directly. The best option would be to switch to Directory.EnumerateFiles and filter afterwards:
var files = Directory.EnumerateFiles(#"C:\Folder", "*.asp", SearchOption.AllDirectories)
.Where(f => f.EndsWith(".asp", StringComparison.OrdinalIgnoreCase));
This is because the Directory methods have specific behavior which prevents this from working directly. From the docs:
If the specified extension is exactly three characters long, the method returns files with extensions that begin with the specified extension. For example, "*.xls" returns both "book.xls" and "book.xlsx".
This is an exception to the normal search rules, but, in your case, is working against you. Using EnumerateFiles streams the results, and filtering afterwards allows you to find only the proper matches.
Unfortunately, i don't think there's a built in way. but
Directory.EnumerateFiles(#"C:\Folder", "*.asp", SearchOption.AllDirectories).Where(f => f.EndsWith(".asp")
should be as performant as a direct query would be. (note that EnumerateFiles returns an IEnumerable and is preferable to GetFiles if you don't need the files actually in an array)
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to parse a query string into a NameValueCollection in .NET
I have input as
https://localhost:8181/PortalSite/View/CommissionStatement.aspx?status=commission&quarter=1;
Output needed
status=commission
How to do in C#(preferably regular expression or something else)..
My solution
var res = src.Split('?')[1].Split('=')[1].Split["&"][0];
but failing in Split["&"]
If what you're going for is guaranteed to be a URL with a query string, I would recommend the HttpUtility.ParseQueryStringMethod.
var result = HttpUtility.ParseQueryString(new Uri(src).Query);
Note that in cases like this, the common fallacy is to just put together some string handling function that cannot deal with the full possible spec of input. In your case, there are a lot of valid URLs that are actually rather hard to handle/parse correctly. So you should stick to the already implemented, proven classes.
Thus, I would use the System.Uri class to consume the URL string. The part of the URL you're actually trying to access is the so called "query", which is also a property of the Uri instance. The query itself can easily and correctly be accessed as its individual key-value parts using the System.Web.HttpUtility.ParseQueryStringMethod() (you need to add System.Web.dll to your project's references and make sure you're not using the .NET 4 client profile for your application, as that will not include this assembly).
Example:
Uri u = new Uri("https://localhost:8181/PortalSite/View/CommissionStatement.aspx?status=commission&quarter=1;");
Console.WriteLine(u.Query); // Prints "status=commission&quarter=1;"
var parameters = HttpUtility.ParseQueryString(u.Query);
Console.WriteLine(parameters["status"]); // Prints "commission"
Once you have the "parameters" you could also iterate over them, search them, etc. YMMV.
If you require the output you show in your question, thus know that you always need the first parameter of the query string (and are not able to look it up by name as I show above), then you could use the following:
string key = parameters.GetKey(0);
Console.WriteLine(key + "=" + parameters[key]); // Prints "status=commission"
You could use the following regex: status=(\w*)
But I think there are better alternatives like using HttpUtility.ParseQueryStringMethod.
I know how to get all files that match the search pattern in a folder like this:
DirectoryInfo folderInfo = new DirectoryInfo(folderPath);
FileInfo[] fileInfos = folderInfo.GetFiles(searchPattern);
But my problem is, if I already know the file path, how can I match it with the search pattern? For compatibility, the search pattern has to be the same format like *.jpg or something.
I'm interpreting your question as meaning you already have a string for the file path and just want to check whether it matches a certain pattern. For this you first need to consider whether pattern patching is really what you need, it may be preferable to just use the extension directly for example.
Assuming that is not an option, you're probably going to want to use regular expressions. You will need to convert the string with wild-cards to a regular expression. Unfortunately I'm not aware of any built in ways of doing this, but it should be possible to do by simply escaping any characters which would have meaning in a regex and replacing the wild cards with appropriate regular expressions.
Well if you already have the full file path and you isolate the extension only, using Path.GetExtension or similar, then you just add a * before you are are set, right?
If u already know the path, Don't use search pattern.
Try
FileInfo fi = new FileInfo(FilePath);
you can use Directory.GetFiles like "Directory.GetFiles("d:/xxx/*.jpg")" to get string array of all match files, and then to open or handle the single file.