C # Implementation of probeContentType in Java 7

Source: Internet
Author: User

A new method, probeContentType, is added to Java 7. Its main function is to determine the content type of a file. The Code is as follows:

import java.io.IOException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;public class FileContentType {    public static void main(String[] args) {                printContentType("D:/Downloads/java.txt");        printContentType("D:/Downloads/java.ppt");        printContentType("D:/Downloads/java.doc");        printContentType("D:/Downloads/java.avi");    }    private static void printContentType(String pathToFile) {                Path path = Paths.get(pathToFile);        String contentType = null;        try {            contentType = Files.probeContentType(path);        } catch (IOException e) {                  e.printStackTrace();        }        System.out.println("File content type is : " + contentType);    }}

There is no corresponding method in C #, but it is not difficult to implement the same function.

using System;using System.IO;using Microsoft.Win32; namespace Demo{    class Program    {        static void Main(string[] args)        {            PrintContentType("D:/Downloads/java.txt");            PrintContentType("D:/Downloads/java.ppt");            PrintContentType("D:/Downloads/java.doc");            PrintContentType("D:/Downloads/java.jar");             Console.ReadKey();        }         private static void PrintContentType(string pathToFile)        {            string result = string.Empty;            string ext = Path.GetExtension(pathToFile);            using (RegistryKey registryKey = Registry.ClassesRoot.OpenSubKey(ext))            {                if (registryKey != null)                {                    var value = registryKey.GetValue("Content Type");                    result = value == null ? "null" : value.ToString();                }            }            Console.WriteLine(result);        }    }}

Because the relevant content type information can be obtained from the Registry, you only need to perform some operations on the registry to achieve the same function.

Published in my blog at the same time

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.