使用的FileStream打开文件在C# [英] Opening a File in C# using FileStream

查看:190
本文介绍了使用的FileStream打开文件在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图打开我打算转换为十六进制的二进制文件,但我遇到的问题通过的FileStream,

读取文件

 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
    openFD.Title =插入BIN文件;
    openFD.InitialDirectory =C:; //]选择默认位置打开文件
    openFD.FileName =; // Iniitalizes文件名
    openFD.Filter =二进制文件| * .bin文件|文本文件| * .TXT //滤波器文件允许通过所选择的类型的    如果(openFD.ShowDialog()!= DialogResult.Cancel)
    {
        chosenFile = openFD.FileName;
        字符串的目录路径= Path.GetDirectoryName(chosenFile); //返回目录和引用文件的文件名
        字符串目录名= System.IO.Path.GetDirectoryName(openFD.FileName); //返回与其中refernce文件正确的目录
        richTextBox1.Text + =目录名称;
        richTextBox1.Text + = chosenFile;
        的FileStream InputBin =新的FileStream(
            目录路径,FileMode.Open,FileAccess.Read,FileShare.None);
    }
}

我收到一个错误说,到路径的访问被拒绝,任何想法?

现在,我已经得到了错误的照顾我已经遇到了另一个问题,我可以读取二进制文件,但我想它显示为十六进制文件,我不知道我做错了,但我M没有得到十六进制的输出,这似乎是int值...

 如果(openFD.ShowDialog()!= DialogResult.Cancel)
        {            chosenFile = openFD.FileName;
            字符串的目录路径= Path.GetDirectoryName(chosenFile);
            字符串目录名= System.IO.Path.GetDirectoryName(openFD.FileName);
            使用(的FileStream流=新的FileStream(chosenFile,FileMode.Open,FileAccess.Read))
            {
                大小=(INT)stream.Length;
                数据=新的字节[大小]
                stream.Read(数据,0,大小);
            }            而(printCount<大小)
            {
                richTextBox1.Text + =数据[printCount]
                printCount ++;
            }


解决方案

您code为miscommented

 字符串目录路径= Path.GetDirectoryName(chosenFile); //返回目录和引用文件的文件名

没有文件名,它的目录路径。你想:

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

Addtionally,如果我猜的基础上你的意图,你应该更新您的完整功能是:

 私人无效的button1_Click(对象发件人,EventArgs的发送)
{
    openFD.Title =插入BIN文件;
    openFD.InitialDirectory =C:; //]选择默认位置打开文件
    openFD.FileName =; // Iniitalizes文件名
    openFD.Filter =二进制文件| * .bin文件|文本文件| * .TXT //滤波器文件允许通过所选择的类型的    如果(openFD.ShowDialog()!= DialogResult.Cancel)
    {
        chosenFile = openFD.FileName;        richTextBox1.Text + = chosenFile; //你可能想,除非你的意思是附加的东西已经在那里与=替换此。        的FileStream InputBin =新的FileStream(chosenFile,FileMode.Open,FileAccess.Read,FileShare.None);
    }
}

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,

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...

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

解决方案

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

这篇关于使用的FileStream打开文件在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆