使用 FileStream 在 C# 中打开文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11637370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:37:10  来源:igfitidea点击:

Opening a File in C# using FileStream

c#binaryhexfilestream

提问by Bubo

I am trying to open a Binary file that I plan on converting to hex but I am running into issues with reading the file via FileStream,

我正在尝试打开一个打算转换为十六进制的二进制文件,但我遇到了通过 FileStream 读取文件的问题,

private void button1_Click(object sender, EventArgs e)
{
    openFD.Title = "Insert a BIN file";
    openFD.InitialDirectory = "C:"; // Chooses the default location to open the file
    openFD.FileName = " "; // Iniitalizes the File name
    openFD.Filter = "Binary File|*.bin|Text File|*.txt"; // FIlters the types of files allowed to by chosen

    if (openFD.ShowDialog() != DialogResult.Cancel)
    {
        chosenFile = openFD.FileName;
        string directoryPath = Path.GetDirectoryName(chosenFile); // Returns the directory and the file name to reference the file
        string dirName = System.IO.Path.GetDirectoryName(openFD.FileName); // Returns the proper directory with which to refernce the file 
        richTextBox1.Text += dirName;
        richTextBox1.Text += chosenFile;
        FileStream InputBin = new FileStream(
            directoryPath, FileMode.Open, FileAccess.Read, FileShare.None);
    }
}

I am receiving an error saying that the access to the path is denied, any ideas?

我收到一条错误消息,说访问路径被拒绝,有什么想法吗?

Now that I have gotten that error taken care of I have ran into another Issue, I can read the binary file, but I want to display it as a Hex file, I'm not sure what I am doing wrong but I'm not getting an output in HEX, it seems to be Int values...

现在我已经解决了这个错误,我遇到了另一个问题,我可以读取二进制文件,但我想将它显示为十六进制文件,我不确定我做错了什么,但我不是以十六进制获得输出,它似乎是 Int 值...

if (openFD.ShowDialog() != DialogResult.Cancel)
        {

            chosenFile = openFD.FileName;
            string directoryPath = Path.GetDirectoryName(chosenFile); 
            string dirName = System.IO.Path.GetDirectoryName(openFD.FileName);
            using (FileStream stream = new FileStream(chosenFile, FileMode.Open, FileAccess.Read))
            {
                size = (int)stream.Length;
                data = new byte[size];
                stream.Read(data, 0, size);
            }

            while (printCount < size)
            {
                richTextBox1.Text += data[printCount];
                printCount++;
            }

采纳答案by Jaime Torres

Your code is miscommented

您的代码注释错误

string directoryPath = Path.GetDirectoryName(chosenFile); // Returns the directory and the file name to reference the file

is not the filename, it's the directory path. You want:

不是文件名,它是目录路径。你要:

FileStream InputBin = new FileStream(chosenFile, FileMode.Open,FileAccess.Read, FileShare.None);

Addtionally, if I were to guess based on your intentions, you should update your full function to be:

此外,如果我根据您的意图进行猜测,您应该将完整功能更新为:

private void button1_Click(object sender, EventArgs e)
{
    openFD.Title = "Insert a BIN file";
    openFD.InitialDirectory = "C:"; // Chooses the default location to open the file
    openFD.FileName = " "; // Iniitalizes the File name
    openFD.Filter = "Binary File|*.bin|Text File|*.txt"; // FIlters the types of files allowed to by chosen

    if (openFD.ShowDialog() != DialogResult.Cancel)
    {
        chosenFile = openFD.FileName;

        richTextBox1.Text += chosenFile; //You may want to replace this with = unless you mean to append something that is already there.

        FileStream InputBin = new FileStream(chosenFile, FileMode.Open,FileAccess.Read, FileShare.None);
    }
}

回答by Jaime Torres

To answer your second quesiton:

回答你的第二个问题:

I am receiving an error saying that the access to the path is denied, any ideas?

Now that I have gotten that error taken care of I have ran into another Issue, I can read the binary file, but I want to display it as a Hex file, I'm not sure what I am doing wrong but I'm not getting an output in HEX, it seems to be Int values...

我收到一条错误消息,说访问路径被拒绝,有什么想法吗?

现在我已经解决了这个错误,我遇到了另一个问题,我可以读取二进制文件,但我想将它显示为十六进制文件,我不确定我做错了什么,但我不是以十六进制获得输出,它似乎是 Int 值...

Modify to use string.Format:

修改为使用string.Format:

    if (openFD.ShowDialog() != DialogResult.Cancel)
    {

        chosenFile = openFD.FileName;
        string directoryPath = Path.GetDirectoryName(chosenFile); 
        string dirName = System.IO.Path.GetDirectoryName(openFD.FileName);
        using (FileStream stream = new FileStream(chosenFile, FileMode.Open, FileAccess.Read))
        {
            size = (int)stream.Length;
            data = new byte[size];
            stream.Read(data, 0, size);
        }

        while (printCount < size)
        {
            richTextBox1.Text += string.Format( "{0:X} ", data[printCount];
            printCount++;
        }
    }

I've included an ideone example.

我已经包含了一个ideone 示例