zoukankan      html  css  js  c++  java
  • CRC校验程序3:通过窗体程序打开txt文件获取数据源,进行逐字节CRC校验

            在上一篇中,生成输出的CRC.exe将要成为这个窗体应用的内置模块。

            新建一个windows窗体应用程序,叫做CRClick。将应用程序CRC.exe从Console应用程序中找到,复制到CRClick文件夹的Debuge文件夹下。同时还有测试用的Test.txt。

            打开窗体设计页面,插入两个Textbox和一个按钮,长度Textbox用来显示被校验的文件的绝对路径,短Textbox显示最终校验码。按钮激发选择文件的窗口。设计如下:

            对于按钮的触发事件,我们生成一个OpenFileDialog控件,用它来读取、打开文件。具体代码如下:文件为Form.cs

     1 using System;
     2 using System.Collections.Generic;
     3 using System.ComponentModel;
     4 using System.Data;
     5 using System.Diagnostics;
     6 using System.Drawing;
     7 using System.IO;
     8 using System.Linq;
     9 using System.Text;
    10 using System.Threading.Tasks;
    11 using System.Windows.Forms;
    12 
    13 namespace CRClick
    14 {
    15     public partial class CRClick : Form
    16     {
    17         public CRClick()
    18         {
    19             InitializeComponent();
    20         }
    21 
    22         private void button1_Click(object sender, EventArgs e)
    23         {
    24                 Stream myStream = null;
    25      OpenFileDialog openFileDialog1 = new OpenFileDialog();
    26      openFileDialog1.InitialDirectory = @"......CRClickCRClickinDebug";
    27      openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    28      openFileDialog1.FilterIndex = 2 ;
    29      openFileDialog1.RestoreDirectory = true ;
    30 
    31      if(openFileDialog1.ShowDialog() == DialogResult.OK)
    32      {
    33          try
    34          {
    35              if ((myStream = openFileDialog1.OpenFile()) != null)
    36              {
    37                  using (myStream)
    38                  {
    39                      route.Text = openFileDialog1.FileName;
    40 
    41                      Process pro = new Process();
    42                      pro.StartInfo.FileName = "CRC.exe";
    43                      pro.StartInfo.UseShellExecute = false;
    44                      pro.StartInfo.Arguments = route.Text;
    45                      pro.StartInfo.RedirectStandardOutput = true;
    46                      pro.StartInfo.RedirectStandardInput = true;
    47                      pro.Start();
    48                      CRCResult.Text = pro.StandardOutput.ReadToEnd();
    49 
    50                  }
    51              }
    52          }
    53          catch (Exception ex)
    54          {
    55              MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
    56          }
    57      }
    58         }
    59     }
    60 }

            从39到49行,是当文件选择结束之后,点下“打开”之后产生的触发事件,将文件绝对路径显示在长条文本框中,同时调用进程CRC.exe运算,结果在短框中输出,如图:

    结果是

    如此,一个CRC校验程序就完成了!

    需要源代码和源文件的可以留言(^>^)

  • 相关阅读:
    jython 访问数据库的方法
    Server 2008安装FTP的简单教程
    如何实现Android重启应用程序代码 ?
    android 应用程序自适应屏幕大小
    Android Dialog用法
    2008Server错误
    7种形式的Android Dialog使用举例
    ADB使用方法
    调用手机震动
    android小记之FTP文件上传
  • 原文地址:https://www.cnblogs.com/Dukechris/p/4396957.html
Copyright © 2011-2022 走看看