实例介绍
【实例截图】
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public static string txtpath;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
listBox1.Sorted = false;
//if (openFileDialog1.ShowDialog() == DialogResult.OK)
//{
// txtPath = openFileDialog1.FileName;
//}
//else
//{
// this.Close();
//}
//rd(txtpath);
string txtPath = "C:/.......";
//path = path DateTime.Today.ToString("yyyyMMdd") "Log.txt";
txtPath = txtPath "20180619Log.txt";
ReadTxtToLst(listBox1,txtPath);
}
private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
}
private void TestLog(string msg)
{
//string path = "C:/.......";
//if (!Directory.Exists(path))
//{
// Directory.CreateDirectory(path);
//}
string path = "C:/.......";
//path = path DateTime.Today.ToString("yyyyMMdd") "Log.txt";
path = path "20180619Log.txt";
//if (!Directory.Exists(path))
//{
// Directory.CreateDirectory(path);
//}
//if (File.Exists(Server.MapPath("~/upimg/Data.html")))
//{
// Response.Write("Yes");
// //存在文件
//}
//else
//{
// Response.Write("No");
// //不存在文件
// File.Create(MapPath("~/upimg/Data.html"));//创建该文件
//}
FileStream fs = new FileStream(path, FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.GetEncoding("GB2312"));
//sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") " " msg);
sw.WriteLine(monthCalendar1.SelectionStart.ToString("yyyy-MM-dd") " " msg);
sw.Close();
fs.Close();
}
private void button1_Click(object sender, EventArgs e)
{
TestLog(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
listBox1.Items.Clear();
//string path = "C:/.......";
//path = path DateTime.Today.ToString("yyyyMMdd") "Log.txt";
//string path="";
//path = path Form1.d;
//MessageBox.Show("sadsa", path);
//StreamReader sr = new StreamReader(path); //path是要读取的文件的完整路径
//String str_read = sr.ReadToEnd(); //从开始到末尾读取文件的所有内容,str_read 存放的就是读取到的文本
//sr.Close();
//string[] arr = str_read.Split('\n');
//foreach (string i in arr)
// listBox1.Items.Add(i.ToString());
//string[] strs = System.IO.File.ReadAllLines(path, System.Text.Encoding.Default);
//foreach (string str in strs)
//{
// listBox1.Items.Add(str.ToString());
//}
string path = "C:/.......";
//path = path DateTime.Today.ToString("yyyyMMdd") "Log.txt";
path = path "20180619Log.txt";
ReadTxtToLst(listBox1, path);
}
private void listBox1_DragLeave(object sender, EventArgs e)
{
}
private void listBox1_DoubleClick(object sender, EventArgs e)
{
listBox1.Sorted = false;
string path = "C:/.......";
//path = path DateTime.Today.ToString("yyyyMMdd") "Log.txt";
path = path "20180619Log.txt";
int m = listBox1.SelectedIndex;
MessageBox.Show("", m.ToString());
listBox1.Items.Remove(listBox1.SelectedItem);
// DLst(path);
var strs = System.IO.File.ReadAllLines(path, Encoding.Default).ToList();
strs.RemoveAt(m);//删除第0行
System.IO.File.WriteAllLines(path, strs, Encoding.Default);
}
private void WriteLstToTxt(ListBox lst, string spath) //listbox 写入txt文件
{
int count = lst.Items.Count;
StreamWriter _wstream = new StreamWriter(spath);
for (int i = 0; i < count; i )
{
string data = lst.Items[i].ToString();
_wstream.Write(data);
_wstream.WriteLine();
}
_wstream.Flush();
_wstream.Close();
}
private void ReadTxtToLst(ListBox lst, string spath) //listbox 读取txt文件
{
StreamReader _rstream = new StreamReader(spath, System.Text.Encoding.Default);
string line;
while ((line = _rstream.ReadLine()) != null)
{
lst.Items.Add(line);
}
_rstream.Close();
}
private void DLst(string spath) //txt清除
{
System.IO.File.WriteAllText(spath, string.Empty);
}
private void button3_Click(object sender, EventArgs e)//写入txt文件
{
string path = "C:/.......";
path = path DateTime.Today.ToString("yyyyMMdd") "Log.txt";
WriteLstToTxt(listBox1, path);
}
public void rd(string spath)
{
string d;
d = spath;
}
public static string d { get; set; }
private void textBox1_TextChanged(object sender, EventArgs e)
{
listBox1.SelectedItem = textBox1.Text;
}
private void listBox1_Click(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button3_Click_1(object sender, EventArgs e)
{
string path = "C:/.......";
path = path "20180619Log.txt";
if (listBox1.Sorted == true)
{
listBox1.Sorted = false;
listBox1.Items.Clear();
ReadTxtToLst(listBox1, path);
//(1)button1.PerformClick();按钮调用方法
//(2)button1_Click(null,null);
//(3)button_Click(null,new EventArgs());
}
else {
listBox1.Sorted = true;
}
}
}
}
.
├── WindowsFormsApplication6
│ ├── Form1.Designer.cs
│ ├── Form1.cs
│ ├── Form1.resx
│ ├── Program.cs
│ ├── Properties
│ │ ├── AssemblyInfo.cs
│ │ ├── Resources.Designer.cs
│ │ ├── Resources.resx
│ │ ├── Settings.Designer.cs
│ │ └── Settings.settings
│ ├── WindowsFormsApplication6.csproj
│ ├── bin
│ │ └── Debug
│ │ ├── WindowsFormsApplication6.exe
│ │ ├── WindowsFormsApplication6.pdb
│ │ ├── WindowsFormsApplication6.vshost.exe
│ │ └── WindowsFormsApplication6.vshost.exe.manifest
│ └── obj
│ └── x86
│ └── Debug
│ ├── DesignTimeResolveAssemblyReferences.cache
│ ├── DesignTimeResolveAssemblyReferencesInput.cache
│ ├── TempPE
│ ├── WindowsFormsApplication6.Form1.resources
│ ├── WindowsFormsApplication6.Properties.Resources.resources
│ ├── WindowsFormsApplication6.csproj.FileListAbsolute.txt
│ ├── WindowsFormsApplication6.csproj.GenerateResource.Cache
│ ├── WindowsFormsApplication6.csprojResolveAssemblyReference.cache
│ ├── WindowsFormsApplication6.exe
│ └── WindowsFormsApplication6.pdb
├── WindowsFormsApplication6.sln
└── 好例子网_WindowsFormsApplication6.zip
8 directories, 25 files
标签: 备忘录
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论