Opening a File in C# using FileStream

爱⌒轻易说出口 提交于 2019-12-21 18:03:07

问题


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

回答1:


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



回答2:


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

Modify to use 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.



来源:https://stackoverflow.com/questions/11637370/opening-a-file-in-c-sharp-using-filestream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!