Search code examples
c#file-ioopenfiledialog

How can i open files of different type with different algorithms in OpenFile dialog?


I want to open 2 type of Files(.txt and .dat) with an OpenFileDialog, each type of file should be opened with an different algorithm. the OpenFile method gets the Stream of the File that i´m openning. but, can i get the type of that file? or is there another way to differentiate the files?


Solution

  • If we talk about winforms, then you have the filename available and you can check the extension before opening the file..

    var dialog = new OpenFileDialog();
    
    if (dialog.ShowDialog() == DialogResult.OK)
    {
        var extension = Path.GetExtension(dialog.FileName);
    
        // do what you need
    }