Kean Walmsley

  • About the Author
    Kean on Google+

July 2014

Sun Mon Tue Wed Thu Fri Sat
    1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31    

« CommandComplete bonus tool | Main | Creating an AutoCAD table linked to an Excel spreadsheet using .NET »

August 21, 2007

Using AutoCAD's file selection dialog from .NET

Today I started putting together some code showing how to link an Excel sheet to an AutoCAD table (watch this space - there should be something posted later this week). As I was working through it I decided enough was enough, and rather than - yet again - using the command-line to get the name of the file, I'd use the opportunity to demonstrate how to make use of AutoCAD's standard file selection dialog in your applications.

At which point I decided to cut the original post short, as I didn't want this useful (but quite short) topic to get swamped by the broader one.

Here's the C# code that asks the user to select an Excel spreadsheet:

using Autodesk.AutoCAD.ApplicationServices;

using Autodesk.AutoCAD.DatabaseServices;

using Autodesk.AutoCAD.EditorInput;

using Autodesk.AutoCAD.Runtime;

using Autodesk.AutoCAD.Windows;

namespace OpenFiles


  public class Commands



    static public void SelectSpreadsheet()


      Document doc =


      Database db = doc.Database;

      Editor ed = doc.Editor;

      OpenFileDialog ofd =

        new OpenFileDialog(

          "Select Excel spreadsheet to link",


          "xls; xlsx",




      System.Windows.Forms.DialogResult dr =


      if (dr != System.Windows.Forms.DialogResult.OK)



        "\nFile selected was \"{0}\".",






A few notes on the arguments to the OpenFileDialog constructor:

  1. The first string is the one shown in the title bar (see below)
  2. You can pass in a default filename in the second argument, but in our case it isn't appropriate
  3. You can provide multiple file extensions upon which to filter in the third argument, separated by semi-colons
  4. The fourth argument is an internal identifier used to store data about the dialog, such as size, position and last navigated path (this data will be picked up automatically the next time the identifier is used)
  5. The fifth argument is for flags: we're choosing not to copy remote files locally if selected via a URL, in this case

If we were opting to allow multiple file selection (passing AllowMultiple as an additional flag to the fifth argument), then we would access the files returned using ofd.GetFileNames() to access an array of strings.

Here's the dialog we see when we run the SS command:



TrackBack URL for this entry:

Listed below are links to weblogs that reference Using AutoCAD's file selection dialog from .NET:

blog comments powered by Disqus

10 Random Posts