0

I have to open a file dialog. In that I have to choose one file either an XML or MAP file. If the choosen file is MAP file then I have to do step-A or if the choosen file is XML then I have to do step-B. My question is how to know which file is selected from the dialog box application?

OpenFileDialog fileDialog1 = new OpenFileDialog();
fileDialog1.Filter = "XML Files|*.xml|MAP Files|*.map";
fileDialog1.ShowDialog();

How to know which file is selected from the above filter ?

open file dialog

4

5 に答える 5

3

次を使用できます。

   string fileName = OpenFileDialog.Filename;

    if(fileName.EndsWith(".xml"))
    {
    //
    }
    else if(fileName.EndsWith(".map"))
    {
     //
    }
于 2012-05-18T10:10:04.703 に答える
1

開いている間はできないと思います。

ユーザーが [OK] を押したらOpenFileDialog.FilenamePath.GetExtensionメソッドまたはを渡しOpenFileDialog.Filename.Endswith(".xml")ます。

拡張子が XML かどうかを確認してからステップを実行しx、それ以外の場合はyステップを実行します。

編集

必要な機能については、[ファイルを開く] ダイアログにイベントが必要です。

2OpenFileDialogクラスあります

  1. System.Windows.Forms
  2. Microsoft.Win32

OpenFileDialog.FileOK両方とも、検索できるイベントは 1 つだけです。

于 2012-05-18T10:07:09.303 に答える
0
        openFileDialog1.FileName = "";
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string filename = openFileDialog1.FileName;

            if (File.Exists(filename))
            {
                //do something here
            }
        }

OpenFileDialogFileName属性は、選択されたファイル名です。

于 2012-05-18T10:10:53.417 に答える
0

スタック ラベルのスイッチで同様の拡張子を使用して、サポートされていないファイル タイプのデフォルト ケースを使用することもできます。

switch (extension)
{
    case "xml":
    case "xaml":
        Debug.WriteLine("It's an XML!");
        break;
    case "map":
        Debug.WriteLine("It's a map!");
        break;
    default:
        MessageBox.Show("Please select an XML or MAP file");
        // Show the dialog again
        break;
}
于 2012-05-18T10:27:21.300 に答える