コード例 #1
0
        /// <summary>
        /// 文件下载
        /// </summary>
        /// <param name="id"></param>
        public void ExportTemplate(int id)
        {
            try
            {
                StruTemplateLibGeneral item = DataSource.Where(data => data.Id == id).SingleOrDefault();

                //文件地址
                string path = globalInfo.ProjectPath + @"\" + globalInfo.ProjectName + @".lcp";

                if (path == @"\.lcp")
                {
                    MessageBox.Show("选择工程后才允许下载!");
                    return;
                }
                //加载xml文件
                XmlDocument doc = new XmlDocument();
                doc.Load(path);


                XmlNode rootNode = doc.GetElementsByTagName("GeneralStruTemplate")[0];
                if (rootNode == null)
                {
                    XmlNode rootBase = doc.GetElementsByTagName("BaseData")[0];
                    rootNode = doc.CreateElement("GeneralStruTemplate");
                    rootBase.AppendChild(rootNode);
                }
                bool notExists = true;//是否已经存在该模板
                foreach (XmlNode xmlNode in rootNode.ChildNodes)
                {
                    if (xmlNode.Attributes.GetNamedItem("Name").InnerText == item.FileName)
                    {
                        notExists = false;
                        DialogResult dr = MessageBox.Show(string.Format("已经存在模板名称【{0}】相同的信息,是否替换?", item.FileName), "重复确认", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                        if (dr == DialogResult.OK)
                        {
                            xmlNode.Attributes.GetNamedItem("Name").InnerText      = item.FileName;
                            xmlNode.Attributes.GetNamedItem("TowerType").InnerText = item.Category;
                        }
                    }
                    break;
                }
                if (notExists)
                {
                    XmlElement row = doc.CreateElement("TowerTemplate");
                    row.SetAttribute("Name", item.FileName);
                    row.SetAttribute("TowerType", item.Category);
                    rootNode.AppendChild(row);

                    //下载具体文件
                    string NewFilePath = proUtils.GetGeneralTowerTemplatePath(item.FileName, item.Category);
                    if (File.Exists(NewFilePath))
                    {
                        File.Delete(NewFilePath);
                    }
                    using (FileStream fsWrite = new FileStream(NewFilePath, FileMode.Create, FileAccess.Write))
                    {
                        byte[] buffer = Encoding.Default.GetBytes(item.Content);
                        //使用utf-8编码格式
                        fsWrite.Write(buffer, 0, buffer.Length);
                    };
                }
                doc.Save(path);


                MessageBox.Show("下载成功!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format("下载失败,具体原因如下:{0}!", ex.Message));
            }
        }