Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7402065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:38:47+00:00 2026-05-29T04:38:47+00:00

I can’t use OpenFileDialog in my application. As an alternative I use GetOpenFileName() method:

  • 0

I can’t use OpenFileDialog in my application.

As an alternative I use GetOpenFileName() method:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Reader
{
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public class OpenFileName
    {
        public int lstructSize;
        public IntPtr hwndOwner;
        public IntPtr hInstance;
        public string lpstrFilter = null;
        public string lpstrCustomFilter = null;
        public int lMaxCustomFilter;
        public int lFilterIndex;
        public string lpstrFile = null;
        public int lMaxFile = 0;
        public string lpstrFileTitle = null;
        public int lMaxFileTitle = 0;
        public string lpstrInitialDir = null;
        public string lpstrTitle = null;
        public int lFlags;
        public ushort nFileOffset;
        public ushort nFileExtension;
        public string lpstrDefExt = null;
        public int lCustData;
        public int lpfHook;
        public int lpTemplateName;
    }

    public class OpenDialog
    {
        [DllImport("Comdlg32.dll",CharSet = CharSet.Auto)]
        public static extern bool GetOpenFileName([In, Out] OpenFileName ofn);
    }
}

And then use it in OnClick event of a button like this:

OpenFileName qofn = new OpenFileName();

qofn.lstructSize = Marshal.SizeOf(qofn);
qofn.lpstrFile = "";
qofn.lMaxFile = 256;
qofn.lpstrFileTitle = "";
qofn.lMaxFileTitle = 64;
qofn.hInstance = this.Handle;
source.Text = "Wait...";
if (OpenDialog.GetOpenFileName(qofn))
{
    MessageBox.Show("ofn.file: " + qofn. lpstrFile );
}

When application runs and button is clicked and I try to open file this is what happens:

1st try:

it returns the path to my file, but instead of
c:\dira\dirb\dirc\filename.ext
I have
c:\dira\dirb\dircfilename.ext
without ‘\’ before the name of the file

2nd try

Everything is OK

next:
there are random crashes, e.g. random access violation, or GUI freezes and application’s process can’t be kiled even in task manager, or other errors.

Usually dialog works 2-3 times before application crashes for good.

What is wrong with my code?

EDIT:

I can’t use OpenFileDialog. I’m using WinPE 4.0 (Windows Assessment and Deployment Kit ADK). When I try OpenFileDIalog, it throws run time error 80040111. It’s probably because the core is not supported (just like Server Core doesn’t support OpenFileDialog, the error’s the same). Probably on WinPE 4.0 they use GetOpenFileName in applications such as notepad. And it works for them.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T04:38:48+00:00Added an answer on May 29, 2026 at 4:38 am

    OK, I found this microsoft sample and it works:

    // Copyright
    // Microsoft Corporation
    // All rights reserved
    
    // OpenFileDlg.cs
    
    using System;
    using System.Text;
    using System.Runtime.InteropServices;
    
    /*
    typedef struct tagOFN { 
      DWORD         lStructSize; 
      HWND          hwndOwner; 
      HINSTANCE     hInstance; 
      LPCTSTR       lpstrFilter; 
      LPTSTR        lpstrCustomFilter; 
      DWORD         nMaxCustFilter; 
      DWORD         nFilterIndex; 
      LPTSTR        lpstrFile; 
      DWORD         nMaxFile; 
      LPTSTR        lpstrFileTitle; 
      DWORD         nMaxFileTitle; 
      LPCTSTR       lpstrInitialDir; 
      LPCTSTR       lpstrTitle; 
      DWORD         Flags; 
      WORD          nFileOffset; 
      WORD          nFileExtension; 
      LPCTSTR       lpstrDefExt; 
      LPARAM        lCustData; 
      LPOFNHOOKPROC lpfnHook; 
      LPCTSTR       lpTemplateName; 
    #if (_WIN32_WINNT >= 0x0500)
      void *        pvReserved;
      DWORD         dwReserved;
      DWORD         FlagsEx;
    #endif // (_WIN32_WINNT >= 0x0500)
    } OPENFILENAME, *LPOPENFILENAME; 
    */
    
    [ 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;
    }
    
    public class LibWrap
    {
        //BOOL GetOpenFileName(LPOPENFILENAME lpofn);
    
        [ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]                
        public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );   
    }
    
    public class App
    {
        public static void Main()
        {
            OpenFileName ofn = new OpenFileName();
    
            ofn.structSize = Marshal.SizeOf( ofn );
    
            ofn.filter = "Log files\0*.log\0Batch files\0*.bat\0";
    
            ofn.file = new String( new char[ 256 ]);
            ofn.maxFile = ofn.file.Length;
    
            ofn.fileTitle = new String( new char[ 64 ]);
            ofn.maxFileTitle = ofn.fileTitle.Length;    
    
            ofn.initialDir = "C:\\";
            ofn.title = "Open file called using platform invoke...";
            ofn.defExt = "txt";
    
            if( LibWrap.GetOpenFileName( ofn ))
            {
                Console.WriteLine( "Selected file with full path: {0}", ofn.file );
                Console.WriteLine( "Selected file name: {0}", ofn.fileTitle );
                Console.WriteLine( "Offset from file name: {0}", ofn.fileOffset );
                Console.WriteLine( "Offset from file extension: {0}", ofn.fileExtension );
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can a LINQ enabled app run on a machine that only has the .NET
Can I get a 'when to use' for these and others? <% %> <%#
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I call a java class method from inside a flash movie?
Can we use the asterisk character * for a search action in SQL database?
Can we close all known/unknown connections to database with the code? I'm using Access
Can I convert a pdf to pcl file with ghostscript? I'm using Ghostscript 9.01
Can a desktop .net library [Aforge.net] be used in a .Net CF 3.5 application?
Can someone explain what a SpanQuery is, and what are typical use cases for
Can anyone tell me how to hide the header in DevExpress gridcontrol..?? I'm using

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.