rookie
rookie

Reputation: 2923

C# open file dialog; specify name and extension?

In C#, you can specify a filter on an OpenFileDialog object.

var dlg = new OpenFileDialog();
dlg.DefaultExt = ".xml";
dlg.Filter = "XML Files|*.xml";

Is there a way to automatically select files by name? For example, if I navigated to a folder of xml files, is there any filtering option that would automatically target "myxml.xml"?

Upvotes: 2

Views: 6119

Answers (4)

Nguyen Van Thanh
Nguyen Van Thanh

Reputation: 825

Step 1: Add this method to your code:

    [DllImport("shell32.dll", SetLastError = true)]
    public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, uint dwFlags);

    [DllImport("shell32.dll", SetLastError = true)]
    public static extern void SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr bindingContext, [Out] out IntPtr pidl, uint sfgaoIn, [Out] out uint psfgaoOut);

    public static void OpenFolderAndSelectItem(string folderPath, string file)
    {
        IntPtr nativeFolder;
        uint psfgaoOut;
        SHParseDisplayName(folderPath, IntPtr.Zero, out nativeFolder, 0, out psfgaoOut);

        if (nativeFolder == IntPtr.Zero)
            return;

        IntPtr nativeFile;
        SHParseDisplayName(System.IO.Path.Combine(folderPath, file), IntPtr.Zero, out nativeFile, 0, out psfgaoOut);

        IntPtr[] fileArray;
        if (nativeFile == IntPtr.Zero)
        {
            fileArray = new IntPtr[0];
        }
        else
        {
            fileArray = new IntPtr[] { nativeFile };
        }

        SHOpenFolderAndSelectItems(nativeFolder, (uint)fileArray.Length, fileArray, 0);

        Marshal.FreeCoTaskMem(nativeFolder);
        if (nativeFile != IntPtr.Zero)
        {
            Marshal.FreeCoTaskMem(nativeFile);
        }
    }

Step 2: Call the method OpenFolderAndSelectItem(string folderPath, string file) to use.

Step 3: Enjoy!

Upvotes: 0

Erra
Erra

Reputation: 103

What you can do is either set the FileName property like this:

 dlg.FileName = "myxml.xml";

or set the Filter property like this:

dlg.Filter = "XML files|file.xml";

(it's important to check that there's no space at the end like this "file.xml ", because if there is, your file won't show up, in other words OpenFileDialog doesn't trim the Filter property)

if you don't know what the file name is beforehand, you can use DirectoryInfo and FileInfo like this:

DirectoryInfo dir = new DirectoryInfo("PATHHERE");
FileInfo[] files = dir.GetFiles();

and loop through the files to find the one you are looking for

Upvotes: 1

adv12
adv12

Reputation: 8551

Yes, you can actually set the filter to a complete filename:

dlg.Filter = "myxml Files|myxml.xml";

Note that when this filter is selected, you won't be able to select other XML files. If you simply want to default to that filename while showing and allowing selection of any XML file, go with Fᴀʀʜᴀɴ Aɴᴀᴍ's (original) answer. And now that he copied my answer into his, you can just go with his.

Upvotes: 1

Fᴀʀʜᴀɴ Aɴᴀᴍ
Fᴀʀʜᴀɴ Aɴᴀᴍ

Reputation: 6251

Yes, just set the FileName property of the OpenFileDialog like this:

dlg.FileName = "myxml.xml";

However, it would be more appropriate if you use the name in the filter. Just place it instead of the star which acts as a wildcard:

dlg.Filter = "XML Files|myxml.xml";

And always remember you can have multiple filters like this: (It may be useful in the future):

"Image Files (*.bmp, *.jpg)|*.bmp;*.jpg"
// -- OR --
"Text Files (*.txt)|*.txt|All Files (*.*)|*.*"

More documentation on filters at MSDN.

Upvotes: 8

Related Questions