简体   繁体   English

如何在C#中写入文件

[英]How to write to a file in C#

I am trying to make a program that, at the same time, displays text that the user inputted and writes to a text file of that same user input data.我正在尝试制作一个程序,该程序同时显示用户输入的文本并写入具有相同用户输入数据的文本文件。 I've tried wrapping the the code with Task.Run:我试过用 Task.Run 包装代码:

 private void button_Click(object sender, RoutedEventArgs e)
        {
            show.Text = inputText.Text;
            //Debug.WriteLine(check1_cont.ToString());
            //Debug.WriteLine(check2_cont.ToString());

            if (check1_cont && check2_cont == true )
            {
                show2.Text = inputText.Text;

                Task.Run(() => File.WriteAllText(@"A:\temp\name.txt", inputText.Text));
            }

        } 

But I get an exception error after the second text (the one in the if statement) when I press the button:但是当我按下按钮时,在第二个文本(if 语句中的那个)之后出现异常错误:

An exception of type 'System.Exception' occurred in normieap.exe but
was not handled in user code

Additional information: The application called an interface that was
marshalled for a different thread. (Exception from HRESULT: 0x8001010E
(RPC_E_WRONG_THREAD))

I try using StreamWriter:我尝试使用 StreamWriter:

private void button_Click(object sender, RoutedEventArgs e)
        {
            show.Text = inputText.Text;
            //Debug.WriteLine(check1_cont.ToString());
            //Debug.WriteLine(check2_cont.ToString());

            if (check1_cont && check2_cont == true )
            {
                show2.Text = inputText.Text;
                using (StreamWriter writer = new StreamWriter(@"A:\temp\name.txt"))
                {
                    writer.WriteLine(inputText.Text);
                }
             }

        } 

But I get an error on the line:但是我在线上收到一个错误:

using (StreamWriter writer = new StreamWriter(@"A:\temp\name.txt"))

Because '@"A:\\temp\\name.txt"' cannot convert from 'string' to 'System.IO.Stream'因为 '@"A:\\temp\\name.txt"' 不能从 'string' 转换为 'System.IO.Stream'

And when I try just the normal way without any wrappers I get a synchronous error.当我在没有任何包装器的情况下尝试正常方式时,我会收到同步错误。 Any solutions to this problem would be much appreciated.对此问题的任何解决方案将不胜感激。

When you run a task asyncrounously, it isn't guaranteed to run on the UI thread.当您异步运行任务时,不能保证它在 UI 线程上运行。 Take your first example and try this:拿你的第一个例子试试这个:

private void button_Click(object sender, RoutedEventArgs e)
    {
        show.Text = inputText.Text;
        //Debug.WriteLine(check1_cont.ToString());
        //Debug.WriteLine(check2_cont.ToString());

        if (check1_cont && check2_cont == true )
        {
            show2.Text = inputText.Text;
            // Copy the text to output
            string outputToWrite = inputText.Text;
            // use the copied text
            Task.Run(() => File.WriteAllText(@"A:\temp\name.txt", outputToWrite));
        }

    }

What's going on here is that a background thread is trying to access a GUI element.这里发生的事情是后台线程正在尝试访问 GUI 元素。 That's generally not allowed in singled threaded UI libraries like Windows Forms, so you need to copy the data out of the control before sending it back to the background thread, otherwise the code will fail as you have seen.这在 Windows 窗体等单线程 UI 库中通常是不允许的,因此您需要在将数据发送回后台线程之前将数据从控件中复制出来,否则代码将失败,如您所见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM