General.bas:Declare Function GetOpenFileName Lib "comdlg32.dll" Alias _
"GetOpenFileNameA" (pOpenfilename As OPENFILENAME) As LongType OPENFILENAME
lStructSize As Long
hwndOwner As Long
hInstance As Long
lpstrFilter As String
lpstrCustomFilter As String
nMaxCustFilter As Long
nFilterIndex As Long
lpstrFile As String
nMaxFile As Long
lpstrFileTitle As String
nMaxFileTitle As Long
lpstrInitialDir As String
lpstrTitle As String
flags As Long
nFileOffset As Integer
nFileExtension As Integer
lpstrDefExt As String
lCustData As Long
lpfnHook As Long
lpTemplateName As String
End Type在调用对话框的按钮的click事件输入下面代码:Private Sub FileOpen_Click()
Dim ofn As OPENFILENAME
Dim rtn As Stringofn.lStructSize = Len(ofn)
ofn.hwndOwner = Me.hWnd
ofn.hInstance = App.hInstance
ofn.lpstrFilter = "所有文件"
ofn.lpstrFile = Space(254)
ofn.nMaxFile = 255
ofn.lpstrFileTitle = Space(254)
ofn.nMaxFileTitle = 255
ofn.lpstrInitialDir = App.Path
ofn.lpstrTitle = "打开文件"
ofn.flags = 6148rtn = GetOpenFileName(ofn)If rtn >= 1 Then
msgbox ofn.lpstrFile
Else
msgbox "Cancel Was Pressed"
End If
End Sub

解决方案 »

  1.   

    Type;
    OPENFILENAME;
    EndType;    
        private long lStructSize;
        
        private long hwndOwner;
        
        private long hInstance;
        
        private string lpstrFilter;
        
        private string lpstrCustomFilter;
        
        private long nMaxCustFilter;
        
        private long nFilterIndex;
        
        private string lpstrFile;
        
        private long nMaxFile;
        
        private string lpstrFileTitle;
        
        private long nMaxFileTitle;
        
        private string lpstrInitialDir;
        
        private string lpstrTitle;
        
        private long flags;
        
        private int nFileOffset;
        
        private int nFileExtension;
        
        private string lpstrDefExt;
        
        private long lCustData;
        
        private long lpfnHook;
        
        private string lpTemplateName;
        
        [DllImport("comdlg32.dll", EntryPoint="GetOpenFileNameA")]
        static extern long GetOpenFileName(OPENFILENAME pOpenfilename);
      

  2.   

    private void FileOpen_Click() {
            OPENFILENAME ofn;
            string rtn;
            ofn.lStructSize = ofn.Length;
            ofn.hwndOwner = this.hWnd;
            ofn.hInstance = App.hInstance;
            ofn.lpstrFilter = "@\t";
            ofn.lpstrFile = Space(254);
            ofn.nMaxFile = 255;
            ofn.lpstrFileTitle = Space(254);
            ofn.nMaxFileTitle = 255;
            ofn.lpstrInitialDir = App.Path;
            ofn.lpstrTitle = "S\0";
            ofn.flags = 6148;
            rtn = GetOpenFileName(ofn);
            if ((rtn >= 1)) {
                msgbox;
                ofn.lpstrFile;
            }
            else {
                msgbox;
                "Cancel Was Pressed";
            }
        }
      

  3.   

    Type;
    OPENFILENAME;
    EndType;这里就不能通过
      

  4.   

    Type;
    OPENFILENAME;
    EndType;
    这个用struct 结构代替即可
      

  5.   

    public struct OPENFILENAME
        {
    public long lStructSize;
        
    public long hwndOwner;
        
    public long hInstance;
        
    public string lpstrFilter;
        
    public string lpstrCustomFilter;
        
    public long nMaxCustFilter;
        
    public long nFilterIndex;
        
    public string lpstrFile;
        
    public long nMaxFile;
        
    public string lpstrFileTitle;
        
    public long nMaxFileTitle;
        
    public string lpstrInitialDir;
        
    public string lpstrTitle;
        
    public long flags;
        
    public int nFileOffset;
        
    public int nFileExtension;
        
    public string lpstrDefExt;
        
    public long lCustData;
        
    public long lpfnHook;
        
    public string lpTemplateName;
     
    }
    [DllImport("comdlg32.dll", EntryPoint="GetOpenFileName")]
    public static extern long GetOpenFileName (
    ref OPENFILENAME pOpenfilename
    );
    定义部分可以写成这样!
    下面的就不可以了!
    我要在webfrom中调用!!会出现
    c:\inetpub\wwwroot\WebApplication2\WebForm1.aspx.cs(106): “WebApplication2.WebForm1.OPENFILENAME”并不包含对“Length”的定义
    c:\inetpub\wwwroot\WebApplication2\WebForm1.aspx.cs(107): “WebApplication2.WebForm1”并不包含对“hWnd”的定义
    c:\inetpub\wwwroot\WebApplication2\WebForm1.aspx.cs(108): 找不到类型或命名空间名称“App”(是否缺少 using 指令或程序集引用?)
    c:\inetpub\wwwroot\WebApplication2\WebForm1.aspx.cs(110): 名称“Space”在类或命名空间“WebApplication2.WebForm1”中不存在
    c:\inetpub\wwwroot\WebApplication2\WebForm1.aspx.cs(112): 名称“Space”在类或命名空间“WebApplication2.WebForm1”中不存在
      

  6.   

    private delegate int OFNHookProcDelegate(int hdlg, int msg, int wParam, int lParam);
    [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
    private struct OPENFILENAME
    {
    public int lStructSize; 
    public IntPtr hwndOwner; 
    public int hInstance; 
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrFilter;
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrCustomFilter; 
    public int nMaxCustFilter; 
    public int nFilterIndex; 
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrFile; 
    public int nMaxFile; 
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrFileTitle; 
    public int nMaxFileTitle; 
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrInitialDir; 
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrTitle; 
    public int Flags; 
    public short nFileOffset; 
    public short nFileExtension; 
    [MarshalAs(UnmanagedType.LPTStr)] public string lpstrDefExt; 
    public int lCustData; 
    public OFNHookProcDelegate lpfnHook;
    [MarshalAs(UnmanagedType.LPTStr)] public string lpTemplateName;
    //only if on nt 5.0 or higher
    public int pvReserved;
    public int dwReserved;
    public int FlagsEx;
    }
      

  7.   

    http://www.developerfusion.co.uk/utilities/convertvbtocsharp.aspx
    去转一下就可以了,记得给分哦:)
      

  8.   

    上面是定义
    这里是ClickOPENFILENAME ofn = new OPENFILENAME();ofn.lStructSize       = Marshal.SizeOf( ofn );//76,88
    ofn.lpstrFilter       = m_Filter.Replace('|', '\0') + '\0';
    ofn.lpstrFile         = m_FileName + new string(' ',512);
    ofn.nMaxFile          = ofn.lpstrFile.Length;
    ofn.lpstrFileTitle    = System.IO.Path.GetFileName(m_FileName) + new string(' ', 512);
    ofn.nMaxFileTitle     = ofn.lpstrFileTitle.Length;
    ofn.lpstrTitle        = m_Title;
    ofn.lpstrDefExt       = m_DefaultExt;
    ofn.lpstrInitialDir   = m_InitDir;
    ofn.lpTemplateName    = "CustomDialog";
    ofn.hwndOwner = Form.ActiveForm.Handle;

    ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_NOTESTFILECREATE | OFN_ENABLEHOOK | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT | OFN_ALLOWMULTISELECT | OFN_FILEMUSTEXIST;ofn.lpfnHook = new OFNHookProcDelegate(HookProc);
    if (System.Environment.OSVersion.Platform != PlatformID.Win32NT)
    {
    ofn.lStructSize -= 12;
    }
    if (!GetOpenFileName(ref ofn))
    {
    int ret = CommDlgExtendedError();
    if (ret!=0)
    {
    throw new ApplicationException("Couldn't show file open dialog - " + ret.ToString());
    } return DialogResult.Cancel;
    }
    private int HookProc(int hdlg, int msg, int wParam, int lParam)
    {
          switch (msg)
           {
                case WM_INITDIALOG: 
                        MoveWindow((IntPtr)GetParent(hdlg), 10, 10, 1000, 1000, 1);
                        break;            }
                return 0;
            }
    在WinForm中没问题
    但是在WebForm中就不知道了
      

  9.   

    有没有能在webfrom运行的!!大侠快出手啊
      

  10.   

    to
    lovefootball(蟑螂(生活就是扯淡--做人要放低姿态)) 
    在webfrom中不行
      

  11.   

    你的代码在VB.net的WebForm中能跑通???如果跑不通在C#里面当然也不行!!!这个API我觉得在WebForm里面不能用
      

  12.   

    如果你想调用对话框
    可以看看FileSystemObject
      

  13.   

    还有可以找一个vb.net转c#的工具,网上多的是,小程序还是可以自己转的,很简单,一行一行来