c#实现写入,读取实现百度搜索框
2016-09-29 14:01
344 查看
//写入内容
private void write(TextBox textBox)
{
if (!File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write("\r\n" + textBox.Text);
sw.Flush();
sw.Close();
fs.Close();
}
else
{
FileStream fs = new FileStream(path ,FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write("\r\n" + textBox.Text);
sw.Flush();
sw.Close();
fs.Close();
}
}
string[] s = new string[] { };
//绑定文本
public void bindTxt(TextBox textBox)
{
List<string> a = s.ToList();
StreamReader sr = new StreamReader(path, Encoding.UTF8);
string line;
while ((line = sr.ReadLine()) != null)
{
a.Add(line);
}
s = a.ToArray();
var source = new AutoCompleteStringCollection();
source.AddRange(s);
textBox.AutoCompleteCustomSource = source;
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
sr.Close();
}
private void button1_Click_1(object sender, EventArgs e)
{
if (!File.Exists(path))
{
write(textBox1);
}
else
{
StreamReader sr = new StreamReader(path, Encoding.UTF8);
bool isWirte = true;
string line1;
while ((line1 = sr.ReadLine()) != null)
{
if (line1 == textBox1.Text)
{
isWirte = false;
sr.Close();
break;
4000
}
}
if (isWirte)
{
sr.Close();
write(textBox1);
}
sr.Close();
bindTxt(textBox1);
}
}
}
}
private void write(TextBox textBox)
{
if (!File.Exists(path))
{
FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write("\r\n" + textBox.Text);
sw.Flush();
sw.Close();
fs.Close();
}
else
{
FileStream fs = new FileStream(path ,FileMode.Append, FileAccess.Write);
StreamWriter sw = new StreamWriter(fs);
sw.Write("\r\n" + textBox.Text);
sw.Flush();
sw.Close();
fs.Close();
}
}
string[] s = new string[] { };
//绑定文本
public void bindTxt(TextBox textBox)
{
List<string> a = s.ToList();
StreamReader sr = new StreamReader(path, Encoding.UTF8);
string line;
while ((line = sr.ReadLine()) != null)
{
a.Add(line);
}
s = a.ToArray();
var source = new AutoCompleteStringCollection();
source.AddRange(s);
textBox.AutoCompleteCustomSource = source;
textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox.AutoCompleteSource = AutoCompleteSource.CustomSource;
sr.Close();
}
private void button1_Click_1(object sender, EventArgs e)
{
if (!File.Exists(path))
{
write(textBox1);
}
else
{
StreamReader sr = new StreamReader(path, Encoding.UTF8);
bool isWirte = true;
string line1;
while ((line1 = sr.ReadLine()) != null)
{
if (line1 == textBox1.Text)
{
isWirte = false;
sr.Close();
break;
4000
}
}
if (isWirte)
{
sr.Close();
write(textBox1);
}
sr.Close();
bindTxt(textBox1);
}
}
}
}
相关文章推荐
- 理解C#中参数
- C# MD5获取错误
- c# 利用 splitcontainer 和treeview 在MDI上面显示窗体 (进阶)
- C# 操作Excel 格式
- 条件计算器模型
- C# 字符串string类型转换成DateTime类型 或者 string转换成DateTime?(字符串转换成可空日期类型)
- ArcEngine c# 创建shp图层,并将要素写入图层保存
- ARCGIS Engine 图层
- 关于c#文件操作Streamwriter,streamreader的使用
- C#学习笔记------3、继承
- C#使用ESC指令控制POS打印机打印小票
- C#学习笔记------2、面向对象
- C#值类型和引用类型_160928
- c#枚举 获取枚举键值对、描述等
- C#程序连接UPDB
- C#操作Excel总结
- C#给Excel添加水印实例详解
- C# 中的委托和事件(详解)
- C#泛型(二)
- Java To CSharp源代码转换