1

I have a Windows Form program that you enter data in to, that data can be saved to a XML format file, I am giving it my own extention of .WSDA, I'd like to be able to without having the program loaded, click this file and have it run the same routine i use to save it.

Here are the events for saving and loading the files via button...

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();
        }
    }
  • You need to associate your program with custom file extension (`.wsda` in your case). Have a look at answers to this question: http://stackoverflow.com/questions/69761/how-to-associate-a-file-extension-to-the-current-executable-in-c-sharp – TheVillageIdiot Sep 06 '15 at 13:31
  • Also some you can find some clue here. http://stackoverflow.com/questions/23727986/allow-a-custom-file-to-double-click-and-open-my-application-while-loading-its-d – M_Idrees Sep 06 '15 at 13:41
  • Not sure these links explain what I'm trying to do. I know how to associate the file. I don't know how to make the program take the XML data and run it as if I had used the openfiledialog when it starts when the program opens by the association. – Brian Pulaski Sep 06 '15 at 17:43

1 Answers1

0

I found a work for what I needed to accomplish.

Program.cs I made with the following.

   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());
        }
    }
}

Then in the actual form I run a IF statement for a temp file to load which the program.cs handles, as you see its then copied to workspace.tmp

This parses the file and sets the form textbox values to that of the loaded file.

  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.
            }