Windows File Associations

Questions similar to this appear on the Bentley Discussion Groups.

Q These questions concern Windows Explorer file associations. How does Windows associate a given application with a file extension? For example, MicroStation® is associated with the .dgn file extension. We expect that, when we double-click a file in Windows Explorer, the associated application starts automatically.

A Perhaps Windows Explorer has forgotten the file association between MicroStation® and the .dgn extension, or perhaps you want it to recognise extensions other than .dgn. For example, the MicroStation VBA developer can add .mvba as a recognised file type. In the case of the TriForma or GeoGraphics user, you want MicroStation to start with the same command-line switches used with the icon shortcut installed by the relevant Bentley Engineering Configuration.

You can fix this in at least three ways…

  1. Using Windows Explorer's Folder Options
  2. Command prompt ASSOC and FTYPE
  3. Editing the Windows Registry

Q This question also concerns Windows Explorer file associations, but with a desired result that is almost the opposite of the previous questions.

A Windows Explorer knows of the file association between MicroStation and the .dgn extension, because that association was created when you installed MicroStation. When you double-click a DGN file, MicroStation starts up and opens that file. However, in its vanilla mode it won't be using any user or project configuration file.

The CAD administrator wants Windows to start MicroStation when a user double-clicks a DGN file. But, she wants Windows to throw away the file name. Without a file name, MicroStation opens the MicroStation Manager dialog. You can explore this route to find how to stop Windows passing the file name to MicroStation.

Q What about workspaces? By default, the Windows file association set up when MicroStation is installed specifies no workspace, so if I double-click a file name MicroStation start with the default workspace.

A You can specify a workspace on the command line in the Windows file association. The default association, as discussed below, looks something like ustation.exe %L, where Windows Explorer substitutes the file path you clicked into %L.

You want the command line to include an environment variable, but evaluate it before passing the result to MicroStation. In other words, something like this: ustation.exe -wuUserWorkspace %L.

There is a way to substitute a Windows environment variable into the file association. There is a trick: you must …

  1. start MicroStation using a batch file
  2. surround the environment variable name with double sets of percent signs

Batch File

The batch file apparently does nothing except to wrap ustation.exe. Copy this one-liner to a file named ustation.bat in the same folder as ustation.exe …

rem ustation batch file passes all command-line arguments to MicroStation
"C:\Program Files\Bentley\Program\ustation.exe" %1 %2 %3 %4 %5 %6 %7 %8 %9

File Assocation

Suppose that you have a user configuration file named after the Windows USERNAME. You could tell Windows Explorer to use that configuration file like this …

"C:\Program Files\Bentley\Program\ustation.bat" -wu%%USERNAME%% %L

Since the file assocation applies to all users, whoever is logged on will start MicroStation using the settings specified in their personal user configuration file. It's up to the CAD Administrator to ensure that everyone wanting to use MicroStation has a personal UCF.

The reason that this convoluted process works is that the batch file will process %%USERNAME%%, first removing the outer percent signs, then realising that what's left is an environment variable, which it evaluates. Why the Windows Explorer shortcut can't process the environment variable I don't know: if you know the reason, or a way to get it to work, let us know!

Windows Explorer

  1. Windows Explorer
    1. Choose menu Tools|Folder Options. The Folder Options dialog opens

    2. Click the File Types tab
    3. Scroll the list Registered File Types: to find DGN
    4. In the box below, labelled Details for 'DGN' Extension, click the Advanced button

    5. The Edit File Type dialog opens. If there is an entry called open (e.g. open with MicroStation), click the Edit button
    6. If there is not one called open, click the New button
    7. In the Editing Action for Type dialog, you can associate a DGN extension with any executable you want. If you want this to be MicroStation, then this action should be something like "X:\Program Files\Bentley\Program\MicroStation\ustation.exe" %L where X is the disk drive where MicroStation is installed, and %L (that's capital letter L) tells Explorer to substitute the long file name of the file you double click. Wrap a path in quotes if it contains spaces.

    8. You can associate multiple applications with a given file exension by repeating steps 1.6 and 1.7. This means that when you right-click a file having a .dgn extension using Windows Explorer, several options will pop up. You can assign a default action, so a double-click always starts, say MicroStation, but right-click|select from list gives the option to open using PowerDraft

You can also associate a project or user workspace with a MicroStation file type. Simple append the workspace file name using the -wu or -wp switch in the Editing Action dialog. Of course, the relevant user (.ucf) or project (.pcf) workspace file must exist in MicroStation's Workspace folder …

ustation.exe -wuUserWorkspace -wpProjectworkspace %L

Command Prompt

Windows provides a facility to replicate the steps in Windows Explorer (1) at a command prompt. Unless you really like using the command prompt, then probably methods (1) or (3) are preferable.

  1. Command-prompt ASSOC and FTYPE
    1. Open a command prompt window
    2. Type ASSOC/? and read the information
    3. Type FTYPE/? and read the information
    4. These commands, used wisely, let you associate one or more file types with an executable

Windows Registry

The following applies to Windows XP. Registry contents may vary in other versions of Windows. These instructions suppose that you want to associate the .dgn extension with TriForma, GeoGraphics, or some other Bentley Engineering Configuration for MicroStation.

  1. Note MicroStation's command-line switches
    1. Look at the shortcut (i.e. right-click on the shortcut) installed with TriForma or GeoGraphics in the Windows Programs menu
    2. Note the command-line switches that follow ustation.exe

      In this example for GeoGraphics, the full text of the command-line is:
      "C:\Program Files\Bentley\Program\MicroStation\ustation.exe" -wumsgeo -wigeograph -wc"C:\Program Files\Bentley\Program\GeoGraphics\config\gglocal.cfg"

  2. Edit the Windows Registry
    1. Open the Registry editor (run regedit from the Start|Run menu)
    2. In hive HKEY_CLASSES_ROOT, search for MicroStation DGN File
    3. Save this current setting. Right-click, and choose Export from the shortcut menu. Save to something like MicroStation-default-assoc.reg. If something subsequently goes wrong, just double-click the .reg file to apply the old settings
    4. Open the key MicroStation DGN File\shell\open\command This currently contains the default MicroStation path with no command-line switches

    5. Append the command-line switches you copied in (1) to ustation.exe If any path includes spaces, wrap it in double-quotes

    6. Save the new setting. Right-click MicroStation DGN File, and choose Export from the shortcut menu. Save to something like MicroStation-TriForma-assoc.reg. If you need to, you can reapply this setting by double-clicking the .reg file; or copy to another computer and double-click there to propagate your modification
    7. Close the Registry editor

    Break the Windows DGN Association

    In a large MicroStation shop, with many networked files, a particular file is likely to use a variety of settings set up by its project configuration file. If a user can open a file with a double-click, then the settings won't be defined. MicroStation won't find all its line-styles, cells, reference models, etc., and the DGN model is likely to appear incomplete.

    In this case, the CAD administrator wants to stop users opening a file with vanilla MicroStation. Rather, she wants a user to start MicroStation, which then pops the MicroStation Manager dialog.

    The real solution is in the file association stored in the Windows Registry. A phantom solution lies in Windows Explorer, and first we'll explore that method, because it's the most obvious, to see why we need to modify the Registry.

    Why does Windows think it knows best?

    We want to stop passing the file name to MicroStation when we double-click a DGN file. You can use the procedure to edit the file association the 'official' way. You can delete the %L (or %1) argument, thinking that it does just what you want. Unfortunately for you, Windows knows better: it silently restores the %1 argument. The result is that you think you fixed the problem, but when you go to test it, MicroStation continues to open the DGN file you double-clicked.

    Get One Over on Windows

    Windows Explorer settings are stored in the Windows Registry. The procedure is the 'safe' way to edit the Registry, but in this case it's too clever. You have to edit the Registry directly.

    Follow the steps described to (5). Don't append any command-line switches. Instead, you want to remove the %L (or %1) placeholder at the end of the line. The result should be simply the command to start MicroStation (ustation.exe) with no arguments. Save your changes to the Registry.

    Now start Windows Explorer and double-click a DGN file. Explorer tells MicroStation to start but, with no file name present, MicroStation pops the MicroStation Manager dialog. Just what you want!

    But remember, if you use Windows Explorer to edit this association, it's going to put that %1 argument back in.