【问题标题】:Opening a File in C# using FileStream使用 FileStream 在 C# 中打开文件
【发布时间】:2012-07-23 03:43:15
【问题描述】:

我正在尝试打开一个我计划转换为十六进制的二进制文件,但我在通过 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);
    }
}

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

现在我已经解决了这个错误,我遇到了另一个问题,我可以读取二进制文件,但我想将它显示为 Hex 文件,我不确定我做错了什么,但我'没有得到十六进制的输出,它似乎是 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++;
            }

【问题讨论】:

  • 如果你设置一个相对路径,或者硬编码你的路径,它会起作用吗?
  • 例如,在十六进制编辑器中查看文件的前 4 个字节如下...58 58 58 58,当我在程序中执行此操作时,它会输出 8888888.. 不确定是否这有助于不
  • @VRKnight 如果您有新问题,您应该单独询问;您的第一个问题已被提出、回答并接受,您还需要一个单独的问题来询问/回答/接受。如果您认为有必要,您可以随时在后续问题中包含指向该问题的链接以作为上下文。

标签: c# binary hex filestream


【解决方案1】:

您的代码注释错误

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

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

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

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

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);
    }
}

【讨论】:

  • chosenFile = openFD.FileName; richTextBox1.Text += selectedFile; - 这会将目录附加到文本框,而不是实际文件
  • 我不明白你的问题,但FileDialog.FileName 返回所选文件的完全限定路径,其中包括目录和文件名。
【解决方案2】:

回答你的第二个问题:

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

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

修改为使用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++;
        }
    }

我添加了一个ideone example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多