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();
}
}
您需要将您的程序与自定义文件扩展名(在您的案例中为“.wsda”)相关联。看看这个问题的答案:http://stackoverflow.com/questions/69761/how-to-associate-a-file-extension-to-the-current-executable-in-c-sharp – TheVillageIdiot
还有一些你可以在这里找到一些线索。 http://stackoverflow.com/questions/23727986/allow-a-custom-file-to-double-click-and-open-my-application-while-loading-its-d –
不确定这些链接解释我的'米试图做。我知道如何关联文件。我不知道如何让程序获取XML数据并运行它,就好像我在程序打开关联时启动openfiledialog时一样。 –