//写入xml文件 public void WriteXml() { XElement doc = new XElement("VisualElements"); doc.Add(new XAttribute("ShowNameOnSquare150x150Logo", ShowNameOnSquare150x150Logo)); //这里用传入的string.Empty与传出的null相区分 if (Square150x150Logo != string.Empty) { doc.Add(new XAttribute("Square150x150Logo", Square150x150Logo)); } if (Square70x70Logo != string.Empty) { doc.Add(new XAttribute("Square70x70Logo", Square70x70Logo)); } if (Square44x44Logo != string.Empty) { doc.Add(new XAttribute("Square44x44Logo", Square44x44Logo)); } doc.Add(new XAttribute("ForegroundText", ForegroundText)); doc.Add(new XAttribute("BackgroundColor", BackgroundColor)); XNamespace ns = "http://www.w3.org/2001/XMLSchema-instance"; XElement root = new XElement("Application"); root.Add(new XAttribute(XNamespace.Xmlns + "xns", ns)); root.Add(doc); if (File.Exists(XmlPath)) { //如果存在,先将文件属性设为可见,不然无法写入 File.SetAttributes(XmlPath, FileAttributes.Normal); } root.Save(XmlPath); SystemHidenFile.HideFile(XmlPath); }
//生成样式文件 private void SaveStyle(object sender, EventArgs e) { string tileName = txtNameView.Text; if (cmbCommand.SelectedIndex == 1) { if (tileName == "我的网址") { new FormMyMessageBox("请更改你的磁贴名称").Show(); ReplaceName(null, null); return; } var di = new DirectoryInfo(urlFolder); foreach (var fi in di.GetFiles()) { string fileName = Path.GetFileNameWithoutExtension(fi.FullName); if (tileName == fileName) { string iniPath = urlFolder + "\\" + fileName + ".ini"; string urlPath = File.ReadAllText(iniPath, Encoding.Default); var mb = MessageBox.Show("已存在同名文件URL磁贴程序,确认覆盖?" + "\n链接:" + urlPath, "错误提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation); if (mb == DialogResult.OK) { File.Delete(fi.FullName); File.Delete(iniPath); } else { return; } } } ReplaceUrlFileName(); } TileXml tileXml = new TileXml(myExePath) { BackgroundColor = txtCodeView.Text, ForegroundText = (cmbFontColor.SelectedIndex == 1) ? "dark" : "light", ShowNameOnSquare150x150Logo = (!chkShowName.Checked) ? "off" : "on" }; string picFolder = tileXml.ExeFolder + MyName; string picPath = picFolder + "\\" + tileXml.ExeName + ".png"; if (cmbTileShowMode.SelectedIndex == 1) { if (chkUseEditedPic.Checked == true) { string shortPicPath = MyName + "\\" + tileXml.ExeName + ".png"; tileXml.Square150x150Logo = shortPicPath; tileXml.Square70x70Logo = shortPicPath; tileXml.Square44x44Logo = shortPicPath; } else { tileXml.Square150x150Logo = tileXml.ReadXml("Square150x150Logo"); tileXml.Square70x70Logo = tileXml.ReadXml("Square70x70Logo"); tileXml.Square44x44Logo = tileXml.ReadXml("Square44x44Logo"); } } else { tileXml.Square150x150Logo = string.Empty; tileXml.Square70x70Logo = string.Empty; tileXml.Square44x44Logo = string.Empty; } try { if (chkUseEditedPic.Checked == true) { Directory.CreateDirectory(picFolder); picEditedView.Image.Save(picPath, ImageFormat.Png); } if (Directory.Exists(picFolder)) { SystemHidenFile.HideFile(picFolder); } if (cmbTileShowMode.SelectedIndex == 0) { if (Directory.Exists(picFolder)) { if (File.Exists(picPath)) { File.Delete(picPath); //文件夹中没有文件时删除 if (Directory.GetDirectories(picFolder).Length <= 0 && Directory.GetFiles(picFolder).Length <= 0) { Directory.Delete(picFolder); } } } } tileXml.WriteXml(); Shortcut shortcut = new Shortcut(); if (cmbCommand.SelectedIndex == 0) { shortcut.CreateShortcut(myExePath, tileName, iconPath, iconIndex, false); } else { shortcut.CreateShortcut(myExePath, tileName, iconPath, iconIndex, true); } new FormMyMessageBox("已成功修改程序磁贴样式,请手动将其固定到开始屏幕").Show(); } catch (Exception) { new FormMyMessageBox("无法在程序目录写入样式文件\n可能为开启了自我保护的安全软件").Show(); } }