August 2013 Blog Posts

流之间的拷贝方法

MitchellChu 2013-08-29 .NET技术

方法一:在 4.5以上,可以使用CopyToAsync方法来完成拷贝。该方法使用请参考:CopyToAsync方法

方法二:在.NET 4.0,可以直接使用Stream.CopyTo(stream)的方法。

// SourceStream 为要拷贝的流
// DestinationStream 为要拷贝流的目标

SourceStream.CopyTo(DestinationStream);

 方法三:在.NET 3.5及以前的版本中,要在Stream和Stream之间,需要自己手动完成。

public static void CopyStream(Stream source, Stream destination)
{
    byte[] buffer = new byte[4096]; //你也可以设置的更大一些
    int read;
    while ((read = source.Read(buffer, 0, buffer.Length)) > 0)
    {
        destination.Write (buffer, 0, read);
    }
}

HttpWebRequest请求中使用安全证书的方法

MitchellChu 2013-08-27 .NET技术

中,我们会使用或HttpWebResponse来发送一个客户端数字证书,具体的方法:

方法一:使用X509Certificate证书类读取数字证书文件 (*.cer),之后将证书附加到请求()当中去:

public void GetResponseString(string uri)
{
	try
	{ 
		X509Certificate Cert = X509Certificate.CreateFromCertFile("C:\\证书.cer"); //证书存放的绝对路径
		ServicePointManager.CertificatePolicy = new CertPolicy(); //处理来自证书服务器的错误信息
		HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(uri);//要访问的像https://要访问的地址/mitchellchu/test.aspx,要用SSL访问的地址
		Request.ClientCertificates.Add(Cert);
		Request.UserAgent = "Mitchell Chu robot test"; // 使用的客户端,如果服务端没有要求可以随便填写
		Request.Method = "GET"; // 请求的方式:POST/GET
		using(HttpWebResponse Response = (HttpWebResponse)Request.GetResponse()) //获取Response
		{
			using(StreamReader sr = new StreamReader(Response.GetResponseStream(), Encoding.Default))
			{
				int count;
				char [] ReadBuf = new char[1024];
				do
				{
					count = sr.Read(ReadBuf, 0, 1024);
					if (0 != count)
					{
						Console.WriteLine(new string(ReadBuf));
					}
				}while(count > 0);
			}
		}
	}
	catch(Exception e)
	{
		Console.WriteLine(e.Message);
	}
		
}


class CertPolicy: ICertificatePolicy
{
	public bool CheckValidationResult(ServicePoint srvPoint
		, X509Certificate certificate, WebRequest request, int certificateProblem)
	{
		// 你可以在这里加上证书检验的方法,错误值可以在WinError.h中获得
		// 这里只是简单的返回true,任何证书都可以正常的使用。
		return true;
	}
}

 优点:简单方便

缺点:需要,如果没有,需要先导出证书文件方可使用。

 

方法二: 使用 CryptoAPI 调用,之后获取证书集合,找到需要使用的证书,以X509Certificate对象形式返回,并附加在请求(HttpWebRequest 或)中发送证书。

/*
该案例来源于微软官方网站
@MitchellChu
*/
using System;
using System.Net;
using System.IO;
using System.Text;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Runtime.InteropServices;

namespace SelectClientCert
{
	class MyCerts{

		private static int CERT_STORE_PROV_SYSTEM = 10;
		private static int CERT_SYSTEM_STORE_CURRENT_USER = (1 << 16);
		///private static int CERT_SYSTEM_STORE_LOCAL_MACHINE = (2 << 16);

		#region 引入扩展方法
		[DllImport("CRYPT32", EntryPoint="CertOpenStore", CharSet=CharSet.Unicode, SetLastError=true)]
		public static extern IntPtr CertOpenStore(
			int storeProvider, int encodingType,
			int hcryptProv, int flags, string pvPara);

		[DllImport("CRYPT32", EntryPoint="CertEnumCertificatesInStore", CharSet=CharSet.Unicode, SetLastError=true)]
		public static extern IntPtr CertEnumCertificatesInStore(
			IntPtr storeProvider,
			IntPtr prevCertContext);

		[DllImport("CRYPT32", EntryPoint="CertCloseStore", CharSet=CharSet.Unicode, SetLastError=true)]
		public static extern bool CertCloseStore(
			IntPtr storeProvider,
			int flags);
		#endregion
		
		X509CertificateCollection m_certs;

		public MyCerts(){
			m_certs = new X509CertificateCollection();
		}

		public int Init()
		{
			IntPtr storeHandle;
			storeHandle = CertOpenStore(CERT_STORE_PROV_SYSTEM, 0, 0, CERT_SYSTEM_STORE_CURRENT_USER, "MY");
			IntPtr currentCertContext;
			currentCertContext = CertEnumCertificatesInStore(storeHandle, (IntPtr)0);
			int i = 0;
			while (currentCertContext != (IntPtr)0) 
			{
				m_certs.Insert(i++, new X509Certificate(currentCertContext));
				currentCertContext = CertEnumCertificatesInStore(storeHandle, currentCertContext);
			}
			CertCloseStore(storeHandle, 0);

			return m_certs.Count;
		}
		
		public X509Certificate this [int index]
		{
			get 
			{
				// Check the index limits.
				if (index < 0 || index > m_certs.Count)
					return null;
				else
					return m_certs[index];
			}
		}
	};
	class MyHttpResource
	{
		String m_url;

		public MyHttpResource(string url){
			m_url = url;
		}

		public void GetFile(){

			HttpWebResponse  result = null;

			try{
			
				HttpWebRequest req = (HttpWebRequest)WebRequest.Create(m_url);
				req.Credentials  = CredentialCache.DefaultCredentials;

				///Method1
				//req.ClientCertificates.Add(X509Certificate.CreateFromCertFile("D:\\Temp\\cert\\c1.cer"));
		
				///Method2
				///Uses interop services
				MyCerts mycert = new MyCerts();
				if(mycert.Init() > 0)
					req.ClientCertificates.Add(mycert[0]);

				result = (HttpWebResponse)req.GetResponse();
				
				Stream ReceiveStream = result.GetResponseStream();
				Encoding encode = System.Text.Encoding.GetEncoding("utf-8");

				StreamReader sr = new StreamReader( ReceiveStream, encode );
				Console.WriteLine("\r\nResponse stream received");

				Char[] read = new Char[256];
				int count = sr.Read( read, 0, 256 );

				Console.WriteLine("HTTP Response...\r\n");
				while (count > 0) 
				{
					String str = new String(read, 0, count);
					Console.Write(str);
					count = sr.Read(read, 0, 256);
				}

			} 
			catch(WebException e) 
			{
            
				Console.WriteLine("\r\nError:");
				#if (DEBUG)
					Console.WriteLine(e.ToString());
				#else		
					Console.WriteLine(e.Message); 				
				#endif

			} 
			finally 
			{
				if ( result != null ) {
					result.Close();
				}
			}
				
		}
	
	}

	class CertSample
	{
		static void Main(string[] args)
		{
			try
			{
				if (args.Length < 1)
				{
					Console.WriteLine("No url is entered to download, returning.\n");
					Console.WriteLine("Usage: CertSample <urltoget>\n");
					Console.WriteLine("  e.g: CertSample https://servername \n"); 

					return;
				}

				MyHttpResource hr = new MyHttpResource(args[0]);
				hr.GetFile();
			}
			catch(Exception e)
			{
				Console.WriteLine(e.ToString());
			}
			return;
		}
	}
}

优点:无需导出文件,直接在系统中查找需要的证书

缺点:需要引用系统方法

.NET获取文件的MIME类型(Content Type)

MitchellChu 2013-08-23 .NET技术

获取文件的MIME类型有很多方法,闲话少絮,直接进入主题,下面是使用C#获取文件的的各种方法。

第一种:这种获取MIME类型(Content Type)的方法需要在 4.5之后才能够支持,但是非常简单。

:
// 需要先行引入System.Web
// 之后可以直接使用MimeMapping类的静态方法获取文件的MIME类型

string file = @"mimetype.txt";
string contentType = MimeMapping.GetMimeMapping(file);
Console.WriteLine("{0}'s MIME TYPE:{1}", file, contentType);
// Console output:
/*
mimetype.txt's Mime Type: text/plain
*/

优点:方便快捷

缺点:只能在.NET 4.5之后使用

如果没有的MIME TYPE将会返回:

application/octet-stream

 

第二种:直接在系统中通过文件的后缀获取文件的

 

// 自定义一个方法
// 通过系统中的Content Type来获取MIME TYPE
public static string GetMimeType(string fileName)
{
    string mimeType = "application/unknown";
    string ext = Path.GetExtension(fileName).ToLower();
    RegistryKey regKey = Registry.ClassesRoot.OpenSubKey(ext);
    if (regKey != null && regKey.GetValue("Content Type") != null){
        mimeType = regKey.GetValue("Content Type").ToString();
    }
    return mimeType;
}

 优点:

1. 直接快速

2. 没有.NET的版本限制;

缺点:

1. 需要有访问注册表的权限;

2. 获取的Content Type范围受系统环境约束。

这个方法在没有找到对应的MIME Type时,返回的是application/unknow,当然,你也可以自己定义其他任何返回值。

 

第三种: 自己动手写代码,将MimeType写成Mapping类,需要的时候直接使用。

// 通过自己定义一个静态类
// 将所有的Content Type都扔进去吧
// 调用的时候直接调用静态方法即可。
public static class MimeMapping
{
    private static Hashtable _mimeMappingTable;

    private static void AddMimeMapping(string extension, string MimeType)
    {
        MimeMapping._mimeMappingTable.Add(extension, MimeType);
    }

    public static string GetMimeMapping(string FileName)
    {
        string text = null;
        int num = FileName.LastIndexOf('.');
        if (0 < num && num > FileName.LastIndexOf('\\'))
        {
            text = (string)MimeMapping._mimeMappingTable[FileName.Substring(num)];
        }
        if (text == null)
        {
            text = (string)MimeMapping._mimeMappingTable[".*"];
        }
        return text;
    }

    static MimeMapping()
    {
        MimeMapping._mimeMappingTable = new Hashtable(190, StringComparer.CurrentCultureIgnoreCase);
        MimeMapping.AddMimeMapping(".323", "text/h323");
        MimeMapping.AddMimeMapping(".asx", "video/x-ms-asf");
        MimeMapping.AddMimeMapping(".acx", "application/internet-property-stream");
        MimeMapping.AddMimeMapping(".ai", "application/postscript");
        MimeMapping.AddMimeMapping(".aif", "audio/x-aiff");
        MimeMapping.AddMimeMapping(".aiff", "audio/aiff");
        MimeMapping.AddMimeMapping(".axs", "application/olescript");
        MimeMapping.AddMimeMapping(".aifc", "audio/aiff");
        MimeMapping.AddMimeMapping(".asr", "video/x-ms-asf");
        MimeMapping.AddMimeMapping(".avi", "video/x-msvideo");
        MimeMapping.AddMimeMapping(".asf", "video/x-ms-asf");
        MimeMapping.AddMimeMapping(".au", "audio/basic");
        MimeMapping.AddMimeMapping(".application", "application/x-ms-application");
        MimeMapping.AddMimeMapping(".bin", "application/octet-stream");
        MimeMapping.AddMimeMapping(".bas", "text/plain");
        MimeMapping.AddMimeMapping(".bcpio", "application/x-bcpio");
        MimeMapping.AddMimeMapping(".bmp", "image/bmp");
        MimeMapping.AddMimeMapping(".cdf", "application/x-cdf");
        MimeMapping.AddMimeMapping(".cat", "application/vndms-pkiseccat");
        MimeMapping.AddMimeMapping(".crt", "application/x-x509-ca-cert");
        MimeMapping.AddMimeMapping(".c", "text/plain");
        MimeMapping.AddMimeMapping(".css", "text/css");
        MimeMapping.AddMimeMapping(".cer", "application/x-x509-ca-cert");
        MimeMapping.AddMimeMapping(".crl", "application/pkix-crl");
        MimeMapping.AddMimeMapping(".cmx", "image/x-cmx");
        MimeMapping.AddMimeMapping(".csh", "application/x-csh");
        MimeMapping.AddMimeMapping(".cod", "image/cis-cod");
        MimeMapping.AddMimeMapping(".cpio", "application/x-cpio");
        MimeMapping.AddMimeMapping(".clp", "application/x-msclip");
        MimeMapping.AddMimeMapping(".crd", "application/x-mscardfile");
        MimeMapping.AddMimeMapping(".deploy", "application/octet-stream");
        MimeMapping.AddMimeMapping(".dll", "application/x-msdownload");
        MimeMapping.AddMimeMapping(".dot", "application/msword");
        MimeMapping.AddMimeMapping(".doc", "application/msword");
        MimeMapping.AddMimeMapping(".dvi", "application/x-dvi");
        MimeMapping.AddMimeMapping(".dir", "application/x-director");
        MimeMapping.AddMimeMapping(".dxr", "application/x-director");
        MimeMapping.AddMimeMapping(".der", "application/x-x509-ca-cert");
        MimeMapping.AddMimeMapping(".dib", "image/bmp");
        MimeMapping.AddMimeMapping(".dcr", "application/x-director");
        MimeMapping.AddMimeMapping(".disco", "text/xml");
        MimeMapping.AddMimeMapping(".exe", "application/octet-stream");
        MimeMapping.AddMimeMapping(".etx", "text/x-setext");
        MimeMapping.AddMimeMapping(".evy", "application/envoy");
        MimeMapping.AddMimeMapping(".eml", "message/rfc822");
        MimeMapping.AddMimeMapping(".eps", "application/postscript");
        MimeMapping.AddMimeMapping(".flr", "x-world/x-vrml");
        MimeMapping.AddMimeMapping(".fif", "application/fractals");
        MimeMapping.AddMimeMapping(".gtar", "application/x-gtar");
        MimeMapping.AddMimeMapping(".gif", "image/gif");
        MimeMapping.AddMimeMapping(".gz", "application/x-gzip");
        MimeMapping.AddMimeMapping(".hta", "application/hta");
        MimeMapping.AddMimeMapping(".htc", "text/x-component");
        MimeMapping.AddMimeMapping(".htt", "text/webviewhtml");
        MimeMapping.AddMimeMapping(".h", "text/plain");
        MimeMapping.AddMimeMapping(".hdf", "application/x-hdf");
        MimeMapping.AddMimeMapping(".hlp", "application/winhlp");
        MimeMapping.AddMimeMapping(".html", "text/html");
        MimeMapping.AddMimeMapping(".htm", "text/html");
        MimeMapping.AddMimeMapping(".hqx", "application/mac-binhex40");
        MimeMapping.AddMimeMapping(".isp", "application/x-internet-signup");
        MimeMapping.AddMimeMapping(".iii", "application/x-iphone");
        MimeMapping.AddMimeMapping(".ief", "image/ief");
        MimeMapping.AddMimeMapping(".ivf", "video/x-ivf");
        MimeMapping.AddMimeMapping(".ins", "application/x-internet-signup");
        MimeMapping.AddMimeMapping(".ico", "image/x-icon");
        MimeMapping.AddMimeMapping(".jpg", "image/jpeg");
        MimeMapping.AddMimeMapping(".jfif", "image/pjpeg");
        MimeMapping.AddMimeMapping(".jpe", "image/jpeg");
        MimeMapping.AddMimeMapping(".jpeg", "image/jpeg");
        MimeMapping.AddMimeMapping(".js", "application/x-javascript");
        MimeMapping.AddMimeMapping(".lsx", "video/x-la-asf");
        MimeMapping.AddMimeMapping(".latex", "application/x-latex");
        MimeMapping.AddMimeMapping(".lsf", "video/x-la-asf");
        MimeMapping.AddMimeMapping(".manifest", "application/x-ms-manifest");
        MimeMapping.AddMimeMapping(".mhtml", "message/rfc822");
        MimeMapping.AddMimeMapping(".mny", "application/x-msmoney");
        MimeMapping.AddMimeMapping(".mht", "message/rfc822");
        MimeMapping.AddMimeMapping(".mid", "audio/mid");
        MimeMapping.AddMimeMapping(".mpv2", "video/mpeg");
        MimeMapping.AddMimeMapping(".man", "application/x-troff-man");
        MimeMapping.AddMimeMapping(".mvb", "application/x-msmediaview");
        MimeMapping.AddMimeMapping(".mpeg", "video/mpeg");
        MimeMapping.AddMimeMapping(".m3u", "audio/x-mpegurl");
        MimeMapping.AddMimeMapping(".mdb", "application/x-msaccess");
        MimeMapping.AddMimeMapping(".mpp", "application/vnd.ms-project");
        MimeMapping.AddMimeMapping(".m1v", "video/mpeg");
        MimeMapping.AddMimeMapping(".mpa", "video/mpeg");
        MimeMapping.AddMimeMapping(".me", "application/x-troff-me");
        MimeMapping.AddMimeMapping(".m13", "application/x-msmediaview");
        MimeMapping.AddMimeMapping(".movie", "video/x-sgi-movie");
        MimeMapping.AddMimeMapping(".m14", "application/x-msmediaview");
        MimeMapping.AddMimeMapping(".mpe", "video/mpeg");
        MimeMapping.AddMimeMapping(".mp2", "video/mpeg");
        MimeMapping.AddMimeMapping(".mov", "video/quicktime");
        MimeMapping.AddMimeMapping(".mp3", "audio/mpeg");
        MimeMapping.AddMimeMapping(".mpg", "video/mpeg");
        MimeMapping.AddMimeMapping(".ms", "application/x-troff-ms");
        MimeMapping.AddMimeMapping(".nc", "application/x-netcdf");
        MimeMapping.AddMimeMapping(".nws", "message/rfc822");
        MimeMapping.AddMimeMapping(".oda", "application/oda");
        MimeMapping.AddMimeMapping(".ods", "application/oleobject");
        MimeMapping.AddMimeMapping(".pmc", "application/x-perfmon");
        MimeMapping.AddMimeMapping(".p7r", "application/x-pkcs7-certreqresp");
        MimeMapping.AddMimeMapping(".p7b", "application/x-pkcs7-certificates");
        MimeMapping.AddMimeMapping(".p7s", "application/pkcs7-signature");
        MimeMapping.AddMimeMapping(".pmw", "application/x-perfmon");
        MimeMapping.AddMimeMapping(".ps", "application/postscript");
        MimeMapping.AddMimeMapping(".p7c", "application/pkcs7-mime");
        MimeMapping.AddMimeMapping(".pbm", "image/x-portable-bitmap");
        MimeMapping.AddMimeMapping(".ppm", "image/x-portable-pixmap");
        MimeMapping.AddMimeMapping(".pub", "application/x-mspublisher");
        MimeMapping.AddMimeMapping(".pnm", "image/x-portable-anymap");
        MimeMapping.AddMimeMapping(".png", "image/png");
        MimeMapping.AddMimeMapping(".pml", "application/x-perfmon");
        MimeMapping.AddMimeMapping(".p10", "application/pkcs10");
        MimeMapping.AddMimeMapping(".pfx", "application/x-pkcs12");
        MimeMapping.AddMimeMapping(".p12", "application/x-pkcs12");
        MimeMapping.AddMimeMapping(".pdf", "application/pdf");
        MimeMapping.AddMimeMapping(".pps", "application/vnd.ms-powerpoint");
        MimeMapping.AddMimeMapping(".p7m", "application/pkcs7-mime");
        MimeMapping.AddMimeMapping(".pko", "application/vndms-pkipko");
        MimeMapping.AddMimeMapping(".ppt", "application/vnd.ms-powerpoint");
        MimeMapping.AddMimeMapping(".pmr", "application/x-perfmon");
        MimeMapping.AddMimeMapping(".pma", "application/x-perfmon");
        MimeMapping.AddMimeMapping(".pot", "application/vnd.ms-powerpoint");
        MimeMapping.AddMimeMapping(".prf", "application/pics-rules");
        MimeMapping.AddMimeMapping(".pgm", "image/x-portable-graymap");
        MimeMapping.AddMimeMapping(".qt", "video/quicktime");
        MimeMapping.AddMimeMapping(".ra", "audio/x-pn-realaudio");
        MimeMapping.AddMimeMapping(".rgb", "image/x-rgb");
        MimeMapping.AddMimeMapping(".ram", "audio/x-pn-realaudio");
        MimeMapping.AddMimeMapping(".rmi", "audio/mid");
        MimeMapping.AddMimeMapping(".ras", "image/x-cmu-raster");
        MimeMapping.AddMimeMapping(".roff", "application/x-troff");
        MimeMapping.AddMimeMapping(".rtf", "application/rtf");
        MimeMapping.AddMimeMapping(".rtx", "text/richtext");
        MimeMapping.AddMimeMapping(".sv4crc", "application/x-sv4crc");
        MimeMapping.AddMimeMapping(".spc", "application/x-pkcs7-certificates");
        MimeMapping.AddMimeMapping(".setreg", "application/set-registration-initiation");
        MimeMapping.AddMimeMapping(".snd", "audio/basic");
        MimeMapping.AddMimeMapping(".stl", "application/vndms-pkistl");
        MimeMapping.AddMimeMapping(".setpay", "application/set-payment-initiation");
        MimeMapping.AddMimeMapping(".stm", "text/html");
        MimeMapping.AddMimeMapping(".shar", "application/x-shar");
        MimeMapping.AddMimeMapping(".sh", "application/x-sh");
        MimeMapping.AddMimeMapping(".sit", "application/x-stuffit");
        MimeMapping.AddMimeMapping(".spl", "application/futuresplash");
        MimeMapping.AddMimeMapping(".sct", "text/scriptlet");
        MimeMapping.AddMimeMapping(".scd", "application/x-msschedule");
        MimeMapping.AddMimeMapping(".sst", "application/vndms-pkicertstore");
        MimeMapping.AddMimeMapping(".src", "application/x-wais-source");
        MimeMapping.AddMimeMapping(".sv4cpio", "application/x-sv4cpio");
        MimeMapping.AddMimeMapping(".tex", "application/x-tex");
        MimeMapping.AddMimeMapping(".tgz", "application/x-compressed");
        MimeMapping.AddMimeMapping(".t", "application/x-troff");
        MimeMapping.AddMimeMapping(".tar", "application/x-tar");
        MimeMapping.AddMimeMapping(".tr", "application/x-troff");
        MimeMapping.AddMimeMapping(".tif", "image/tiff");
        MimeMapping.AddMimeMapping(".txt", "text/plain");
        MimeMapping.AddMimeMapping(".texinfo", "application/x-texinfo");
        MimeMapping.AddMimeMapping(".trm", "application/x-msterminal");
        MimeMapping.AddMimeMapping(".tiff", "image/tiff");
        MimeMapping.AddMimeMapping(".tcl", "application/x-tcl");
        MimeMapping.AddMimeMapping(".texi", "application/x-texinfo");
        MimeMapping.AddMimeMapping(".tsv", "text/tab-separated-values");
        MimeMapping.AddMimeMapping(".ustar", "application/x-ustar");
        MimeMapping.AddMimeMapping(".uls", "text/iuls");
        MimeMapping.AddMimeMapping(".vcf", "text/x-vcard");
        MimeMapping.AddMimeMapping(".wps", "application/vnd.ms-works");
        MimeMapping.AddMimeMapping(".wav", "audio/wav");
        MimeMapping.AddMimeMapping(".wrz", "x-world/x-vrml");
        MimeMapping.AddMimeMapping(".wri", "application/x-mswrite");
        MimeMapping.AddMimeMapping(".wks", "application/vnd.ms-works");
        MimeMapping.AddMimeMapping(".wmf", "application/x-msmetafile");
        MimeMapping.AddMimeMapping(".wcm", "application/vnd.ms-works");
        MimeMapping.AddMimeMapping(".wrl", "x-world/x-vrml");
        MimeMapping.AddMimeMapping(".wdb", "application/vnd.ms-works");
        MimeMapping.AddMimeMapping(".wsdl", "text/xml");
        MimeMapping.AddMimeMapping(".xap", "application/x-silverlight-app");
        MimeMapping.AddMimeMapping(".xml", "text/xml");
        MimeMapping.AddMimeMapping(".xlm", "application/vnd.ms-excel");
        MimeMapping.AddMimeMapping(".xaf", "x-world/x-vrml");
        MimeMapping.AddMimeMapping(".xla", "application/vnd.ms-excel");
        MimeMapping.AddMimeMapping(".xls", "application/vnd.ms-excel");
        MimeMapping.AddMimeMapping(".xof", "x-world/x-vrml");
        MimeMapping.AddMimeMapping(".xlt", "application/vnd.ms-excel");
        MimeMapping.AddMimeMapping(".xlc", "application/vnd.ms-excel");
        MimeMapping.AddMimeMapping(".xsl", "text/xml");
        MimeMapping.AddMimeMapping(".xbm", "image/x-xbitmap");
        MimeMapping.AddMimeMapping(".xlw", "application/vnd.ms-excel");
        MimeMapping.AddMimeMapping(".xpm", "image/x-xpixmap");
        MimeMapping.AddMimeMapping(".xwd", "image/x-xwindowdump");
        MimeMapping.AddMimeMapping(".xsd", "text/xml");
        MimeMapping.AddMimeMapping(".z", "application/x-compress");
        MimeMapping.AddMimeMapping(".zip", "application/x-zip-compressed");
        MimeMapping.AddMimeMapping(".*", "application/octet-stream");
    }
}

//需要调用的时候
string file = @"MimeType.txt";
string contentType = MimeMapping.GetMimeMapping(file);
Console。WriteLine("{0}'s MIME TYPE: {1}", file, contentType);
// ~~~~

优点:

1. MIME TYPE获取方便,无权限干扰;

2. 可以随时添加新的MIME类型(Content Type)。

缺点:

需要自己写代码。

常见地址中的英文单词简写对照表

MitchellChu 2013-08-19 万象漫谈

  在填写的时候,为了简洁,往往会使用简写,下面是常用的

中文名 英文 英文简写
Province Prov.
City  
County  
乡/镇

Township

 
District Dist.
村/里 Village Vil.
Neighborhood  
Road Rd.
Street St.
大道 Boulevard Blvd.
Section sec.
Lane Ln.
Alley Aly.
衖(台,xiang) Sub-Alley  
Number No.
Floor F
Room Rm.
East E.
西 West

W.

South

S.

North N.
First 1st
Second 2nd
Third 3rd
Fourth 4th
Fifth 5th

 

关于博主

  一枚成分复杂的网络IT分子,常年游弋于电子商务,属于互联网行业分类中的杂牌军。当前正在待业中...