Reputation: 115
I am using openfiledailog box using ike this but i want to do same functionality without using Microsoft.Win32 refrence
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".png";
dlg.Filter = "JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif";
//Nullable<bool> result =
dlg.ShowDialog();
Upvotes: 2
Views: 120
Reputation: 125197
Option 1
You can create your own dialog that shows list of files and let the user select file.
Option 2
You can use GetOpenFileName
instead:
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
And here is pinvoke.net page.
Here is a working sample of what you need:
[DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public class OpenFileName
{
public int structSize = 0;
public IntPtr dlgOwner = IntPtr.Zero;
public IntPtr instance = IntPtr.Zero;
public String filter = null;
public String customFilter = null;
public int maxCustFilter = 0;
public int filterIndex = 0;
public String file = null;
public int maxFile = 0;
public String fileTitle = null;
public int maxFileTitle = 0;
public String initialDir = null;
public String title = null;
public int flags = 0;
public short fileOffset = 0;
public short fileExtension = 0;
public String defExt = null;
public IntPtr custData = IntPtr.Zero;
public IntPtr hook = IntPtr.Zero;
public String templateName = null;
public IntPtr reservedPtr = IntPtr.Zero;
public int reservedInt = 0;
public int flagsEx = 0;
}
private void OpenButton_Click(object sender, EventArgs e)
{
OpenFileName openFileName = new OpenFileName();
openFileName.structSize = Marshal.SizeOf(openFileName);
openFileName.filter = "JPEG Files (*.jpeg)\0*.jpeg\0PNG Files (*.png)\0*.png\0JPG Files (*.jpg)\0*.jpg\0GIF Files (*.gif)\0*.gif\0";
openFileName.file = new String(new char[256]);
openFileName.maxFile = openFileName.file.Length;
openFileName.fileTitle = new String(new char[64]);
openFileName.maxFileTitle = openFileName.fileTitle.Length;
openFileName.title = "Open";
openFileName.defExt = "png";
if (GetOpenFileName(openFileName))
{
MessageBox.Show(openFileName.file);
}
}
Based on MSDN and PInvoke and Arie Code.
Upvotes: 2