基本信息
源码名称:C# 读写txt文件 示例代码
源码大小:4.96KB
文件格式:.cs
开发语言:C#
更新时间:2015-09-02
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
namespace Duxieqi
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public String name;
public List<string> liststring=new List<string>();
String[] www=new string [4];
private void Form1_Load(object sender, EventArgs e)
{
}
public void ReadInfo(string sss)
{
FileStream fs = new FileStream(sss, FileMode.Open);
StreamReader sr = new StreamReader(fs);
string info = sr.ReadLine();
if (!string.IsNullOrEmpty(info))
{
string[] array1 = info.TrimStart().TrimEnd().Split(' ');
for (int i = 0; i <= array1.Length; i )
{
if (array1[i] != "")
{
liststring.Add(array1[i]);
}
}
dataGridView1.Rows[0].Cells[0].Value = liststring[0].ToString();
dataGridView1.Rows[0].Cells[1].Value = liststring[1];
dataGridView1.Rows[0].Cells[2].Value = liststring[2];
dataGridView1.Rows[0].Cells[3].Value = liststring[3];
fs.Close();
}
////string[] arrP = liststring.ConvertAll<string>(new Converter<Product, string>(delegate(Product p) { return p.Name; })).ToArray();
//www[0] = liststring[0].ToString();
//www[1] = liststring[1];
//www[2] = liststring[2];
//www[3] = liststring[3];
}
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog(); //实例化一个对象
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //能够创建的文本文件类型
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
name = saveFileDialog1.FileName.ToString();
if (!File.Exists(name))
{
;
FileStream fs1 = new FileStream(name, FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
// sw.WriteLine("");//开始写入值字符串
sw.Close();
fs1.Close();
MessageBox.Show("创建成功!");
}
else
{
MessageBox.Show("文件已经存在!");
FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(textBox1.Text);//开始写入值
sr.Close();
fs.Close();
}
}
else
return;
}
private void button1_Click(object sender, EventArgs e)
{
//try
//{
FileStream fs = new FileStream(name, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
string input = textBox1.Text " " textBox2.Text " " textBox3.Text " " textBox3.Text " ";
dataGridView1.Rows[0].Cells[0].Value = textBox1.Text;
dataGridView1.Rows[0].Cells[1].Value = textBox2.Text;
dataGridView1.Rows[0].Cells[2].Value = textBox3.Text;
dataGridView1.Rows[0].Cells[3].Value = textBox4.Text;
sr.WriteLine(input);//开始写入值
//sw.Write(input);
sr.Close();
MessageBox.Show("添加商品信息成功!", "成功提示");
// ReadInfo();
sr.Close();
fs.Close();
//}
//catch (Exception)
//{
// MessageBox.Show("出现错误,无法继续执行!", "错误提示");
//}
}
private void button2_Click(object sender, EventArgs e)
{
/// int Index = 0;
//if (this.listView1.SelectedItems.Count > 0)//判断listview有被选中项
//{
// Index = this.listView1.SelectedItems[0].Index;//取当前选中项的index,SelectedItems[0]这必须为0
// listView1.Items[Index].Remove();
//}
}
}
}