【问题标题】:c# how to save an xml file in a different folder than app/bin/debug/c#如何将xml文件保存在与app/bin/debug/不同的文件夹中
【发布时间】:2018-04-25 09:43:24
【问题描述】:

我只是想知道是否有办法将包含图表数据的 XML 文件保存到特定文件夹中。我有一个名为 SAVED 的按钮,用于保存图表当前状态的 png 或 jpeg 文件,并保存此图表的 xml 文件以便稍后加载。问题是该文件默认进入 app/bin/debug/myXMLfile.xml

graph.SaveImage(save.FileName, System.Windows.Forms.DataVisualization.Charting.ChartImageFormat.Png);
graph.Serializer.Save(txtNom.Text + "_" + txtPrenom.Text + ".xml");

【问题讨论】:

  • 您应该可以通过指定路径将其保存在不同的文件夹中。 chart1.SaveImage("C:\\myfolder\\mychart.png", ChartImageFormat.Png);

标签: c# xml save


【解决方案1】:

你试过给“save”方法一个指定的路径吗??

前任

graph.Serializer.Save(C:\\txtNom.Text + "_" + txtPrenom.Text + ".xml");    

【讨论】:

    【解决方案2】:

    我试过了

    graph.Serializer.Save(save.FileName+".xml");
    

    但是我有一些像 myFile.png.xml 这样丑陋的东西,所以我只使用 substring 方法来获取没有 .png 扩展名的文件的 hte 名称,然后我添加 xml 扩展名。它给出了这样的东西

     graph.Serializer.Save(save.FileName.Substring(0,save.FileName.IndexOf('.')) + ".xml");
    

    【讨论】:

      【解决方案3】:

      这是一种使用通用函数提示用户输入每个文件的方法。您需要在表单的代码隐藏部分中添加对这些库的引用:

      using System.Drawing.Imaging;
      using System.IO;
      

      然后添加保存按钮的事件处理程序和提示用户完整文件路径的常用功能:

          private void btnSave_Click(object sender, EventArgs e)
          {
              string imageFilePath = null;
              string dataFilePath = null;
              ImageFormat format = System.Drawing.Imaging.ImageFormat.Png;
      
              try
              {
                  // Image data
                  imageFilePath = promptForFilePath("Save Chart image", "PNG Files (*.png)|*.png|JPEG Files (*.jpg,*.jpeg)|*.jpg;*.jpeg|BMP Files (*.bmp)|*.bmp|All Files (*.*)|*.*");
      
                  if (!String.IsNullOrWhiteSpace(imageFilePath))
                  {
                      switch (Path.GetExtension(imageFilePath).Replace(".", "").ToLower())
                      {
                          case "jpeg":
                          case "jpg": 
                                      format = ImageFormat.Jpeg;
                              break;
                          case "png": format = ImageFormat.Png;
                              break;
                          case "bmp": format = ImageFormat.Bmp;
                              break;
                      }
      
                      this.graph.SaveImage(imageFilePath, format);
                  }
      
                  // XML data
                  dataFilePath = promptForFilePath("Save Chart data","XML Files (*.xml)|*.xml|All Files (*.*)|*.*");
      
                  if (!String.IsNullOrWhiteSpace(dataFilePath)) this.graph.Serializer.Save(dataFilePath);
      
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error:  " + ex.Message, "btnSave_Click Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
              finally
              {
      
              }
          }
      
          string promptForFilePath(string title = "Save File", string fileFilter = "All Files (*.*)|*.*")
          {
              string result = null;
              SaveFileDialog saveFileDialog = null;
      
              try
              {
                  saveFileDialog = new SaveFileDialog();
                  saveFileDialog.Title = title;
                  saveFileDialog.Filter = fileFilter;
      
                  if(saveFileDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
                  {
                      result = saveFileDialog.FileName; 
                  }
              }
              catch (Exception ex)
              {
                  MessageBox.Show("Error:  " + ex.Message, "saveChartData Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
              }
              finally
              {
                  if (saveFileDialog != null)
                  {
                      saveFileDialog.Dispose();
                      saveFileDialog = null;
                  }
              }
      
              return result;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-04-21
        • 2014-12-24
        • 1970-01-01
        • 2010-09-13
        • 2010-10-21
        • 2013-01-29
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多