• 大小: 37KB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2021-06-07
  • 语言: 其他
  • 标签:

资源简介

创建一个windows应用程序,打开一个标准的对话框,指定读取的文件,然后把改文件显示为二进制,在多行文本中逐个显示文件中的每个字节,每行显示16个字节。以16进制格式显示该字节的值。排列整齐,采用FileStream类

资源截图

代码片段和文件信息

using System;
using System.ComponentModel;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace BinaryFileReader
{
    public partial class Form1 : Form
    {
        private readonly OpenFileDialog chooseOpenFileDialog = new OpenFileDialog();  //文件对话框
        private string chosenFile;            //当前文件路径                                 

       //处理菜单和文件对话框的程序
        public Form1()
        {
            InitializeComponent();

            menuFileOpen.Click += OnFileOpen; 
                
            chooseOpenFileDialog.FileOk +=OnOpenFileDialogOK;
        }

        private void OnFileOpen(object Sender EventArgs e)
        {
            chooseOpenFileDialog.ShowDialog();
        }

        private void OnOpenFileDialogOK(object Sender CancelEventArgs e)
        {
            chosenFile = chooseOpenFileDialog.FileName;
            Text = Path.GetFileName(chosenFile);//从其获取文件名和扩展名的路径字符串。 
            DisplayFile();
        }
        //读取选中的文件并显示;
        private void DisplayFile()
        {
            int nCols = 16;
            FileStream inStream = new FileStream(chosenFile FileMode.OpenFileAccess.Read);//实例化FileStream
            long nBytesToRead = inStream.Length;  //确定文件有多少个字节;
            //if (nBytesToRead > 65536 / 4)   
            //    nBytesToRead = 65536 / 4;
            int nLines = (int)(nBytesToRead / nCols) ;//计算总共显示多少行;
            string[] lines = new string[nLines];
            int nBytesRead = 0;//读取的字节数
            for (int i = 0; i < nLines; i++)
            {
                StringBuilder nextLine = new StringBuilder();//构造每一文本行
                //nextLine.Capacity = 4 * nCols;
                for (int j = 0; j < nCols; j++)
                {
                    int nextByte = inStream.ReadByte();
                    nBytesRead++;
                    //if (nextByte < 0 || nBytesRead > 65536)
                    //    break;
                    char nextChar = (char)nextByte;//强制转换为字符
                   
                    if (nextChar < 16)  //值小于16的字符的显示方法
                        nextLine.Append(“ x0“ + string.Format(“{01:X}“ (int)nextChar));
                    //else if
                    //    (char.IsLetterOrDigit(nextChar) || char.IsPunctuation(nextChar))
                    //    nextLine.Append(“  “ + nextChar + “  “);
                    else
                        nextLine.Append(“ x“ + string.Format(“{02:X}“ (int)nextChar));
                                                            
                }
                lines[i] = nextLine.ToString();
            }
            inStream.Close();
            //textBoxContents.Lines = lines;
            richTextBox1.Lines = lines;
        }

     }
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件       9216  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\bin\Debug\BinaryFileReader.exe

     文件      22016  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\bin\Debug\BinaryFileReader.pdb

     文件      14328  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\bin\Debug\BinaryFileReader.vshost.exe

     文件        490  2007-07-21 01:33  BinaryFileReader\BinaryFileReader\bin\Debug\BinaryFileReader.vshost.exe.manifest

     目录          0  2011-03-31 11:19  BinaryFileReader\BinaryFileReader\bin\Debug

     目录          0  2011-03-31 11:18  BinaryFileReader\BinaryFileReader\bin

     文件       3776  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\BinaryFileReader.csproj

     文件       3009  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\Form1.cs

     文件       3163  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\Form1.Designer.cs

     文件       6011  2011-03-31 14:42  BinaryFileReader\BinaryFileReader\Form1.resx

     文件       2182  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\obj\Debug\BinaryFileReader.csproj.FileListAbsolute.txt

     文件        847  2011-03-31 14:42  BinaryFileReader\BinaryFileReader\obj\Debug\BinaryFileReader.csproj.GenerateResource.Cache

     文件       9216  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\obj\Debug\BinaryFileReader.exe

     文件        180  2011-03-31 14:42  BinaryFileReader\BinaryFileReader\obj\Debug\BinaryFileReader.Form1.resources

     文件      22016  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\obj\Debug\BinaryFileReader.pdb

     文件        180  2011-03-31 11:19  BinaryFileReader\BinaryFileReader\obj\Debug\BinaryFileReader.Properties.Resources.resources

     目录          0  2011-03-31 11:18  BinaryFileReader\BinaryFileReader\obj\Debug\TempPE

     目录          0  2011-04-01 10:16  BinaryFileReader\BinaryFileReader\obj\Debug

     目录          0  2011-03-31 11:18  BinaryFileReader\BinaryFileReader\obj

     文件        468  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\Program.cs

     文件       1422  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\Properties\AssemblyInfo.cs

     文件       2545  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\Properties\Resources.Designer.cs

     文件       5612  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\Properties\Resources.resx

     文件        982  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\Properties\Settings.Designer.cs

     文件        249  2008-03-02 16:19  BinaryFileReader\BinaryFileReader\Properties\Settings.settings

     目录          0  2008-03-06 12:54  BinaryFileReader\BinaryFileReader\Properties

     目录          0  2011-04-01 10:16  BinaryFileReader\BinaryFileReader

     文件        938  2008-03-02 16:19  BinaryFileReader\BinaryFileReader.sln

    ..A..H.     15872  2011-04-01 11:09  BinaryFileReader\BinaryFileReader.suo

     目录          0  2011-03-31 11:24  BinaryFileReader

............此处省略3个文件信息

评论

共有 条评论

相关资源