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

来源:互联网 发布:光驱推荐知乎 编辑:程序博客网 时间:2024/05/17 09:24

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();//填充treeprivate 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("程序运行结束!");}
原创粉丝点击