完整程序下载:http://download.csdn.net/detail/johnsuna/4992159
清除重复的电话通讯录,并写成XML文件,方便导入手机中。如果与腾讯手机管家等手机管理软件相结合,使用效果更佳。不过,由于时间原因,有些硬编码,你可以根据需要进行改进。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace ClearRepeatItems
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string fileName=string.Empty;
private void openFile_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fileName = openFileDialog1.FileName;
UpdateItems(fileName);
}
}
StringBuilder sb = new StringBuilder();
List<string> listNames = new List<string>();
string[] listNamesArray;
private void UpdateItems(string fileName)
{
try
{
using (StreamReader sr = new StreamReader(fileName, Encoding.Default))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (!listNames.Contains(line))
{
listNames.Add(line);
}
}
}
}
catch (Exception e)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(e.Message);
}
listNamesArray = listNames.ToArray();
listBox1.Items.AddRange(listNamesArray);
}
private void saveFile_Click(object sender, EventArgs e)
{
string newFileName = Path.GetFileNameWithoutExtension(fileName) + "_New." + Path.GetExtension(fileName);
FileStream fs = new FileStream(newFileName, FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw = new StreamWriter(newFileName, false, Encoding.Default);
for (int i = 0; i < listNamesArray.Length; i++)
{
sb.AppendLine(listNamesArray[i]);
}
sw.Write(sb.ToString());
sw.Close();
}
private void saveXml_Click(object sender, EventArgs e)
{
StringBuilder sbXml = new StringBuilder();
sbXml.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\" ?>");
sbXml.AppendLine("<Contacts>");
for (int i = 0; i < listNamesArray.Length; i++)
{
string name = listNamesArray[i].Split(',')[0];
string tel = listNamesArray[i].Split(',')[1];
sbXml.AppendLine("<Contact>");
sbXml.AppendLine("<Name>" + name + "</Name>");
sbXml.AppendLine("<Starred>0</Starred>");
sbXml.AppendLine("<PhoneList>");
sbXml.AppendLine("<Phone Type=\"2\">" + tel + "</Phone>");
sbXml.AppendLine("</PhoneList>");
sbXml.AppendLine("<Account value=\"0\">");
sbXml.AppendLine("<Name>Phone</Name>");
sbXml.AppendLine("<Type>com.android.huawei.phone</Type>");
sbXml.AppendLine("</Account>");
sbXml.AppendLine("<GroupList>");
sbXml.AppendLine("<GroupName>家庭</GroupName>");
sbXml.AppendLine("</GroupList>");
sbXml.AppendLine("</Contact>");
}
sbXml.AppendLine("</Contacts>");
string newFileName = Path.GetFileNameWithoutExtension(fileName) + "_New.xml";
FileStream fs = new FileStream(newFileName, FileMode.Append, FileAccess.Write, FileShare.Write);
fs.Close();
StreamWriter sw = new StreamWriter(newFileName, false, Encoding.UTF8);
sw.Write(sbXml.ToString());
sw.Close();
}
}
}