Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

The FileNameEditor class lets you provide your users with a standard open file dialog box or save file dialog box from the CodeSmith Generator property grid. To use this class, you must include a reference to the CodeSmith.CustomProperties assembly in your template. You'll make the code simpler if you import the namespace as well:

Info
For a full list of available editors please see the following page.
Code Block
languagehtml/xml
<%@ Assembly Name="CodeSmith.CustomProperties" %>

...


<%@ Import Namespace="CodeSmith.CustomProperties" %>

After you include the appropriate assembly reference, you can define a property in a script block that uses the FileNameEditor:
Wiki Markup

Code Block
languagecsharp
<script runat="template">
private string 

...

_userFileName = @"c:\temp\test.txt";

...

[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
Category("Custom"), Description("User selected file.")

...

]
public string UserFileName
{

...

      get {return 

...

_userFileName;}

...

      set {_userFileName= value;}
}
</script>

When the user executes the template, the specified property will display a builder button on the property sheet (highlighted in green):

IMAGE GOES HEREImage Added

Clicking the builder button will open the specified file dialog box:

IMAGE GOES HEREImage Added

You can customize the appearance of the file dialog box by applying the FileDialogAttribute to the property. For example, consider this property definition: Wiki Markup

Code Block
languagecsharp
private string 

...

_openFileName = @"c:\temp\test.txt";

...

[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
FileDialogAttribute(FileDialogType.Open, Title="Select Input File"),
Category("Custom"), Description("User selected file.")

...

]
public string OpenFileName
{

...

      get {return 

...

_openFileName;}

...


      set {_openFileName= value;}
}

The File Dialog title has been updated in this example to "Select Input File" The resulting file dialog box looks like this: 

Image Added

You can specify these properties in the FileDialogAttribute:

Property

Meaning

Default

FileDialogType

Save or Open

FileDialogType.Save

Filter

Filter string for file extensions

All Files (.)|*.*

Title

Dialog box title

Select propertyname

DefaultExtension

Default file extensions

None

CheckFileExists

True to only allow selecting existing files

False

CheckPathExists

True to only allow using existing paths

False

...

To select a folder name instead of a file name, use the FolderNameEditor class from the .NET Framework instead:

...

 

Code Block
languagecsharp
<%@ Assembly Name="System.Design" %>
\\

<script runat="template">
\\

private string 
\
_outputDirectory = @"c:\temp";
\\

\
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
\\

Category("Custom"), Description("Output directory.")
\
]
\\

public string OutputDirectory
\\

{
    
{\\
  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
 get {return 
\
_outputDirectory;}
\\ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

       set {_outputDirectory= value;}
\\

}
\\

</script>