Sie sind auf Seite 1von 4

Programming <3

By: Aaron and some random friends


C# (Programming Language)
C# (pronounced as see sharp) is a multi-paradigm programming
language encompassing strong typing, imperative, declarative, functional,
generic,
object-oriented
(class-based),
and
component-oriented
programming disciplines. It was developed by Microsoft within its .NET
initiative and later approved as a standard by Ecma (ECMA-334) and ISO
(ISO/IEC 23270:2006). C# is one of the programming languages designed for
the Common Language Infrastructure.
How to identify file extensions using C#
Most file names have a specific extension. A program may only need to
test for one file extension. It is ideal to use the Path.GetExtension, this
method is found in the base class library. This code extracts the extension.
The resulting extension includes the separator character, which is a dot.
Example:
Using System;
Class Program
{
static void Main()
{
string p = @"C:\Users\Sam\Documents\Test.txt";
if (IsTxtFile(p))
{
Console.WriteLine(".txt");
}
}
static bool IsTxtFile(string f)
{
return f != null &&
f.EndsWith(".txt", StringComparison.Ordinal);
}
}
Output:
.txt

Other method is converting your VB.NET lass to C# to detect mime


types. They will be identified by a mix of sniffing the first 256 Bytes of a file
and a set of known types.
Example (not all files types are listed here):
// check whether or not the uploaded file is an image:
var
contentType
MimeTypes.GetContentType(FileUpload1.PostedFile.FileName);
if(contentType.StartsWith("image"))
{
// do something with the image ...
}
Here's the class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.IO;

public class MimeTypes


{
private static List<string> knownTypes;
private static Dictionary<string, string> mimeTypes;
[DllImport("urlmon.dll", CharSet = CharSet.Auto)]
private static extern UInt32 FindMimeFromData(
UInt32 pBC, [MarshalAs(UnmanagedType.LPStr)]
string pwzUrl, [MarshalAs(UnmanagedType.LPArray)]
byte[] pBuffer, UInt32 cbSize, [MarshalAs(UnmanagedType.LPStr)]
string pwzMimeProposed, UInt32 dwMimeFlags, ref UInt32
ppwzMimeOut, UInt32 dwReserverd
);
public static string GetContentType(string fileName)
{
if (knownTypes == null || mimeTypes == null)
InitializeMimeTypeLists();
string contentType = "";
string extension = System.IO.Path.GetExtension(fileName).Replace(".",
"").ToLower();
mimeTypes.TryGetValue(extension, out contentType);
if
(string.IsNullOrEmpty(contentType)
||
knownTypes.Contains(contentType))
{
string headerType = ScanFileForMimeType(fileName);

if (headerType
string.IsNullOrEmpty(contentType))
contentType = headerType;
}
return contentType;
}

!=

"application/octet-stream"

||

private static string ScanFileForMimeType(string fileName)


{
try
{
byte[] buffer = new byte[256];
using (FileStream fs = new FileStream(fileName, FileMode.Open))
{
int readLength = Convert.ToInt32(Math.Min(256, fs.Length));
fs.Read(buffer, 0, readLength);
}
UInt32 mimeType = default(UInt32);
FindMimeFromData(0, null, buffer, 256, null, 0, ref mimeType, 0);
IntPtr mimeTypePtr = new IntPtr(mimeType);
string mime = Marshal.PtrToStringUni(mimeTypePtr);
Marshal.FreeCoTaskMem(mimeTypePtr);
if (string.IsNullOrEmpty(mime))
mime = "application/octet-stream";
return mime;
} catch (Exception ex)
{
return "application/octet-stream";
}
}
private static void InitializeMimeTypeLists()
{
knownTypes = new string[] {
"text/insert file type"
}.ToList();
mimeTypes = new Dictionary<string, string>();
mimeTypes.Add("3dm", "x-world/x-3dmf");
}
}
You have to update this list when a mime type is unknown and it is
not sufficient to know that it is application/octet-stream. It might suffice to

check if an extension is txt but the mime-type is binary data, because that
means that this file is not what it pretends to be.

Das könnte Ihnen auch gefallen