我有这个方法:
private delegate void watcherReader(StreamReader sr);
private void watchProc(StreamReader sr) {
while (true) {
string line = sr.ReadLine();
while (line != null) {
if (stop) {
return;
}
//Console.WriteLine(line);
line = stripColors(line);
txtOut.Text += line + "\n";
line = sr.ReadLine();
}
}
}
Run Code Online (Sandbox Code Playgroud)
它从进程(cmd.exe)读取流.当用户关闭cmd.exe窗口时,它会导致CPU使用率跳至100%.在使用调试器时,我看到它在sr.ReadLine()上停止并且永远不会返回.因为它正在观察StandardErrorStream和StandardOutputStream,所以它在两个核心上都使用100%.
如果需要,这里有一些项目代码.
[DllImport("User32")]
private static extern int ShowWindow(int hwnd, int nCmdShow); //this will allow me to hide a window
public ConsoleForm(Process p) {
this.p = p;
p.Start();
ShowWindow((int)p.MainWindowHandle, 0); //0 means to hide the window.
this.inStream = …Run Code Online (Sandbox Code Playgroud) 在使用C#中的streamReader函数读取文本文件(包含要导出到数据库的文件的位置)时,如何向将在命令提示符窗口(控制台应用程序)中显示的代码添加确认消息所以我知道该文件已被读取并被导出?
public class Script
{
public static void Main(string[] args)
{
// Prepare the type that will handle all of the exporting needs
FileExporter exporter = new FileExporter();
try
{
//create an instance of StreamReader to read from a file.
//The using statemen also closes the StreamReader.
using (StreamReader sr = new StreamReader("ScriptFile.txt"))
{
string filePath;
//read and display lines from the file until the end of
//the file is reached.
while ((filePath = sr.ReadLine()) != null)
{
// Throw …Run Code Online (Sandbox Code Playgroud) 下午大家.我对SQLite并不熟悉所以我没有搞乱数据库的所有设置.我比较熟悉SQL Server,Oracle甚至一些Access和mySQL.好吧,目前,我正在获取一个包含110,000多条记录的文件并逐行读取文件,解析数据并对表运行insert语句.该表取决于作为行的第一个字段的记录类型.好吧,我现在正在加载它,并且它已经运行了12分钟(正如我写的那样)并且只导入了14,000条记录.算一算,这意味着它需要1小时到15分钟到1小时30分钟.取决于我的系统其余部分当时的行为方式.因为有不同的记录类型,我无法' 如果有一个SQLite选项(不确定是否有),请执行批量插入.这是作为后台工作者运行的.下面是拉取和解析数据的函数,以及将其插入数据库的函数.请记住,这是一个MVC格式的C#应用程序(当我控制它并且没有时间重构它时就像这样):
MainForm.cs后台工作器函数
#region Background Worker Functions
#region private void InitializeBackgroundWorker()
/*************************************************************************************
*************************************************************************************/
private void InitializeBackgroundWorker()
{
backgroundWorker.DoWork +=
new DoWorkEventHandler(backgroundWorker1_DoWork);
backgroundWorker.RunWorkerCompleted +=
new RunWorkerCompletedEventHandler(
backgroundWorker1_RunWorkerCompleted);
backgroundWorker.ProgressChanged +=
new ProgressChangedEventHandler(
backgroundWorker1_ProgressChanged);
}
#endregion
/*****************************************************************************************************************************************************************************************************/
#region private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
/*************************************************************************************
*************************************************************************************/
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// Get the BackgroundWorker that raised this event.
BackgroundWorker worker = sender as BackgroundWorker;
// Assign the result of the computation
// to the Result property of the DoWorkEventArgs …Run Code Online (Sandbox Code Playgroud) 如何在NamedPipe上发送带有空行的多行字符串?
如果我发一个字符串
string text= @"line 1
line2
line four
";
StreamWriter sw = new StreamWriter(client);
sw.Write(text);
Run Code Online (Sandbox Code Playgroud)
我只在服务器端"第1行":
StreamReader sr = new StreamReader(server);
string message = sr.ReadLine();
Run Code Online (Sandbox Code Playgroud)
当我尝试这样的事情
StringBuilder message = new StringBuilder();
string line;
while ((line = sr.ReadLine()) != null)
{
message.Append(line + Environment.NewLine);
}
Run Code Online (Sandbox Code Playgroud)
它在客户端连接时挂起,只在客户端断开连接时才会释放.
任何想法我怎么能没有挂在这个循环中得到整个字符串?我需要处理字符串并以相同的方式将其返回给客户端.
保持字符串的原始格式包括空行和空格非常重要.
我正在尝试打开.txt文件,搜索一个单词并将其替换为出现的任何位置.我可以这样做,但不能用.txt文件只用我在文件中写的字符串.这.cs是我到目前为止的方法:
public void EditorialControl(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
Console.WriteLine(line);
list.Add(line);
}
reader.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
当我调用方法时Main(),它应该取参数word并用我选择的替换字替换它.
你能帮助我用代码替换方法中的单词吗?
我想忽略蓝色框中的部分并开始从箭头中读取我的txt文件

我打算只循环前8行并将它们存储在垃圾变量中.如果我这样做,我的crusor现在是在第9行,所以我可以从那里开始阅读?我的代码肯定是错的,它甚至没有读取前8行.
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
StreamReader sr = new StreamReader(File.OpenRead(ofd.FileName));
for (int i = 0; i < 8; i++)
{
string junk = sr.ReadLine();
}
sr.Dispose();
}
}
Run Code Online (Sandbox Code Playgroud) 我需要一些指导.
我正在使用StreamReader读取excel文件,然后使用StreamReader.ReadToEnd()将文件转换为字符串 ; 方法.然后我使用StreamWriter.Write()方法将字符串写入文件系统上的其他位置.
然后我从我之前写的位置重新读取文件.但是,我似乎正在阅读一些垃圾值,我无法从新位置打开excel文件...
我在这里做错了文件被破坏了吗?我在这里错过了与编码有关的事吗?
所以我试图关闭一个已经打开的文件(transactions.txt),我曾经将其读入文本框,现在我想保存回该文件,但是问题调试表明该文件正在使用中,所以我需要找到一种关闭它的方法。谁能帮我这个?谢谢!
SearchID = textBox1.Text;
string ID = SearchID.ToString();
bool idFound = false;
int count = 0;
foreach (var line in File.ReadLines("transactions.txt"))
{
//listView1.Items.Add(line);
if (line.Contains(ID))
{
idFound = true;
}
//Displays Transactions if the variable SearchID is found.
if (idFound && count < 8)
{
textBox2.Text += line + "\r\n";
count++;
}
}
}
private void SaveEditedTransaction()
{
SearchID = textBox1.Text;
string ID = SearchID.ToString();
bool idFound = false;
int count = 0;
foreach (var lines in File.ReadLines("transactions.txt"))
{
//listView1.Items.Add(line); …Run Code Online (Sandbox Code Playgroud) 我正在制作一个Web Crawler,我发现我的一个方法GetHTML非常慢,因为它使用StreamReader从HttpWebResponse对象中获取HTML字符串.
这是方法:
static string GetHTML(string URL)
{
HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(URL);
Request.Proxy = null;
HttpWebResponse Response = ((HttpWebResponse)Request.GetResponse());
Stream RespStream = Response.GetResponseStream();
return new StreamReader(RespStream).ReadToEnd(); // Very slow
}
Run Code Online (Sandbox Code Playgroud)
我用秒表进行了测试,并在YouTube上使用了这种方法.
Time it takes to get an HTTP response: 500 MS
Time it takes to convert the HttpWebResponse object to a string: 550 MS
Run Code Online (Sandbox Code Playgroud)
所以HTTP请求很好,只是ReadToEnd()这么慢.
是否有任何替代ReadToEnd()方法从响应对象中获取HTML字符串?我尝试使用WebClient.DownloadString()方法,但它只是一个使用流的HttpWebRequest的包装器.
编辑:尝试使用套接字,它更快:
static string SocketHTML(string URL)
{
string IP = Dns.GetHostAddresses(URL)[0].ToString();
Socket s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.Connect(new IPEndPoint(IPAddress.Parse(IP), 80));
s.Send(Encoding.ASCII.GetBytes("GET / HTTP/1.1\r\n\r\n")); …Run Code Online (Sandbox Code Playgroud) 我对StreamReader有一个奇怪的问题。我的程序是一个控制台程序,它应该遍历所有* .cs文件的目录结构。然后检查文件中是否有特定单词,并写入文件路径以输出。
using (StringReader sr = new StringReader(fPath))
{
string content = sr.ReadLine(); // sr.ReadToEnd();
Debug.WriteLine(content);
int found = content.IndexOf(p);
if (found != -1)
{
result = true;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我用来在特定文件中查找工作的代码。问题是sr.ReadToEnd(而且还有ReadLine)返回fPath的值而不是文件的内容!
该文件存在且未锁定。
如果fPath是:“ C:\ TEMP \ DC_LV1_LaMine_Mk2Plus_134_ix220_20160404 \ Alarm.Script.cs”
内容将为:“ C:\ TEMP \ DC_LV1_LaMine_Mk2Plus_134_ix220_20160404 \ Alarm.Script.cs”
谁能看到我做错了吗?
streamreader ×10
c# ×9
streamwriter ×3
.net ×2
database ×1
delegates ×1
excel ×1
insert ×1
methods ×1
named-pipes ×1
performance ×1
readline ×1
replace ×1
sqlite ×1
stream ×1
text-files ×1