我有一个StreamReader,偶尔会检查是否有更多要从简单的文本文件中读取.它使用peek属性.问题在于,当我使用peek时,位置会发生变化,而不是假设.
FileStream m_fsReader = new FileStream(
m_strDataFileName,
FileMode.OpenOrCreate,
FileAccess.Read,
FileShare.ReadWrite );
StreamReader m_SR = new StreamReader(m_fsReader);
Console.WriteLine("IfCanRead SR Position " + m_fsReader.Position +
" and Length " + m_fsReader.Length);
if (m_SR.Peek() == -1) {
Console.WriteLine("IfCanRead false 2 SR Position " +
m_fsReader.Position + " and Length " + m_fsReader.Length);
return false;
}
else {
Console.WriteLine("IfCanRead true 2 SR Position " +
m_fsReader.Position + " and Length " + m_fsReader.Length);
return true;
}
Run Code Online (Sandbox Code Playgroud) 我对这篇文章有类似的要求... 在运行时使用textfile填充Gridview
我想用文本文件读取文本文件StreamReader并DataTable使用文件中的数据填充,但是我不确定如何split()使用选项卡实现.
有人能指出我正确的方向吗?
我正在使用txt或htm文件.目前我正在通过char查找文档char,使用for循环,但我需要逐字查找文本,然后在char中查找单词char.我怎样才能做到这一点?
for (int i = 0; i < text.Length; i++)
{}
Run Code Online (Sandbox Code Playgroud) 我想byte[]用C#读取文件的当前编码.
正如在MSDN中编写的那样,当构造函数没有编码时,默认编码将是UTF-8:
var reader = new StreamReader(new MemoryStream(data)).
Run Code Online (Sandbox Code Playgroud)
我也试过这个,但仍然把文件作为UTF-8:
var reader = new StreamReader(new MemoryStream(data),true)
Run Code Online (Sandbox Code Playgroud)
我需要byte[]用当前编码来阅读.
private void button1_Click(object sender, EventArgs e)
{
string fileLoc = @"c:\wms.txt";
if (File.Exists(fileLoc))
{
using (TextReader tr = new StreamReader(fileLoc))
{
MessageBox.Show(tr.ReadLine());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这在我创建Windows应用程序时非常有效.
当我在设备应用程序中使用相同的代码时 - Windows CE我收到错误:

使用:.Net 2.0,visual Studio 2005
static void Main(string[] args)
{
string path = Console.ReadLine();
using (StreamReader sr = new Streamreader(path);
{
\\ do something with the file
}
}
Run Code Online (Sandbox Code Playgroud)
嗨,我正在尝试允许用户将txt文件拖放到控制台中,让它读取路径,然后解析文件中的信息.
但是我得到了:
mscorlib.dll中出现未处理的"System.ArgumentException"类型异常
附加信息:路径中的非法字符.
当我运行我的代码.我想我可以通过在使用之前格式化文件路径来解决它,但这似乎不是"预期的解决方案".我失踪了吗?
谢谢.
我编写了一个Winform应用程序,它读取文本文件的每一行,使用行上的RegEx进行搜索和替换,然后将其写回新文件.我选择了"逐行"方法,因为有些文件太大而无法加载到内存中.
我正在使用BackgroundWorker对象,因此可以使用作业的进度更新UI.下面是代码(为简洁起见省略了部分),它处理读取然后输出文件中的行.
public void bgWorker_DoWork(object sender, DoWorkEventArgs e)
{
// Details of obtaining file paths omitted for brevity
int totalLineCount = File.ReadLines(inputFilePath).Count();
using (StreamReader sr = new StreamReader(inputFilePath))
{
int currentLine = 0;
String line;
while ((line = sr.ReadLine()) != null)
{
currentLine++;
// Match and replace contents of the line
// omitted for brevity
if (currentLine % 100 == 0)
{
int percentComplete = (currentLine * 100 / totalLineCount);
bgWorker.ReportProgress(percentComplete);
}
using (FileStream fs = new FileStream(outputFilePath, FileMode.Append, FileAccess.Write)) …Run Code Online (Sandbox Code Playgroud) 所以我试图在文件中搜索一些特定的字符串,这些字符串在a中list被调用并被调用universities,我正在使用a 来加载文件.CoursesUGPGStreamreader
我遇到的问题是,在第一个foreach循环执行剩余的搜索之后,我想要完成返回,N/a就像文本文件中不存在字符串一样.但是我知道它们在文本文件中.
有没有理由这样或更好的方法来编码?
我的代码如下.
任何帮助将不胜感激.
validdirectory = new DirectoryInfo(path);
Vfiles = validdirectory.GetFiles("*.txt");
foreach (FileInfo file in Vfiles)
{
//reads the file contents
bool Stepout = false;
bool nextouterloop = false;
using (StreamReader ReadMessage = new StreamReader(file.FullName))
{
String MessageContents = ReadMessage.ReadToEnd();
Message_Viewer.Text = MessageContents;
foreach (string Uni_Name in Universities)
{
if (MessageContents.Contains(Uni_Name))
{
Display_Uni.Text = Uni_Name;
}
}
foreach (string course in Courses)
{
if (MessageContents.Contains(course))
{
Display_Course.Text …Run Code Online (Sandbox Code Playgroud) 我有一些名为的文件:
正如您可以看到每个文件的SWD值交换,我想知道是否有办法在不知道SWD值的情况下打开文件,如下所示:
6327_1-SWD ??? - 171016_1.txt
或者是否StreamReader需要文件的确切名称?
SemaphoreSlim sm = new SemaphoreSlim(10);
using (FileStream fileStream = File.OpenRead("..."))
using (StreamReader streamReader = new StreamReader(fileStream, Encoding.UTF8, true, 4096))
{
String line;
while ((line = streamReader.ReadLine()) != null)
{
sm.Wait();
new Thread(() =>
{
doSomething(line);
sm.Release();
}).Start();
}
}
MessageBox.Show("This should only show once doSomething() has done its LAST line.");
Run Code Online (Sandbox Code Playgroud)
因此,我有一个非常大的文件,我想在每一行上执行代码。
我想并行执行,但一次最多只能执行10次。
我的解决方案是使用SemaphoreSlim等待并在线程完成时释放。(由于该函数是同步的,因此.Release()的放置有效)。
问题是代码占用了大量CPU。内存的运行情况与预期的一样,而不是加载超过400mb,而是每几秒钟上升和下降几mbs。
但是CPU发疯了,它的大部分时间都在100%的时间内锁定了30秒钟,然后略微下降并返回。
既然我不想将每一行都加载到内存中,并且想要一直运行代码,那么最好的解决方案是什么?
我从new Thread(()=>{}).Start();改为Task.Factory.StartNew(()=>{});注释中提到的,似乎线程创建和销毁正在导致性能下降。这似乎是正确的。当我移到Task.Factory.StartNew之后,它以与信号量所提到的相同的速度运行,并且它的CPU与我的Parallel.ForEach代码版本完全一样。
c# ×10
streamreader ×10
.net ×3
.net-2.0 ×1
asp.net ×1
encoding ×1
filestream ×1
peek ×1
semaphore ×1
split ×1
stream ×1
streamwriter ×1
text ×1
textreader ×1
vb.net ×1
while-loop ×1
windows-ce ×1