I was given a file name, and I should be able to read it from disk and send its contents over the network. I need to determine if the file is text or binary, so I know whether to use StreamReader or BinaryReader. Another reason I need to know the type of content is that if it is binary, I need to MIME encode the data before sending it over the wire. I would also like to tell the consumer what the type of content is (including the encoding if it is text).
Thanks!
.
, :
private static string GetContentTypeFromRegistry(string file) { RegistryKey contentTypeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type"); foreach (string keyName in contentTypeKey.GetSubKeyNames()) { if (System.IO.Path.GetExtension(file).ToLower().CompareTo((string)contentTypeKey.OpenSubKey(keyName).GetValue("Extension")) == 0) { return keyName; } } return "unknown"; } private static string GetFileExtensionFromRegistry(string contentType) { RegistryKey contentTypeKey = Registry.ClassesRoot.OpenSubKey(@"MIME\Database\Content Type\" + contentType); if (contentTypeKey != null) { string extension = (string)contentTypeKey.GetValue("Extension"); if (extension != null) { return extension; } } return String.Empty; }
, , . Stream.Write Stream.Read, , , ? . * Reader, .
You can try this free mime detector library. http://www.netomatix.com/Products/DocumentManagement/MimeDetector.aspx
Source: https://habr.com/ru/post/1703046/More articles:Configure Eclipse for the OA Framework? - eclipseКак разбить выпуклый многоугольник на правые треугольники, выровненные по осям X и Y? - mathSunStudio C ++ compiler editor to disable warnings? - warningsWhat is the preferred event handling method in C #? - c #Eclipse: Can a single JAR have more than one JavaDoc location? - eclipseПочему JRuby не распознает BigNums, пока Ruby делает? - javaExtend Membership API, MemberhipUser - securityfirefoxs displays thin gray lines at the edges of the contents of the png window at a certain width - why? - firefoxAdd or remove MDI form discovery with child form - c #Streamline file copying with multiple streams - performanceAll Articles