Get extension of file without providing extension in the path - c#

Is it possible to get the extension of a file but when you specify the entire path other than the extension? For example:
C:\Users\Administrator\Pictures\BlueHillsTest
Thanks

Directory.GetFiles will allow you to specify a wildcard for files to search:
System.IO.Directory.GetFiles(#"C:\temp\py\", "test.*")
for me, returns an array of 3 matching items. I expect an array, since the directory contains test.cover, test.py, and test.pyc.
If I use the First extension method:
System.IO.Directory.GetFiles(#"C:\temp\py\", "test.*").First()
then it only returns the first result (test.cover).
However, using the Single extension method:
System.IO.Directory.GetFiles(#"C:\temp\py\", "test.*").Single()
raises an InvalidOperationException because the "Sequence contains more than one element" (which might be what you want, depending on your circumstances).
But if I try
System.IO.Directory.GetFiles(#"C:\temp\py\", "step.*").Single()
then I get just step.py (no exception raised) because that's the only file matching step.* in that directory.

No it is not possible as you might have both BlueHillsTest.xxx and BlueHillsTest.yyy in this location. Which one do you expect to return in this case?

Related

How to get files names inside folder using regular expression in c#

I need to retrieve all files names inside a specific folder, but I need to use regular expression in which I need to get files names depends on a number in the file name. For example :
fitness-0Chromosom1
I mean zero in this case, I wrote the following:
//GetFiles on DirectoryInfo returns a FileInfo object.
var pdfFiles = new DirectoryInfo("C:/Users/Welcome/Desktop/Rounds/Fitness/AUV" + 1).GetFiles("fitness-"+geneticIteration+"*"+".txt");
Where geneticIteration is the number represent 0. Is it true or not?
Yes, your code above will return all file names matching "fitness-[a number]*.txt" in directory "C:/Users/Welcome/Desktop/Rounds/Fitness/AUV1".
Example results could be (given that "a number" = 0):
fitness-0Chromosom1.txt
fitness-0abc.txt
fitness-0.txt
GetFiles(path) also accepts the another parameter called 'searchPattern' where you can pass your regular expression. Another way to match the file name with your regular expression is using the Regex.IsMatch(path, pattern), which will return the true or false based on the comparison/match.

Multi File Selection using using Custom WPF OpenFileDialog

I'm using Custom WPF Dialog box to select one or more files.
Problem is that When you select more than a file, it'll throw Argumentexception("Illegal characters in path.") because of void OnPathChanged(IFileDlgExt sender, string pathName) method on FileDialogExt class. Reason for issue is that pathName for multiple file is like:
D:\Development\ "WpfCustomFileDialog.dll" "WpfCustomFileDialog.pdb"Which is not a valid parameter for System.IO.Path.GetFileName and it'll throw Argumentexception("Illegal characters in path.").
How can I fix this it?
Regards
It doesn't look like there's anything to fix, really, though it is a lousy return value. You just need to separate or parse your returned pathName and call GetFileName() with each actual name.
Assuming your D:\ line is a string, you should be able to .Split('"'). Iterate through the resulting array to .Trim() the errant whitespace and get rid of the now-empty entries (the spaces between quoted names).
If you then combine the first array element (the folder) with each other element (the file), those should be your valid file names.
If you're including the project as source, you might want to do this work inside the window, and return a List<String> or something, rather than that ugly string.

Filtered File Listing

i have a project to make and i want in option 2 the user to give an extension/keyword for example and the program to search the directory for this and print all the files that include this extension/keyword and i need some help if it's possible.
Then use another overload of the Directory.GetFiles() method which accepts a search pattern as a parameter.
Check it out on MSDN. And here is an example.
var files = Directory.GetFiles(#"c:\", "*.exe");
Obviously, replace that second argument with a variable name where you store an input from a user.
Or perhaps something like:
IEnumerable<string> results = filePaths.Where(x => x.EndsWith(".exe"));

Find file by name and unknown extension C#

Are there any clever ways to easily find a file by its name and unknown extension in C#?
I have a image folder with images of different types .jpg, .gif and .png.
And all the program knows is the name of the image, and not the extension.
Is it possible to get the file by its name, without doing some big recursive resource consuming loop?
You could use the EnumerateFiles method and specify a search pattern:
var files = Directory.EnumerateFiles(#"c:\work", "somefilename.*");
This will of course return an IEnumerable<string> of all files that match this search pattern. If you know there can only be one, or wanted to get the first in the list, just chain it with LINQ to further filter the results.
Yes, it is possible. Use overloaded Directory.GetFiles method which accepts search pattern as second parameter:
string[] files = Directory.GetFiles(#"c:\images", fileName + ".*");
Linq to rescue
var filenames = Directory.GetFiles(#"C:\Windows\System32").Select(f => Path.GetFileName(f)).Where(fn => fn.StartsWith("ap"));
Let linq do the looping and filtering for you
There is a couple of options:
Build your file index in advance (simple dictionary will do the
trick).
Use windows search API.
Use EnumerateFiles, altough performance wise this is similar to a bit optimized big recursion traverse.

Getting files in directory with *.tif file mask using C#

So, I feel lame for asking this, but I'm kinda stumped. I'm trying to get a list of file in a directory that end in tif ... only tif ... not tiff. So, I did this in C# ...
Directory.GetFiles(path, "*.tif", SearchOption.TopDirectoryOnly);
I would expect it to only return tif files, but that is not the case. I get tiff as well. I would think that if I supplied the mask .tif? that would get me both, but not the mask .tif. I tried it at a command prompt as well and I am getting both as well in DOS. Am I missing something here? This just seems wrong to me. I guess I could sanitize the results afterwards, but if I don't have to that would be best.
From MSDN:
When using the asterisk wildcard character in a searchPattern (for
example, "*.txt"), the matching behavior varies depending on the
length of the specified file extension. A searchPattern with a file
extension of exactly three characters returns files with an extension
of three or more characters, where the first three characters match
the file extension specified in the searchPattern. A searchPattern
with a file extension of one, two, or more than three characters
returns only files with extensions of exactly that length that match
the file extension specified in the searchPattern. When using the
question mark wildcard character, this method returns only files that
match the specified file extension. For example, given two files in a
directory, "file1.txt" and "file1.txtother", a search pattern of
"file?.txt" returns only the first file, while a search pattern of
"file*.txt" returns both files.
That's just how Directory.GetFiles works. From the manual:
When using the asterisk wildcard character in a searchPattern, such as
"*.txt", the matching behavior when the extension is exactly three
characters long is different than when the extension is more or less
than three characters long. A searchPattern with a file extension of
exactly three characters returns files having an extension of three or
more characters, where the first three characters match the file
extension specified in the searchPattern.
Directory.GetFiles internally uses FindFirstFile function from Win32 API.
From the documentation of FindFirstFile:
• The search includes the long and short file names.
A file that has long file name of asd.tiff will have a short file name like asd~1.tif and this is why it shows up in the results.
More than three character extensions are matched except when the path is on a network share (or mapped drive). For some reason the pattern only matches the long file name on remote drives.

Categories

Resources