Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/334.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中运行windows窗体时访问和更改txt文档#_C#_.net_Winforms_File_Streamreader - Fatal编程技术网

C# 在C中运行windows窗体时访问和更改txt文档#

C# 在C中运行windows窗体时访问和更改txt文档#,c#,.net,winforms,file,streamreader,C#,.net,Winforms,File,Streamreader,我目前正在自学如何使用Windows窗体与我一直在制作的MIRC机器人交互。目前,我有一个问题通过bot输入并输出到一个.txt文件,然后由Windows窗体获取。目前,我的代码在提取数据时工作正常,但一旦我创建了StreamReader从文本中提取数据,MIRC就无法在Windows窗体运行时进一步修改文件。我试过把球打近(),但那没用。以下是我在Windows窗体中用于按钮的代码: private void button1_Click(object sender, EventArgs e)

我目前正在自学如何使用Windows窗体与我一直在制作的MIRC机器人交互。目前,我有一个问题通过bot输入并输出到一个.txt文件,然后由Windows窗体获取。目前,我的代码在提取数据时工作正常,但一旦我创建了StreamReader从文本中提取数据,MIRC就无法在Windows窗体运行时进一步修改文件。我试过把球打近(),但那没用。以下是我在Windows窗体中用于按钮的代码:

private void button1_Click(object sender, EventArgs e)
{
    i = 0;
    questionDoc = new StreamReader("questions.txt");
    if (questionDoc.ReadLine() != null)
    {
        fullText = questionDoc.ReadToEnd();
        questionList = fullText.Split('\t');
        for (int j = 0; j < questionList.Length; j++)
        {
            this.label1.Text = questionList[j];
        }
        questionDoc.Close();
    }
    else
        this.label1.Text = "No questions!";
}
private void按钮1\u单击(对象发送者,事件参数e)
{
i=0;
questionDoc=新的StreamReader(“questions.txt”);
if(questionDoc.ReadLine()!=null)
{
全文=questionDoc.ReadToEnd();
questionList=全文.Split('\t');
for(int j=0;j

因此,目前我可以拉问题,但txt文件不能再更新我第一次点击该按钮。还有别的办法吗?谢谢你的帮助

您可以轻松制作文件副本并打开副本:

private void button1_Click(object sender, EventArgs e)
{
    i = 0;
    File.Copy("questions.txt", "questionsCopy.txt", true);
    questionDoc = new StreamReader("questionsCopy.txt");
    if (questionDoc.ReadLine() != null)
    {
        fullText = questionDoc.ReadToEnd();
        questionList = fullText.Split('\t');
        for (int j = 0; j < questionList.Length; j++)
        {
            this.label1.Text = questionList[j];
        }
        questionDoc.Close();
    }
    else
        this.label1.Text = "No questions!";

}
private void按钮1\u单击(对象发送者,事件参数e)
{
i=0;
文件副本(“questions.txt”、“questionsCopy.txt”,true);
questionDoc=新的StreamReader(“questionScope.txt”);
if(questionDoc.ReadLine()!=null)
{
全文=questionDoc.ReadToEnd();
questionList=全文.Split('\t');
for(int j=0;j
即使第一个ReadLine()返回null,也尝试关闭该文件。另外,将“questionDoc”设置为变量而不是字段,并无条件地进行处理,如下所示:

    private void button1_Click(object sender, EventArgs e)
    {
        i = 0;
        using (var questionDoc = new StreamReader("questions.txt"))
        {
            if (questionDoc != null && questionDoc.ReadLine() != null)
            {
                fullText = questionDoc.ReadToEnd();
                questionList = fullText.Split('\t');
                for (int j = 0; j < questionList.Length; j++)
                {
                    this.label1.Text = questionList[j];
                }
            }
            else
                this.label1.Text = "No questions!";
        }
    }
private void按钮1\u单击(对象发送者,事件参数e)
{
i=0;
使用(var questionDoc=newstreamreader(“questions.txt”))
{
if(questionDoc!=null&&questionDoc.ReadLine()!=null)
{
全文=questionDoc.ReadToEnd();
questionList=全文.Split('\t');
for(int j=0;j

如果文件被其他应用程序锁定,或被删除,或出现其他问题,您可能需要为添加
try/catch

您可以使用
file.Open()
更改文件访问权限,以便其他进程可以使用
FileShare.ReadWrite
():


如果文件为空,则不会关闭文件句柄,因此其他应用程序无法写入该文件。
using (FileStream fs = File.Open("questions.txt", FileMode.Open,
       FileAccess.Read, FileShare.ReadWrite)) {
    using (StreamReader questionDoc = new StreamReader(fs)) {
        // do your stuff
    }
}