2010-07-16 30 views
1

大家好我寫了一個小代碼來打開一個特定的文本文件。現在在打開的文件對話框下的文件名下拉列表中顯示一些文件名,如果我從中選擇一個文件,然後單擊確定我想顯示一個錯誤信息爲錯誤的文件選擇。限制,如果不是一個正確的文件使用OpenFileDialog

我的代碼來打開特定的文本文件,如下所示

openFileDialog1.FileName = string.Empty; 
strFilePath = Directory.GetCurrentDirectory(); 
strFilePath = Directory.GetParent(strFilePath).ToString(); 
strFilePath = Directory.GetParent(strFilePath).ToString(); 
strFilePath = strFilePath + "\\ACH"; 
openFileDialog1.InitialDirectory = strFilePath; 
openFileDialog1.Filter = "(*.txt)|FileHeader*.txt"; 
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    try 
    { 
     if ((myStream = openFileDialog1.OpenFile()) != null) 
     { 
      using (myStream) 
      { 
       // Insert code to read the stream here. 
       //FileName = openFileDialog1.FileName; 
       txtFileHeader.Text = openFileDialog1.FileName.ToString(); ; 
      } 
     } 
    } 
    catch (Exception ex) 
    { 
     MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
    } 

} 

如果做任何修改,請讓我知道。


我只是寫我的代碼如下,因爲我沒有找到一個像變種任何聲明

  string compareType = StringComparison.InvariantCultureIgnoreCase; 
      string fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName); 
      string extension = Path.GetExtension(openFileDialog1.FileName); 
      if (fileName.StartsWith("FileHeader", compareType) 
       && extension.Equals(".txt", compareType)) 

我收到錯誤,

無法隱式轉換類型「System.StringComparison」來'string'

錯誤3'string.StartsWith(string,System.StringComparison)'的最佳重載方法匹配有一些無效參數

錯誤4參數「2」:不能從「字串」到「System.StringComparison」

錯誤5靜態成員「string.Equals(字符串,字符串)」不能以一個實例引用來訪問轉換;與類型名限定它,而不是

所以任何一個可以請該怎麼辦...

+0

我不知道我理解的問題 - 你如何識別「錯誤」的文件? – 2010-07-16 10:17:15

+0

您是否在提問如何在文件對話框中彈出的下拉式自動完成菜單中讀取名稱? – 2010-07-16 10:19:27

+0

使用此代碼,我收到錯誤\t 1 - 名稱'myStream'在當前上下文中不存在。 mystream變量在哪裏被聲明? – 2011-03-22 13:59:42

回答

3

怎麼這樣呢?你也可以使用正則表達式,但是我沒有在這臺機器上使用VS,並且覺得自己很懶惰。

我會添加,而你可以提交不正確的文件名。將All Files (*.*)|*.*添加到文件篩選器列表可能會更好,並允許用戶選擇他可能喜歡的任何內容。如果選擇錯誤的文件,則允許應用程序在文件讀取過程中失敗。這將是更符合大多數其他應用程序功能的預期用例。

Stream myStream; 
openFileDialog1.FileName = string.Empty; 
openFileDialog1.InitialDirectory = @"..\..\ACH"; 
openFileDialog1.Filter = "FileHeader (FileHeader*.txt)|FileHeader*.txt"; 
if (openFileDialog1.ShowDialog() == DialogResult.OK) 
{ 
    var compareType = StringComparison.InvariantCultureIgnoreCase; 
    var fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName); 
    var extension = Path.GetExtension(openFileDialog1.FileName); 
    if (fileName.StartsWith("FileHeader", compareType) 
     && extension.Equals(".txt", compareType)) 
    { 
     try 
     { 
      using (myStream = openFileDialog1.OpenFile()) 
      { 
       // Insert code to read the stream here. 
       //FileName = openFileDialog1.FileName; 
       txtFileHeader.Text = openFileDialog1.FileName; 
      } 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show("Error: Could not read file from disk. " + 
          "Original error: " + ex.Message); 
     } 
    } 
    else 
    { 
     MessageBox.Show("Invaild File Type Selected"); 
    } 
} 
+0

我得到一個錯誤爲 'string.StartsWith(string,System.StringComparison)'的最好的重載方法匹配有一些無效的參數 – Dotnet 2010-07-16 11:42:42

+0

我發現唯一的錯誤是'MessageBox.Box'。你能更新你的例子嗎? – 2010-07-16 13:22:43

+0

如果(fileName.StartsWith( 「FileHeader裏」,compareType) && extension.Equals( 「TXT」,compareType)) 此時我正在錯誤 ,我只是改變了變量如下 串compareType = Convert.ToString(StringComparison.InvariantCultureIgnoreCase); string fileName =路徑。GetFileNameWithoutExtension(openFileDialog1.FileName); 字符串擴展= Path.GetExtension(openFileDialog1.FileName); – Dotnet 2010-07-17 04:34:10

0

最後的答案和正確的

  if (openFileDialog1.ShowDialog() == DialogResult.OK) 
     { 
      StringComparison compareType = StringComparison.InvariantCultureIgnoreCase; 
      string fileName = Path.GetFileNameWithoutExtension(openFileDialog1.FileName); 
      string extension = Path.GetExtension(openFileDialog1.FileName); 
      if (fileName.StartsWith("CCD_BatchHeader", compareType) || fileName.StartsWith("PPD_BatchHeader", compareType) 
       && extension.Equals(".txt", compareType)) 
       try 
       { 

        if ((myStream = openFileDialog1.OpenFile()) != null) 
        { 
         using (myStream) 
         { 

          txtBatch.Text = openFileDialog1.FileName.ToString(); 
          myStream.Close(); 
         } 
        } 

       } 

       catch (Exception ex) 
       { 
        MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message); 
       } 
      else 
      { 
       MessageBox.Show("InvalidFile"); 
      } 
+0

這個答案和Matthew Whited的區別是什麼? – 2011-03-22 14:39:22

+0

@Brian檢查代碼兩次,你會發現不同的代碼,他給我寫我自己的代碼 – Dotnet 2011-03-22 15:04:08

相關問題