知识日记1

王朝学院·作者佚名  2016-08-27
窄屏简体版  字體: 小  |  中  |  大  |  超大  

这段时间做了一个小监控工具,涉及txt文本的操作,整理总结,防止自己忘记。

零碎知识点:

1、时间转化

//时间转换

string xx = "2014/12/19 10:05:10";

DateTime dt = DateTime.ParseExact(xx, "yyyy/MM/dd HH:mm:ss", null);

2、获取本机ip

//获取本地IP

IPHostEntry ipentry = Dns.GetHostByName(Dns.GetHostName());

string ip = ipentry.AddressList[0].ToString();

3、获取文件执行路径、执行文件名

//获取执行路径

//\/:*?"<>|

string path =application.StartupPath+"-"+Application.PRoductName;

4、读取文件夹下面的所有文件

DirectoryInfo TheFolder = new DirectoryInfo(txtPath.Text);

FileInfo[] dirInfo = TheFolder.GetFiles();

foreach (FileInfo file in dirInfo)

{

lbox.Items.Add(file.DirectoryName + "\\" + file.Name);

}

5、c#程序异常发出报警的声音

//调用系统dll 发出报警声

[DllImport("kernel32.dll")]

public static extern bool Beep(int freq, int duration);

public void play()

{

Beep(800, 3000);

}

txt文件操作

1、读txt文件

//1次读取所有文本内容try{

StreamReader srd =newStreamReader(strpath, Encoding.UTF8);string result =srd.ReadToEnd();

txtRead.Text =result;

srd.Close();

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

//循环每行读取try{

StreamReader srd=newStreamReader(strpath, Encoding.UTF8);stringstr =srd.ReadLine();while(str !=null)

{

txtRead.Text+= str +"\r\n";

str=srd.ReadLine();

}

srd.Close();

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

2、写txt文件

//接着已有内容换行继续写入try{

StreamWriter swt=File.AppendText(strpath);

swt.WriteLine(str);

swt.Close();

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

//在文本中写入文本,替换原有内容try{

StreamWriter swt=newStreamWriter(strpath);

swt.Write(str);

swt.Close();

}catch(Exception ex)

{

MessageBox.Show(ex.Message);

}

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航