大家好我寫了一個小代碼來打開一個特定的文本文件。現在在打開的文件對話框下的文件名下拉列表中顯示一些文件名,如果我從中選擇一個文件,然後單擊確定我想顯示一個錯誤信息爲錯誤的文件選擇。限制,如果不是一個正確的文件使用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(字符串,字符串)」不能以一個實例引用來訪問轉換;與類型名限定它,而不是
所以任何一個可以請該怎麼辦...
我不知道我理解的問題 - 你如何識別「錯誤」的文件? – 2010-07-16 10:17:15
您是否在提問如何在文件對話框中彈出的下拉式自動完成菜單中讀取名稱? – 2010-07-16 10:19:27
使用此代碼,我收到錯誤\t 1 - 名稱'myStream'在當前上下文中不存在。 mystream變量在哪裏被聲明? – 2011-03-22 13:59:42