如何在C#中找到文件的扩展名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1886866/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the extension of a file in C#?
提问by Surya sasidhar
In my web application (asp.net,c#) I am uploading video file in a page but I want to upload only flv videos. How can I restrict when I upload other extension videos?
在我的 Web 应用程序 (asp.net,c#) 中,我正在页面中上传视频文件,但我只想上传 flv 视频。上传其他扩展视频时如何限制?
采纳答案by James
string myFilePath = @"C:\MyFile.txt";
string ext = Path.GetExtension(myFilePath);
// ext would be ".txt"
回答by Mark Dickinson
At the server you can check the MIME type, lookup flv mime type here or on google.
在服务器上,您可以检查 MIME 类型,在此处或在 google 上查找 flv mime 类型。
You should be checking that the mime type is
您应该检查 mime 类型是否为
video/x-flv
If you were using a FileUpload in C# for instance, you could do
例如,如果您在 C# 中使用 FileUpload,您可以这样做
FileUpload.PostedFile.ContentType == "video/x-flv"
回答by Carra
I'm not sure if this is what you want but:
我不确定这是否是您想要的,但是:
Directory.GetFiles(@"c:\mydir", "*.flv");
Or:
或者:
Path.GetExtension(@"c:\test.flv")
回答by ZombieSheep
You will not be able to restrict the file type that the user uploads at the client side[*]. You'll only be able to do this at the server side. If a user uploads an incorrect file you will only be able to recognise that once the file is uploaded uploaded. There is no reliable and safe way to stop a user uploading whatever file format they want.
您将无法限制用户在客户端上传的文件类型[*]。您只能在服务器端执行此操作。如果用户上传了错误的文件,您只能在上传文件后才能识别。没有可靠和安全的方法来阻止用户上传他们想要的任何文件格式。
[*] yes, you can do all kinds of clever stuff to detect the file extension before starting the upload, but don't rely on it. Someone will get around it and upload whatever they like sooner or later.
[*] 是的,您可以在开始上传之前做各种聪明的事情来检测文件扩展名,但不要依赖它。迟早有人会绕过它并上传他们喜欢的任何内容。
回答by Marat Faskhiev
You can check .flv signature. You can download specification here:
您可以检查 .flv 签名。您可以在此处下载规范:
http://www.adobe.com/devnet/flv/
http://www.adobe.com/devnet/flv/
See "The FLV header" chapter.
请参阅“FLV 标头”一章。
回答by Noctis
In addition, if you have a FileInfo fi
, you can simply do:
此外,如果您有一个FileInfo fi
,您可以简单地执行以下操作:
string ext = fi.Extension;
and it'll hold the extension of the file (note: it will include the .
, so a result of the above could be: .jpg
.txt
, and so on....
并且它将保存文件的扩展名(注意:它将包含.
,因此上述结果可能是:.jpg
.txt
,等等....
回答by Mukul Yadav
You may simply read the stream of a file
您可以简单地读取文件流
using (var target = new MemoryStream())
{
postedFile.InputStream.CopyTo(target);
var array = target.ToArray();
}
First 5/6 indexes will tell you the file type. In case of FLV its 70, 76, 86, 1, 5.
前 5/6 个索引会告诉您文件类型。在 FLV 的情况下,它的70, 76, 86, 1, 5。
private static readonly byte[] FLV = { 70, 76, 86, 1, 5};
bool isAllowed = array.Take(5).SequenceEqual(FLV);
if isAllowed
equals true
then its FLV.
如果isAllowed
等于true
则其 FLV。
OR
或者
Read the content of a file
读取文件内容
var contentArray = target.GetBuffer();
var content = Encoding.ASCII.GetString(contentArray);
First two/three letters will tell you the file type.
In case of FLV its "FLV......"
前两个/三个字母会告诉您文件类型。
在 FLV 的情况下,它的“FLV ......”
content.StartsWith("FLV")
回答by avishay
This solution also helps in cases of more than one extension like "Avishay.student.DB"
此解决方案还有助于解决多个扩展名(如“Avishay.student.DB”)的情况
FileInfo FileInf = new FileInfo(filePath);
string strExtention = FileInf.Name.Replace(System.IO.Path.GetFileNameWithoutExtension(FileInf.Name), "");
回答by megamaiku
Found an alternate solution over at DotNetPerlsthat I liked better because it doesn't require you to specify a path. Here's an example where I populated an array with the help of a custom method
在DotNetPerls上找到了一个我更喜欢的替代解决方案,因为它不需要您指定路径。这是我在自定义方法的帮助下填充数组的示例
// This custom method takes a path
// and adds all files and folder names to the 'files' array
string[] files = Utilities.FileList("C:\", "");
// Then for each array item...
foreach (string f in files)
{
// Here is the important line I used to ommit .DLL files:
if (!f.EndsWith(".dll", StringComparison.Ordinal))
// then populated a listBox with the array contents
myListBox.Items.Add(f);
}
回答by Papun Sahoo
string FileExtn = System.IO.Path.GetExtension(fpdDocument.PostedFile.FileName);
The above method works fine with the firefox and IE , i am able to view all types of files like zip,txt,xls,xlsx,doc,docx,jpg,png
上述方法适用于 Firefox 和 IE,我可以查看所有类型的文件,如 zip、txt、xls、xlsx、doc、docx、jpg、png
but when i try to find the extension of file from googlechrome , i failed.
但是当我尝试从 googlechrome 找到文件的扩展名时,我失败了。