2012-07-24 26 views
4

我試圖打開我打算轉換爲十六進制,二進制文件,但我遇到的問題與讀經的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++; 
      } 
+0

如果你設置了一個相對路徑,或者硬編碼你的路徑,它是否工作? – 2012-07-24 18:51:26

+0

例如,在十六進制編輯器中查看文件的前4個字節如下... 58 58 58 58,當我在我的程序中執行它時,它輸出8888888 ..不確定是否有幫助 – Bubo 2012-07-24 20:42:32

+0

@VRKnight如果你有新問題,你應該單獨提問;你的第一個問題被詢問,回答和接受,你還需要一個單獨的問題來問/答/接受。如果你認爲它是必要的,你可以隨時在後面的問題中包含這個問題的鏈接。 – 2012-12-27 20:05:03

回答

10

您的代碼miscommented

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

Addtionally,如果我猜根據你的意圖,你應該更新你的全功能是:

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

chosenFile = openFD.FileName; richTextBox1.Text + = chosenFile; - 這將目錄附加到文本框,而不是實際的文件 – Bubo 2012-07-24 19:06:46

+0

我不明白你的問題,但[FileDialog.FileName'](http://msdn.microsoft.com/en-us/library/system.windows .forms.filedialog.filename.aspx)返回所選文件的完全限定路徑,其中包括目錄和文件名。 – 2012-07-24 19:24:20

0

要回答你的第二個quesiton:

我收到一條錯誤消息,指出訪問路徑被拒絕, 有什麼想法?

現在我已經得到了錯誤照顧我碰到了 另一個問題,我可以讀取二進制文件,但我想顯示它爲 一個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