华为andriod智能手机通讯录整理程序

完整程序下载: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();
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值