Home > Net >  By c # button click open the same text file, three times in the file are occupied by the other threa
By c # button click open the same text file, three times in the file are occupied by the other threa

Time:05-28

 
Public static List GetTextList (string FileName)
{
List List=null;

Try
{
List=new List (a);
FileStream fs=new FileStream (FileName, FileMode. Open);
StreamReader sr=new StreamReader (fs, Encoding. The Default).
String STR=sr. ReadLine ();

While (STR!=string. The Empty)
{
If (STR. The Substring (6, 2)=="00")
{
String data=https://bbs.csdn.net/topics/str.Substring (1, STR. Length - 1);
List. The Add (data);
}

STR=sr. ReadLine ();
}
The sr. The Close ();
Fs. The Close ();
}
The catch (Exception ex)
{
Console. WriteLine (" [ERROR] "+ ex. Message. The ToString ());
}

return list;
}


Per click a button to invoke a getTextList, such as good 5 seconds then click, when click a third time:
The catch (Exception ex)
{
Console. WriteLine (" [ERROR] "+ ex. Message. The ToString ());
}
The exception is caught,

CodePudding user response:

Are you sure the while (STR!=string. The Empty) is not an infinite loop? Generally the sr. ReadLine () no, value is null, you can use the while (! String. IsNullOrEmpty (STR)) have a try,

CodePudding user response:

Only read the words, a file opened in read-only mode

FileStream stream=new FileStream (" path ", FileMode. Open, FileAccess. Read, FileShare. Read);

CodePudding user response:

Why bother?
String [] a=File. ReadAllLines (FileName);
Or;
Var list=File. ReadAllLines (FileName) ToList ();


How simple!
  •  Tags:  
  • C#
  • Related