public void WirteFile() { string path = @"D:\杆塔项目\other\【0715】结构计算\Tower library"; DirectoryInfo root = new DirectoryInfo(path); DirectoryInfo[] dics = root.GetDirectories(); StruTemplateLibGeneral item; foreach (DirectoryInfo ParentInfo in dics) { foreach (FileInfo info in ParentInfo.GetFiles("*.dat")) { using (FileStream fsRead = new FileStream(info.FullName, FileMode.Open, FileAccess.Read)) { item = new StruTemplateLibGeneral(); byte[] buffer = new byte[fsRead.Length]; // int r = fsRead.Read(buffer, 0, buffer.Length); //返回本次实际读取到的有效字节数 item.FileName = Path.GetFileNameWithoutExtension(info.FullName); item.Category = ParentInfo.Name; item.FileExtension = info.Extension; item.Content = System.Text.Encoding.Default.GetString(buffer);//使用utf-8编码格式 struTemplateLibGeneralService.Add(item); } } } }
/// <summary> /// 文件查看 /// </summary> /// <param name="id"></param> public void CheckTemplate(int id) { StruTemplateLibGeneral item = this.DataSource.Where(data => data.Id == id).SingleOrDefault(); TowerTemplate template = new NewTowerTemplateReader(TowerTypeStringConvert.TowerStringToType(item.Category)).ReadContentStream(item.Content, item.FileName); StruTemplateEditViewModel model = ViewModelSource.Create(() => new StruTemplateEditViewModel(template, true)); model.CloseEditTemplateWindowEvent += CloseTemplateEditWindow; editWindow = new StruTemplateEditWindow(); editWindow.DataContext = model; editWindow.ShowDialog(); }
/// <summary> /// 查询所有信息 /// </summary> /// <returns></returns> public bool Add(StruTemplateLibGeneral item) { return(StruTemplateLibGeneralDb.Insert(item)); }
/// <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)); } }