C#使用表单2中的saveFileDialog将表单1中的字符串值保存到文件中

时间:2013-08-01 07:17:31

标签: c# winforms savefiledialog

我需要帮助完成作业。我有2个表单:form1有一个字符串(我在加密/解密后得到它),Form2(frmSaveFile)有saveFileDialog,用户将浏览一个位置以将生成的字符串保存到文件中。

我的问题是:如何将form1中的字符串传递给form2中的savefileDialog?并最终将其读回到form1进行解密?

以下是我的Form2代码的样子:

private Form1 myForm1;
    private void btnBrowse_Click_1(object sender, EventArgs e)
    {

       myForm1 = new Form1();
      string val =  myForm1.Encrypted_TextVal;  // I try to get this val from form1 but it's null cause I call it before form1 does anything with it!

        SaveFileDialog save = new SaveFileDialog();
        if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK) {

            StreamWriter write = new StreamWriter(File.Create(save.FileName));
            write.Write(val);

}

这是Form2代码:

{

....code code.....

 string hashDecryptedText = BitConverter.ToString(sh1.ComputeHash(textToBitArray.GetBytes(Decrypted))); // string to save in a file

}

感谢您的帮助

1 个答案:

答案 0 :(得分:3)

你去,我希望它有所帮助。

using System;
using System.Windows.Forms;
using System.IO;

namespace Stackoverflow
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        static string Hash = "Your encrytped/hash/w.e";

        Form2 form2 = new Form2(Hash);
    }

    public partial class Form2 : Form
    {
        public Form2(string Hash)
        {
             SaveFileDialog save = new SaveFileDialog();
             save.DefaultExt = ".txt";
             if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (StreamWriter write = new StreamWriter(File.Create(save.FileName)))                
                    write.Write(Hash);
             }
        }
    }
}

第二种方式:

public partial class Form1 : Form
    {
        public static Form1 Global;
        public Form1()
        {
            InitializeComponent();
            Global = this;
        }

        public string Hash = "Your encrytped/hash/w.e";

        Form2 form2 = new Form2();
    }

    public partial class Form2 : Form
    {
        public Form2()
        {
             SaveFileDialog save = new SaveFileDialog();
             save.DefaultExt = ".txt";
             if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (StreamWriter write = new     StreamWriter(File.Create(save.FileName)))                
                    write.Write(Form1.Global.Hash);
             }
        }
    }

第三种方式:

public static class DataHolder
    {
        private static string _hash;
        public static string Hash { get { return _hash; } set { _hash = value; } }
    }

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetHash("HASH");
        }

        public void SetHash(string hash)
        {
            DataHolder.Hash = hash;           
        }

        Form2 form2 = new Form2();

    }

    public partial class Form2 : Form
    {
        public Form2()
        {
             SaveFileDialog save = new SaveFileDialog();
             save.DefaultExt = ".txt";
             if (save.ShowDialog() == System.Windows.Forms.DialogResult.OK)
             {
                 using (StreamWriter write = new StreamWriter(File.Create(save.FileName)))                
                    write.Write(DataHolder.Hash);
             }
        }
    }