Click here to Skip to main content
15,886,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I don't want to use SaveFileDialog class to pop dialog.
because some reasons,i want to use the api GetSaveFileName,but some parameters within c# i don't know how to define ,can anybody give demo and show me how to use this API.
My question's important is about parameter but not use dllimport.
thanks for you answer.

my demo is below,but it can't pop dialog:
C#
public partial class Form1 : Form
{
    public struct OPENFILENAMEA
    {
        public uint lStructSize;
        public uint hwndOwner;
        public uint hInstance;
        public uint lpstrFilter;
        public uint lpstrCustomFilter;
        public uint nMaxCustFilter;
        public uint nFilterIndex;
        public uint lpstrFile;
        public uint nMaxFile;
        public uint lpstrFileTitle;
        public uint nMaxFileTitle;
        public uint lpstrInitialDir;
        public uint lpstrTitle;
        public uint Flags;
        public ushort nFileOffset;
        public ushort nFileExtension;
        public uint lpstrDefExt;
        public uint lCustData;
        public uint lpfnHook;
        public uint lpTemplateName;
    }

    [DllImport("comdlg32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    public static extern bool GetSaveFileName(OPENFILENAMEA ofn);

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        OPENFILENAMEA ofn = new OPENFILENAMEA();
        bool isTrue = GetSaveFileName(ofn);

    }
}
Posted
Updated 15-Feb-15 2:53am
v3
Comments
Richard MacCutchan 15-Feb-15 8:57am    
Why? SaveFileDialog provides exactly the same functionality without the worry of trying to use a C structure in C#. And your code is missing all the initialisation of the structure so it will just crash.

1 solution

Richard is correct. You don't even initialize the structure you're supposed to be passing in so nothing is going to happen except an error being generated, which you don't even bother to go pickup with the CommDlgExtendedError[^] function.

Your definition of the OPENFILENAME structure is also very wrong. You can find a much better definition on P/Invoke.net, here[^].
 
Share this answer
 
Comments
xuyunhai 15-Feb-15 22:11pm    
How to define the savedialog as windows 2000 style,not windows xp or others.
Dave Kreskowiak 15-Feb-15 22:24pm    
I have no idea. I've never used the function and really don't have the time to go building a test app to do your research for you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900