public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Writetext();
Readtext();
}
/// <summary>创建文件</summary>
/// <returns></returns>
/// <summary>导入文件</summary>
/// <returns></returns>
public void Writetext()
{
//StreamWriter sw = new StreamWriter("D:/VS项目文件/测试2017-9-5/测试2017-9-5/Log/Log.txt", true);
// 这个构造函数的参数是文件名和一个Boolean值,这个Boolean值规定是追加文件,还是创建新文件:
// a、false:创建一个新文件,或者截取现有文件并打开它。
//b、true:打开文件,保留原来的数据。如果找不到文件,则创建一个新文件。
FileStream aFile = new FileStream(@"D:/VS项目文件/测试2017-9-5/测试2017-9-5/Log/Log.txt", FileMode.OpenOrCreate);
StreamWriter sw = new StreamWriter(aFile);
//A 为要写入的字符串组
// string A ="1,2,3,4,5,6,1,1,1,1,1,1,1,1,1,1,1,1";
string a = "barcode";
string b = "wendu";
string c = "yali";
string A = a + b + c;
string[] strs = { A };
foreach (string s in strs)
{
sw.WriteLine(s);
}
sw.Close();
}
/// <summary>读取文件</summary>
/// <returns></returns>
public void Readtext()
{
// StreamReader sr = File.OpenText("text.txt");
//FileStream fs = File.Open("text.txt", FileMode.Open, FileAccess.Read);
//StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312"));
// string line = sr.ReadLine();
// sr.Close();
FileStream aFile = new FileStream(@"D:/VS项目文件/测试2017-9-5/测试2017-9-5/Log/Log.txt", FileMode.OpenOrCreate);
StreamReader sr =new StreamReader(aFile);
//B为要显示的一行字符串;
string B = sr.ReadLine();
string sub = B.Substring(0, 13);
//sub为字符串B的前13位;
string Barcode ="1111111111111";//选取的的代码(13位);
if (sub != Barcode)
{
MessageBox.Show(B);
}
sr.Close();
// fs.Close();
}
}