2015-04-25 5 views
0

Я слежу за учебником Youtube по созданию списков и сохранению/загрузке их в xml-файлы. Тем не менее, я играл с ним, и я просто не могу показать, что у меня появляется диалоговое окно, и пусть пользователь выбирает местоположение и сам вводит имя файла. На данный момент это предопределенное имя и местоположение файла, которые будут записываться каждый раз, когда я его сохраню. Это не то, что я хочу.Запись в пользовательский XML-файл

Вот мой код:

private void button6_Click(object sender, EventArgs e) 
    { 
     string path = Directory.GetCurrentDirectory(); 

     if (!Directory.Exists(path + "\\TaskList")) // if the file directory doesnt exist.. 
      Directory.CreateDirectory(path + "\\TaskList"); // create the file directory 

     if (!File.Exists(path + "\\TaskList\\settings.xml")) // if the XML file doesnt exist.. 
     { 
      XmlTextWriter xW = new XmlTextWriter(path + "\\TaskList\\settings.xml", Encoding.UTF8); // create the xml file 
      xW.WriteStartElement("TaskList"); 
      xW.WriteEndElement(); 
      xW.Close(); 
     } 

     // create XML document to write to 
     XmlDocument xDoc = new XmlDocument(); 
     xDoc.Load(path + "\\TaskList\\settings.xml"); 

     // create node for every property inside the taskProperties class 
     foreach (taskProperties newTask in task) 
     { 
      XmlNode nodeTop = xDoc.CreateElement("Task"); 
      XmlNode nodeTitle = xDoc.CreateElement("Title"); 
      XmlNode nodeDescription = xDoc.CreateElement("Description"); 
      XmlNode nodePriority = xDoc.CreateElement("Priority"); 
      XmlNode nodeCompletionDate = xDoc.CreateElement("CompletionDate"); 
      XmlNode nodeTaskComplete = xDoc.CreateElement("TaskComplete"); 

      nodeTitle.InnerText = newTask.title; 
      nodeDescription.InnerText = newTask.description; 
      nodePriority.InnerText = newTask.priority; 
      nodeCompletionDate.InnerText = newTask.completionDate.ToFileTime().ToString(); // convert to file time (numbers) then to a string 
      nodeTaskComplete.InnerText = newTask.taskComplete; 

      // add these nodes to the 'nodeTop' node 
      nodeTop.AppendChild(nodeTitle); 
      nodeTop.AppendChild(nodeDescription); 
      nodeTop.AppendChild(nodePriority); 
      nodeTop.AppendChild(nodeCompletionDate); 
      nodeTop.AppendChild(nodeTaskComplete); 
      // add the nodeTop to the document 
      xDoc.DocumentElement.AppendChild(nodeTop); 
     } 
     // save the document 
     xDoc.Save(path + "\\TaskList\\settings.xml"); 
    } 

Любая помощь на том, как я могу добиться этого будет весьма признателен.

ответ

3

Используйте это, чтобы открыть диалоговое окно «Сохранить файл» и получить местоположение, которое пользователь выбирает.

SaveFileDialog fdgSave= new SaveFileDialog(); 
fdgSave.InitialDirectory = Convert.ToString(Directory.GetCurrentDirectory()); 
fdgSave.Filter = "XML (*.XML)|*.xml|All Files (*.*)|*.*" ; 
fdgSave.FilterIndex = 1; 

if(fdgSave.ShowDialog() == DialogResult.OK) 
{ 
    Console.WriteLine(fdgSave.FileName);//Do what you want here 
} 
+0

Отлично. Спасибо! – Snowflake232

Смежные вопросы