Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/273.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# 在RichTextBox上使用LoadFile或ReadallLines_C#_Richtextbox - Fatal编程技术网

C# 在RichTextBox上使用LoadFile或ReadallLines

C# 在RichTextBox上使用LoadFile或ReadallLines,c#,richtextbox,C#,Richtextbox,我在一些系统上遇到了一个问题,当我们试图加载RichTextBox时,程序变得没有响应,我们无法做任何事情,必须通过Taskmanager终止它。 它在大多数系统上都能工作,但在另一个国家的少数系统似乎存在问题 我们尝试了以下简单的方法: private void testing4() { richTextBox1.LoadFile(@"C:\testing.logs", RichTextBoxStreamType.PlainText); } 如果我们决定使用一个普通的文本框,它似乎在

我在一些系统上遇到了一个问题,当我们试图加载RichTextBox时,程序变得没有响应,我们无法做任何事情,必须通过Taskmanager终止它。 它在大多数系统上都能工作,但在另一个国家的少数系统似乎存在问题

我们尝试了以下简单的方法:

private void testing4()
{
    richTextBox1.LoadFile(@"C:\testing.logs", RichTextBoxStreamType.PlainText);
}
如果我们决定使用一个普通的文本框,它似乎在使用.NET4.5,但它仍然没有响应。有什么想法吗?

这可能会帮助您:

foreach(var line in File.ReadLines(@"C:\testing.logs"))
{
  richTextBox1.AppendText(line+Environment.NewLine);
}

由于您使用的是framework 4.5,因此可以异步执行,如MSDN示例所示:

public async void ReadFile()
{
    string filePath = @"C:\testing.logs";

    if (File.Exists(filePath) == false)
    {
        Debug.WriteLine("file not found: " + filePath);
    }
    else
    {
        try
        {
            string text = await ReadTextAsync(filePath);
            richTextBox1.AppendText(text);
        }
        catch (Exception ex)
        {
            Debug.WriteLine(ex.Message);
        }
    }
}

private async Task<string> ReadTextAsync(string filePath)
{
    using (FileStream sourceStream = new FileStream(filePath,
        FileMode.Open, FileAccess.Read, FileShare.Read,
        bufferSize: 4096, useAsync: true))
    {
        StringBuilder sb = new StringBuilder();

        byte[] buffer = new byte[0x1000];
        int numRead;
        while ((numRead = await sourceStream.ReadAsync(buffer, 0, buffer.Length)) != 0)
        {
            string text = Encoding.Unicode.GetString(buffer, 0, numRead);
            sb.Append(text);
        }

        return sb.ToString();
    }
}
public异步void ReadFile()
{
字符串filePath=@“C:\testing.logs”;
if(File.Exists(filePath)==false)
{
Debug.WriteLine(“未找到文件:+filePath”);
}
其他的
{
尝试
{
string text=await ReadTextAsync(文件路径);
richTextBox1.AppendText(文本);
}
捕获(例外情况除外)
{
Debug.WriteLine(例如消息);
}
}
}
专用异步任务ReadTextAsync(字符串文件路径)
{
使用(FileStream sourceStream=newfilestream(filePath,
FileMode.Open,FileAccess.Read,FileShare.Read,
bufferSize:4096,UseAync:true)
{
StringBuilder sb=新的StringBuilder();
字节[]缓冲区=新字节[0x1000];
国际货币联盟;
while((numRead=wait sourceStream.ReadAsync(buffer,0,buffer.Length))!=0)
{
string text=Encoding.Unicode.GetString(缓冲区,0,numRead);
附加(正文);
}
使某人返回字符串();
}
}

来源:

当您尝试加载文本文件时,是否出现任何异常?可能是权限问题导致您的网站没有响应文件共享问题?文件在其他地方使用,我的意思是?我们确信它没有在其他地方使用,我们对此进行了验证,因为我们认为这也是问题所在。但它仅由程序使用,并且只有在PCRichTextBox上本地使用,如果超出ASCII范围,则可以缓慢转换UTF8文本。也许这就是问题所在?(有一个关于它的旧线程)这个日志文件有多大?它可能会在相当长的一段时间内变得紧张不安,当分页文件包含许多兆字节的文本时,会严重地打击该文件。使用任务管理器进行诊断,添加“PF增量”列。