【阶段总结】关于C# WinForm程序的一些应用总结

1、选择文件夹

FolderBrowserDialog folder = new FolderBrowserDialog();
folder.Description = "选择目录";
if (folder.ShowDialog() == DialogResult.OK)
{
    //返回选择文件夹的路径
    string strPath = folder.SelectedPath;
}

2、获取指定路径下指定类型文件

//获得指定路径下的指定类型的文件,并返回一个List列表
public List<string> GetAllFilesInDirectory(string path)
{
    //path为某个目录,如: “D:\Program Files”
    List<string> listFiles = new List<string>();
    DirectoryInfo dir = new DirectoryInfo(path);
    FileInfo[] inf = dir.GetFiles();
    foreach (FileInfo finf in inf)
    {
        //设定文件类型
        if (finf.Extension.Equals(".wav"))
            listFiles.Add(finf.FullName);
    }
    return listFiles;
}

3、以指定颜色将文本显示在RichTextBox中

void RichShow(RichTextBox richTextBox,string Contxt, Color color)
{
    richTextBox.SelectionColor = color;
    richTextBox.AppendText(Contxt + Environment.NewLine);
}

4、将指定路径下的目录结构显示在TreeView控件中

//设置展开treeView1的第一个节点
this.treeView1.Nodes[0].Expand();
//填充tree
private void FillTree(TreeView treeView, string path, bool isSource)
{
    treeView.Nodes.Clear(); // 清空
    // 获取系统上的所有逻辑驱动器
    try
    {
        // 获取驱动器顶级目录列表
        DirectoryInfo dir = new DirectoryInfo(path);

        // 如果获得的目录信息正确,则将它添加到 TreeView 控件中
        if (dir.Exists == true)
        {
            TreeNode newNode = new TreeNode(path);
            treeView.Nodes.Add(newNode);

            if (isSource)
            {
                GetSubDirectoryNodes(newNode, newNode.Text, true);
            }
            else
            {
                GetSubDirectoryNodes(newNode, newNode.Text, false);
            }
        }
    }
    catch (Exception e)
    {
        MessageBox.Show(e.Message);
    }

}
// 遍历子目录
private void GetSubDirectoryNodes(TreeNode parentNode, string fullName, bool getFileNames)
{
    DirectoryInfo dir = new DirectoryInfo(fullName);
    DirectoryInfo[] subDirs = dir.GetDirectories();
    // 为每一个子目录添加一个子节点
    foreach (DirectoryInfo subDir in subDirs)
    {
        // 不显示隐藏文件夹
        if ((subDir.Attributes & FileAttributes.Hidden) != 0)
        {
            continue;
        }
        TreeNode subNode = new TreeNode(subDir.Name);
        parentNode.Nodes.Add(subNode);
        // 递归调用GetSubDirectoryNodes
        GetSubDirectoryNodes(subNode, subDir.FullName, getFileNames);
    }
    // 获取目录中的文件
    if (getFileNames)
    {
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            TreeNode fileNode = new TreeNode(file.Name);
            parentNode.Nodes.Add(fileNode);
        }
    }
}

5、生成bat脚本,其他脚本类似

//可以用于读取一些预置的配置内容
public StreamReader ReadConfig(string path)
{
    StreamReader sr = new StreamReader(path, Encoding.Default);
    return sr;
}
//第一个参数:需要写入bat文件的内容,这里是一个路径
//第二个参数:bat脚本的名称
//第三个参数:配置文件存放的路径,需要读取配置文件的内容
public void WriteBat(string path, string batName, string configPath)
{
    StreamReader fsConfig = ReadConfig(configPath);
    //以创建文件的方式
    FileStream fsRunBat = new FileStream(batName, FileMode.Create);
    StreamWriter sw = new StreamWriter(fsRunBat, Encoding.GetEncoding("GB2312"));
    //利用StreamWriter写入数据
    sw.Write("set indir=" + path);

    //读取配置内容,再逐行写入bat文件
    String line;
    while ((line = fsConfig.ReadLine()) != null)
    {
        sw.Write(line.ToString());
        sw.Write("\r\n");
    }
    sw.Flush();
    sw.Close();
    fsConfig.Close();
}

6.A、调用bat脚本,传入bat脚本路径,逐行将bat执行结果显示在RichTextBox中

private void RunBat(string batPath)
{
    FileInfo file = new FileInfo(batPath);

    ProcessStartInfo psi = new ProcessStartInfo();
    psi.WorkingDirectory = file.Directory.FullName;

    //batPath传入的是相对路径,需要采用Split函数分割组合成绝对路径
    psi.FileName = file.Directory.FullName + "\\" + batPath.Split('\\')[2];
    //隐藏dos命令窗口
    psi.WindowStyle = ProcessWindowStyle.Hidden;
    psi.UseShellExecute = false;
    psi.RedirectStandardOutput = true;
    psi.CreateNoWindow = true;
    //不能接收输入,原因未知
    //psi.RedirectStandardInput = true;
    using (Process process = Process.Start(psi))
    {
        //逐行将bat执行结果显示在RichTextBox中
        //这种方式有缺陷,结果必须在bat脚本执行完才会显示
        while (true)
        {
            string str = process.StandardOutput.ReadLine();
            if (null == str)
                break;
            RichShow(str, Color.GreenYellow);
        }
        //string str = process.StandardOutput.ReadToEnd();
        //返回运行结果
        //return process.ExitCode;
    }
}

6.B、调用bat脚本,传入bat脚本路径,过程信息在dos窗口中显示

private void RunBat(string batPath)
{
    Process pro = new Process();
    FileInfo file = new FileInfo(batPath);
    pro.StartInfo.WorkingDirectory = file.Directory.FullName;
    pro.StartInfo.FileName = "run.bat";
    pro.StartInfo.CreateNoWindow = true;
    pro.Start();
    pro.WaitForExit();
    MessageBox.Show("程序运行结束!");
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值