简体   繁体   English

Visual Studio 2010 C#-将文件浏览器中的保存文件加载到应用程序中

[英]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.

相关问题 在Visual Studio 2010 C#应用程序中添加脚本文件 - Adding a script file in a Visual Studio 2010 C# application 即使成功构建项目,应用程序也无法与MDF文件建立连接[Visual Studio 2010 C#] - The application cannot make a connection with MDF file even building the project successfully [Visual Studio 2010 C#] 如何在运行时将 c# 文件添加到 Visual Studio 解决方案资源管理器? - How to add a c# file to Visual Studio Solution Explorer at runtime? 在Visual Studio 2010 C#项目中读取链接的xml文件 - Read a linked xml file in Visual Studio 2010 C# project 在Visual Studio 2010 C#项目中将.chm文件链接到.dll - Linking a .chm file to a .dll in Visual Studio 2010 C# Project 从项目上传文件-Selenium WebDriver Visual Studio 2010 C# - Upload File from Project - Selenium WebDriver Visual Studio 2010 C# Visual Studio的“测试资源管理器”输出位于何处?它是保存的文件吗? - Where is the Visual Studio 'Test Explorer' Output located? Is it a saved file? 特定于应用程序的数据库文件-Visual Studio 2010 - application specific database file - visual studio 2010 从 C# 在 Visual Studio 中切换文件 - Switch file in Visual Studio from C# 我可以在 Visual Studio 2010 中查看以前保存的文件版本吗? - Can I view previously saved versions of a file in Visual Studio 2010?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM