c# 文件读取

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace ProcessMonitoring
{
public partial class Form2 : Form
{
string filePath = System.Environment.CurrentDirectory + @"\data.txt";
ProcessMonitoring pm = new ProcessMonitoring();

public Form2()
{
InitializeComponent();

this.IntervalTime.Text = "60";
this.ShowInTaskbar = false;//窗口在任务栏不显示
}
public static int interval_Time = 300;
private void Form2_Load(object sender, EventArgs e)
{
queryData(filePath);

}
private void button1_Click(object sender, EventArgs e)
{
insert(this.appName.Text);

}
private string insert(string appNameText)
{


// string[] files = File.ReadAllLines(filePath);
if (isExistData(filePath, appNameText) == true)
{
MessageBox.Show(appNameText + " 已存在");
}
else if(appNameText == "")
{
MessageBox.Show("增加程序不能为空");
}
else
{
//File.AppendAllText(filePath, "\r\n" + appNameText.Trim());
FileStream fs = new FileStream(filePath, FileMode.Append);
StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);

if (fs.Length==0)
{
sw.WriteLine(appNameText.Trim());
}
else
{
// MessageBox.Show(fs.Length.ToString());

sw.WriteLine(appNameText.Trim());
}
sw.Flush();
sw.Close();
//MessageBox.Show(isExistData(filePath, appNameText).ToString());
if (isExistData(filePath, appNameText) == true)
{
MessageBox.Show(appNameText + "添加成功");
queryData(filePath);
}
else
{
MessageBox.Show(appNameText + "添加失败");
queryData(filePath);
}
}
return appNameText;
}
private bool isExistData(string filePath, string appNameText)
{

string[] lines = File.ReadAllLines(filePath);
bool flag =true;

foreach (string line in lines)
{

if (line == appNameText)
{

flag = true;

}
else
{
flag = false;

}
}
if (lines.Length == 0)
{
flag = false;
}

return flag;
}

//查询已增加应用程序
public void queryData(string filePath)
{

string[] lines = File.ReadAllLines(filePath);
this.listBox1.Items.Clear();
foreach (string line in lines)
{
if ( line != "")
{
this.listBox1.Items.Add(pm.getProcessName(line));
}
}

}

private void IntervalTime_TextChanged(object sender, EventArgs e)
{
interval_Time = int.Parse(this.IntervalTime.Text);
}

private void intervalButon_Click(object sender, EventArgs e)
{
if (interval_Time == int.Parse(this.IntervalTime.Text))
{
MessageBox.Show("保存成功");
// MessageBox.Show(interval_Time.ToString());


}
else
{
MessageBox.Show("保存失败");
}

}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
// MessageBox.Show(listBox1.SelectedItem.ToString());
}

}

private void deleteAppButton_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
if (pm.deleteFile(filePath, listBox1.SelectedItem.ToString()) == true)
{
MessageBox.Show("删除成功");
this.queryData(filePath);
}
else
{
MessageBox.Show("删除失败");
};
}
}

private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{

}
}


}

posted @   菜鸟程序员lxs  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示