C#生成vcf 文件源代码 QuotedPrintable 解码支持中文支持Android-vcf文件怎么打开

需要需要生成vcf 联系人通讯录,想到最快的是C# 做桌面应用程序,搜索了一下发现 contacts.vcf 文件其实就是文本,可以用记事本打开,格式如下

BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:Ben
TEL;CELL:123546
END:VCARD

如果是多个就一直这样排列下去;

 public static void CreateCard(string name,string phoneNumber,bool isUtf8Encode)
{
String fileName = string.Format(@"D:/{0}_{1}.vcf", name, isUtf8Encode ? "Android":"windows");
FileStream fs = new FileStream(fileName, FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine("BEGIN:VCARD");
sw.WriteLine("VERSION:2.1");
sw.WriteLine("N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:" + EncodeQuotedPrintable(name, isUtf8Encode));
sw.WriteLine("TEL;CELL:"+ phoneNumber);
sw.WriteLine("END:VCARD");
sw.Flush();
sw.Close();
fs.Close(); ;
}

Quoted-Printable编码定义,其实很简单,就是将要编码的文字转换成byte字节流,然后使用十六进制的char型和“=”联合表达出来,如=0C表示12。下面提供编码及解码的方式:

 public static string EncodeQuotedPrintable(string value,bool isUtf8Encode)
{
if (string.IsNullOrEmpty(value))
return value;
StringBuilder builder = new StringBuilder();

byte[] bytes = isUtf8Encode? Encoding.UTF8.GetBytes(value): Encoding.Default.GetBytes(value);
foreach (byte v in bytes)
{
// The following are not required to be encoded:
// - Tab (ASCII 9)
// - Space (ASCII 32)
// - Characters 33 to 126, except for the equal sign (61).
if ((v == 9) || ((v >= 32) && (v <= 60)) || ((v >= 62) && (v <= 126)))
{
builder.Append(Convert.ToChar(v));
}
else
{
builder.Append('=');
builder.Append(v.ToString("X2"));
}
}
char lastChar = builder[builder.Length - 1];
if (char.IsWhiteSpace(lastChar))
{
builder.Remove(builder.Length - 1, 1);
builder.Append('=');
builder.Append(((int)lastChar).ToString("X2"));
}
return builder.ToString();
}



public static string DecodeQuotedPrintable(string input, string charSet)
{
Encoding enc;
try
{
enc = Encoding.GetEncoding(charSet);
}
catch
{
enc = new UTF8Encoding();
}
var occurences = new Regex(@"(=[0-9A-Z]{2}){1,}", RegexOptions.Multiline);
var matches = occurences.Matches(input);
foreach (Match match in matches)
{
try
{
byte[] b = new byte[match.Groups[0].Value.Length / 3];
for (int i = 0; i < match.Groups[0].Value.Length / 3; i++)
{
b[i] = byte.Parse(match.Groups[0].Value.Substring(i * 3 + 1, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
char[] hexChar = enc.GetChars(b);
input = input.Replace(match.Groups[0].Value, new String(hexChar));
}
catch
{; }
}
input = input.Replace("?=", "").Replace("\r\n", "");
return input;
}

由于Outlook 和Android 的解码方式不同,所以在编码的时候也需要注意,Android使用的是Utf-8, 而outlook 使用的是ascii,所以这里需要注意; utf8占用3个字节,ascii 占用两个字节;

下面是通过UTF-8生成出来 的

BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=E5=B0=8F=E6=9B=BE
TEL;CELL:13912341234
END:VCARD

下面是通过default (ASCII)生成出来 的

BEGIN:VCARD
VERSION:2.1
N;CHARSET=UTF-8;ENCODING=QUOTED-PRINTABLE:=D0=A1=D4=F8
TEL;CELL:13912341234
END:VCARD

C#生成vcf 文件源代码 QuotedPrintable 解码支持中文支持Android

完整源代码见CSDN 搜索 C# 生成VCF demo源代码,支持Android,outlook QuotedPrintable 中文编码

或者私信给我

推荐阅读