我試圖打開我打算轉換爲十六進制,二進制文件,但我遇到的問題與讀經的FileStream文件,打開文件在C#中使用的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);
}
}
我收到一個錯誤說道路的訪問被拒絕了,有什麼想法?
現在我已經得到了錯誤照顧我已經遇到另一個問題,我可以讀取二進制文件,但我想顯示它爲一個十六進制文件,我不知道我在做什麼錯誤,但我沒有收到十六進制輸出,這似乎是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++;
}
如果你設置了一個相對路徑,或者硬編碼你的路徑,它是否工作? – 2012-07-24 18:51:26
例如,在十六進制編輯器中查看文件的前4個字節如下... 58 58 58 58,當我在我的程序中執行它時,它輸出8888888 ..不確定是否有幫助 – Bubo 2012-07-24 20:42:32
@VRKnight如果你有新問題,你應該單獨提問;你的第一個問題被詢問,回答和接受,你還需要一個單獨的問題來問/答/接受。如果你認爲它是必要的,你可以隨時在後面的問題中包含這個問題的鏈接。 – 2012-12-27 20:05:03