简体   繁体   English

如何知道在c#的打开对话框中选择了哪个文件

[英]How to know which file is selected in open dialog in c#

I have to open a file dialog.我必须打开一个文件对话框。 In that I have to choose one file either an XML or MAP file.因为我必须选择一个文件,XML 或 MAP 文件。 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.如果选择的文件是 MAP 文件,那么我必须执行步骤 A,或者如果选择的文件是 XML,那么我必须执行步骤 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 ?如何知道从上面的过滤器中选择了哪个文件?

打开文件对话框

You can use: 您可以使用:

   string fileName = OpenFileDialog.Filename;

    if(fileName.EndsWith(".xml"))
    {
    //
    }
    else if(fileName.EndsWith(".map"))
    {
     //
    }

I think you can't do that while it is open. 我认为您无法在打开状态下执行此操作。

When user presses OK then pass OpenFileDialog.Filename in Path.GetExtension method or OpenFileDialog.Filename.Endswith(".xml") . 当用户按OK时,则在Path.GetExtension方法或OpenFileDialog.Filename.Endswith(".xml")传递OpenFileDialog.Filename

Check if extension is XML then do x step otherwise y step. 检查扩展名是否为XML,然后执行x步骤,否则执行y步骤。

EDIT 编辑

See for functionality that you require, there has to be an event in open file dialog. 请参阅以了解所需的功能,打开文件对话框中必须有一个事件。

There are 2 OpenFileDialog Class 有2个OpenFileDialog

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

Both have only one event OpenFileDialog.FileOK which you can look for. 两者都只有一个事件OpenFileDialog.FileOK您可以查找。

        openFileDialog1.FileName = "";
        if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string filename = openFileDialog1.FileName;

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

The FileName attribute of OpenFileDialog is the file name selected. OpenFileDialogFileName属性是所选的文件名。

You could even use similar extensions in a switch with stacked labels and use the default case for unsupported file types: 您甚至可以在带有堆叠标签的交换机中使用类似的扩展名,并对不支持的文件类型使用默认大小写:

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

Well, the above answers will work if luckily all the filters have different extensions.好吧,如果幸运的是所有过滤器都有不同的扩展名,上面的答案将起作用。 but if we are talking about different File versions with the same extension then we can get the selected filter through this code:但是如果我们谈论的是具有相同扩展名的不同文件版本,那么我们可以通过以下代码获得选定的过滤器:

SaveFileDialog dlg = new SaveFileDialog();
        dlg.Filter = "Excel 97|*.xls|Excel 95|*.xls";
        if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            //Filer index is 1 based.
            switch (dlg.FilterIndex)
            {
                case 1:
                    //Filter name: Excel 97
                    break;
                case 2:
                    //Filter name: Excel 95
                    break;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM