[英]Visual Studio 2010 C# - Loading saved file from file explorer into application
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. 我有一个Windows Form程序,您可以在其中输入数据,该数据可以保存到XML格式的文件中,我给它自己扩展的.WSDA,我希望能够在不加载程序的情况下单击此文件,并使其运行与我用于保存该文件的例程相同的例程。
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();
}
}
I found a work for what I needed to accomplish. 我找到了需要完成的工作。
Program.cs I made with the following. 我用以下代码制作的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());
}
}
}
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 然后以实际的形式运行一个临时文件的IF语句,以加载program.cs处理的临时文件,如您所见,然后将其复制到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.
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.