2015-09-06 68 views
1

我有一个Windows Form程序,您可以将数据输入到该数据中,该数据可以保存为XML格式的文件,我给它自己扩展.WSDA ,我希望能够在没有加载程序的情况下点击这个文件并让它运行我用来保存它的相同例程。Visual Studio 2010 C# - 从文件资源管理器中将保存的文件加载到应用程序中

这里是事件保存,并通过按钮载入文件...

private void button11_Click(object sender, EventArgs e) 
    { 
     // Opens Dialog Box 
     SaveFileDialog saveFileDialog1 = new SaveFileDialog(); 
     saveFileDialog1.Filter = "Workspace Data File |*.wsda"; 
     saveFileDialog1.Title = "Save current Workspace data."; 
     saveFileDialog1.ShowDialog(); 
     if (saveFileDialog1.FileName != "") 
     {    

      // Create an instance of the FormData class and populate it with form data..    
      FormData FormSave = new FormData(); 
      FormSave.form_type = textForm.Text; 
      FormSave.policy_num = textPolicynum.Text; 
      FormSave.effective_date = textEffdate.Text; 
      FormSave.sai_number = textSAI.Text; 
      FormSave.office_code = textOffice.Text; 
      FormSave.change_date = textChgdate.Text; 
      FormSave.name_insured = textNamedIns.Text; 
      FormSave.error_code = textErrorCode.Text; 
      FormSave.producer_code = textProducer.Text; 
      FormSave.nticid = textNtic.Text; 
      FormSave.notes = textNotes.Text; 

      // Create and XmlSerializer to serialize the data. 
      XmlSerializer xs = new XmlSerializer(typeof(FormData)); 

      // XmlTextWriter writes file. 
      XmlTextWriter xtw = new XmlTextWriter(new StreamWriter(saveFileDialog1.FileName)); 
      xs.Serialize(xtw, FormSave);  
      // Stop writing to file. 
      xtw.Flush(); 
      xtw.Close(); 
     } 
    } 

    private void button12_Click(object sender, EventArgs e) 
    { 

     // Opens Dialog Box 
     OpenFileDialog openFileDialog1 = new OpenFileDialog(); 
     openFileDialog1.Filter = "Workspace Data File |*.wsda"; 
     openFileDialog1.Title = "Save current Workspace data."; 
     openFileDialog1.ShowDialog(); 
     if (openFileDialog1.FileName != "") 
     { 

      //XmlTextReader Reads file. 
      XmlSerializer xs = new XmlSerializer(typeof(FormData)); 
      XmlTextReader xtw = new XmlTextReader(openFileDialog1.FileName); 
      FormData FormLoad = new FormData(); 

      //Converts it to form data again. 
      FormLoad = xs.Deserialize(xtw) as FormData;     

      //Updates the fields with the data file. 
      textForm.Text = FormLoad.form_type; 
      textPolicynum.Text = FormLoad.policy_num; 
      textEffdate.Text = FormLoad.effective_date; 
      textSAI.Text = FormLoad.sai_number; 
      textOffice.Text = FormLoad.office_code; 
      textProducer.Text = FormLoad.producer_code; 
      textChgdate.Text = FormLoad.change_date; 
      textNamedIns.Text = FormLoad.name_insured; 
      textErrorCode.Text = FormLoad.error_code; 
      textNtic.Text = FormLoad.nticid; 
      textNotes.Text = FormLoad.notes; 

      // Stop Reading File. 
      xtw.Close(); 
     } 
    } 
+0

您需要将您的程序与自定义文件扩展名(在您的案例中为“.wsda”)相关联。看看这个问题的答案:http://stackoverflow.com/questions/69761/how-to-associate-a-file-extension-to-the-current-executable-in-c-sharp – TheVillageIdiot

+0

还有一些你可以在这里找到一些线索。 http://stackoverflow.com/questions/23727986/allow-a-custom-file-to-double-click-and-open-my-application-while-loading-its-d –

+0

不确定这些链接解释我的'米试图做。我知道如何关联文件。我不知道如何让程序获取XML数据并运行它,就好像我在程序打开关联时启动openfiledialog时一样。 –

回答

0

我找到了一个工作,我需要完成的任务。

Program.cs我做了以下。

static class Program 
    { 

     /// <summary> 
     /// The main entry point for the application. 
     /// </summary> 
     [STAThread] 
     static void Main(string[] args) 
     { 
      if (args.Length == 1) //make sure an argument is passed 
      { 
       FileInfo file = new FileInfo(args[0]);     
       if (file.Exists) //make sure it's actually a file 
       { 
        File.Copy(file.FullName, "./workspace.tmp"); //copys the associated file to the program folder and renames it. 
       // This is importants since the name of the file can be anything *.wsda 
       } 
      } 

      Application.EnableVisualStyles(); 
      Application.SetCompatibleTextRenderingDefault(false); 
      Application.Run(new Form1()); 
     } 
    } 
} 

然后在实际的形式,我运行一个IF语句临时文件载入的Program.cs的处理,当你看到它,然后复制到workspace.tmp

这解析文件和设置表单文本框的值与加载文件的值相同。

if (System.IO.File.Exists("./workspace.tmp")) 
      { 
      //XmlTextReader Reads file. 
      XmlSerializer xs = new XmlSerializer(typeof(FormData)); 
      XmlTextReader xtw = new XmlTextReader("./workspace.tmp"); 
      FormData FormLoad = new FormData(); 

      //Converts it to form data again. 
      FormLoad = xs.Deserialize(xtw) as FormData; 

      //Updates the fields with the data file. 
      textForm.Text = FormLoad.form_type; 
      textPolicynum.Text = FormLoad.policy_num; 
      textEffdate.Text = FormLoad.effective_date; 
      textSAI.Text = FormLoad.sai_number; 
      textOffice.Text = FormLoad.office_code; 
      textProducer.Text = FormLoad.producer_code; 
      textChgdate.Text = FormLoad.change_date; 
      textNamedIns.Text = FormLoad.name_insured; 
      textErrorCode.Text = FormLoad.error_code; 
      textNtic.Text = FormLoad.nticid; 
      textNotes.Text = FormLoad.notes; 

      // Stop Reading File. 
      xtw.Close(); 
      File.Delete("./workspace.tmp"); // since its done loading it, deletes the file. 
      } 
相关问题