首页 > 编程语言 >c# 文件读取

c# 文件读取

时间:2024-06-15 17:55:17浏览次数:11  
标签:文件 MessageBox 读取 appNameText Show c# filePath System string

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)
{

}
}


}

标签:文件,MessageBox,读取,appNameText,Show,c#,filePath,System,string
From: https://www.cnblogs.com/l-xs/p/18249550

相关文章

  • docker没外网安装方法
    docker没外网安装方法需要先清理之前docker的依赖,避免影响安装失败1,下载docker的压缩tar包百度云Docker18.06.1地址:https://pan.baidu.com/s/1YdN9z72QutPkHBfLq06H1A密码:dvvh2,解压tar包#解压tar-xvfdocker-18.06.1-ce.tgz#将压缩的全部文件复制到/usr/bincpdoc......
  • c# excel
    usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.IO;usingSystem.Web;namespaceGetPCInformation{classExcel{publicvoidExportExcel(DataTabledt){//设置导出文件路径stringpath=HttpC......
  • Oracle 性能调优 10053事件
    思维导图10053事件概述我们在查看一条SQL语句的执行计划时,只看到了CBO最终告诉我们的执行计划结果,但是我们并不知道CBO为何要这样做。特别是当执行计划明显失真时,我们特别想搞清楚为什么CBO会做出这样的一个选择,那么就可以用10053事件来分析SQL分析过程的trace文件。同100......
  • c# 系统信息
    usingSystem;usingSystem.Windows.Forms;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Management;usingSystem.IO;usingSystem.Diagnostics;usingSystem.Net;usingSystem.Net.Sockets;usingSystem.Runtime.InteropServices;namespaceGetPCInfo......
  • 一篇文章教你如何解决vs编译器中,库函数scanf的正常使用!!!
    前言:这将是我写的第二篇博文了,以后我将会坚持每天花固定时间来写博客,希望我当我写了一定数量的文章之后,我自己简单的就能够写出质量优秀文章来,加油!!!一、scanf函数无法正常使用的原因1.错误实例在vs中如果想直接实现并使用scanf函数,很抱歉直接使用,用不了,因为编译器压根就......
  • 生产环境使用10053分析Oracle的执行计划
    【问题现象】在SQL出现性能问题后,通过分析统计信息、直方图、SQLAWR、查看执行计划等,仍然找不出为什么SQL要选择差的执行计划,就需要通过10053查看这个SQL的执行计划的更详细的信息。【操作方法】通过10053事件来查看执行计划和详细的SQL解析过程,10053的trace文件提供了Oracle......
  • 算法训练(leetcode)第九天 | 232. 用栈实现队列、225. 用队列实现栈、20. 有效的括号、1
    刷题记录232.用栈实现队列225.用队列实现栈20.有效的括号1047.删除字符串中的所有相邻重复项232.用栈实现队列leetcode题目地址考察栈与队列之间的特性。栈:后进先出(先进后出)——FILO。队列:先进先出——FIFO。所以使用两个栈模拟队列,分别为in和out。当入队新......
  • Docker 入门
    Docker是一个开源的应用容器引擎,它允许开发者打包他们的应用以及应用的运行环境到一个可移植的容器中。以下是学习Docker的推荐路线:###入门阶段1.**了解容器化的概念**:  -容器化与虚拟化的区别  -容器化的优势2.**Docker简介**:  -Docker的历史和目标......
  • C++学习手册
    创建一份全面的C++学习手册是一个庞大的任务,但这里我可以为你提供一个基础的大纲和一些关键点,以帮助你开始学习C++。###C++学习手册大纲####第一部分:C++简介1.C++的历史与发展2.C++的特点3.C++的应用领域4.开发环境的搭建####第二部分:基础语法1.基本数据类型2......
  • HIT计算机系统CSAPP期末大作业
                                            计算机系统大作业题    目  程序人生-Hello’sP2P 专      业     数据科学与大数据技术                  学  号     202......