How to find the extension of a file in C#?
Path.GetExtension
string myFilePath = @"C:\MyFile.txt";
string ext = Path.GetExtension(myFilePath);
// ext would be ".txt"
Get File Extension C#
You can use Directory.EnumerateFiles()
like this:
string path = "F:\\WorkingCopy\\files\\";
string filename = ddl.SelectedItem.Text;
string existingFile = Directory.EnumerateFiles(path, filename + ".*").FirstOrDefault();
if (!string.IsNullOrEmpty(existingFile))
Console.WriteLine("Extension is: " + Path.GetExtension(existingFile));
Directory.EnumerateFiles
searches the path
for files like filename.*
. Path.GetExtension()
returns the extension of the found file.
In general, I prefer to use EnumerateFiles()
instead of GetFiles
because it returns an IEnumerable<string>
instead string[]
. This suggests that it only returns the matching files as needed instead searching all matching files at once. (This doesn't really matter in your case, just a general note).
How to know the extension of documents/files?
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");
Find a file with a certain extension in folder
Look at the System.IO.Directory
class and the static method GetFiles
. It has an overload that accepts a path and a search pattern. Example:
string[] files = System.IO.Directory.GetFiles(path, "*.txt");
A quick way to search for file extensions in C#
Fastest would be:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
p.StartInfo.Arguments = "/c dir c:\*.iso /s /b";
p.Start();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
After this you can split the output using \n
This is telling to search all ISO files in directory+all sub-directories and give only path+files names.
You can also use:
Directory.GetFiles(@"c:\", "*.iso", SearchOption.AllDirectories)
Change File Extension Using C#
There is: Path.ChangeExtension
method. E.g.:
var result = Path.ChangeExtension(myffile, ".jpg");
In the case if you also want to physically change the extension, you could use File.Move
method:
File.Move(myffile, Path.ChangeExtension(myffile, ".jpg"));
FileUpload get file extension
"Path" am I missing a using statement?
You have to add
using System.IO;
to the list of namespaces
Related Topics
Simplest Way to Do a Fire and Forget Method in C# 4.0
Using Case/Switch and Gettype to Determine the Object
How to Backup and Restore the System Clipboard in C#
Validation Error Style in Wpf, Similar to Silverlight
Mysqlcommand Command.Parameters.Add Is Obsolete
Admin Rights for a Single Method
Internet Explorer Protective Mode Setting and Zoom Levels
How to Execute an Ssis Package from .Net
Are There Any Suggestions for Developing a C# Coding Standards/Best Practices Document
Why Is Inserting Entities in Ef 4.1 So Slow Compared to Objectcontext
What Is the Best Scripting Language to Embed in a C# Desktop Application
How to Have an Enum Bound Combobox with Custom String Formatting for Enum Values
How to Find a Specific Element in a List<T>
Verifying That a String Contains Only Letters in C#