如何在打开时自动保存* txt文件?(或复制* txt)[C#Visual studio]

时间:2015-06-07 08:05:24

标签: streamreader streamwriter

有人可以帮忙吗? 首先,按下按钮后我在我的PC中选择了任何* txt文件(使用openFileDialog),我打开它,程序应该在BlaBla.txt中自动保存它(没有文件对话框)。或者也许复制* txt文件。 我尝试了很多方法,我得到的只是空的 blabla.txt

 if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new
                   System.IO.StreamReader(openFileDialog1.FileName);


            string line = sr.ReadLine();

            System.IO.StreamWriter file = new System.IO.StreamWriter("BlaBla.txt");
            file.WriteLine(sr);


        }

edidet代码。仍然无效。

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamReader sr = new
                   System.IO.StreamReader(openFileDialog1.FileName);

              String line = reader.ReadLine();

               // System.IO.StreamWriter file = new System.IO.StreamWriter("ddd.txt");

              //  file.Write(sr);
                   System.IO.StreamWriter file = new System.IO.StreamWriter("ddd.txt");
                file.Write(sr);
                file.Flush();
            }_____________________________________________

找到另一种保存方式,我的工作正常。

if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {


                string org, copy;

                System.IO.StreamReader file = new System.IO.StreamReader(openFileDialog1.FileName);
                while ((org = file.ReadLine()) != null)
                {
                    copy = org.ToString();
                    using (System.IO.StreamWriter files = new System.IO.StreamWriter("blabla.txt", true))
                    {
                        files.WriteLine(copy);
                    }
                }

                file.Close();
            }

0 个答案:

没有答案