我正在尝试制作一个程序,该程序同时显示用户输入并写入到同一用户输入数据的文本文件中的文本。我试过包裹与Task.Run代码:如何写入C#文件
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));
}
}
但我的第二个文本(一个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))
我尝试使用的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);
}
}
}
,但我得到就行了一个错误:
using (StreamWriter writer = new StreamWriter(@"A:\temp\name.txt"))
因为'@“A:\ temp \ name.txt''不能从'字符串'转换为'System.IO.Stream'
而当我尝试没有任何包装的普通方式时,我得到一个同步错误。任何解决这个问题的方法都将非常感激。
请提供一个MCVE(http://stackoverflow.com/help/mcve)来演示此问题。你的例子引入了不相关的信息(比如'show.Text'和'show2.Text',它们在代码中没有用到)。你需要提供一个清楚的例子,说明你正在尝试做什么,而没有所有其他混乱对于手头的问题毫无意义。 –
驱动器A和B用于软盘,您需要先更改驱动器 –
我将驱动器A分配给了我的辅助硬盘驱动器。 – Yetoo