CodeSmith实用技巧(二):使用FileNameEditor
FileNameEditor类给我们提供了在CodeSmith属性面板中弹出打开或保存文件对话框的方式,在使用时,首先在模版中得添加对程序集CodeSmith.CustomProperties的引用。然后就可以在模版中定义一个属性来使用FileNameEditor:
1
<script runat="template">
2
3
private string _userFileName = @"c:\temp\test.txt";
4
5
6
7
[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
8
9
Category("Custom"), Description("User selected file.")]
10
11
12
13
public string UserFileName
14
15
{
16
17
get {return _userFileName;}
18
19
set {_userFileName= value;}
20
21
}
22
23
</script>
24
25

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

当我们执行该模版时,在属性面板中同样显示为一个按钮:
单击该按钮,弹出一个保存文件的对话框:
我们也可以通过FileDialogAttribute来自定义弹出的对话框,修改模版为:
1
private string _openFileName = @"c:\temp\test.txt";
2
3
4
[Editor(typeof(FileNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
5
6
FileDialogAttribute(FileDialogType.Open, Title="Select Input File"),
7
8
Category("Custom"), Description("User selected file.")]
9
10
11
12
public string OpenFileName
13
14
{
15
16
get {return _openFileName;}
17
18
set {_openFileName= value;}
19
20
}
21
22

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

弹出的对话框如下所示:
当我们想用一个文件夹的名称来代替文件时,可以使用FolderNameEditor类。
1
<%@ Assembly Name="System.Design" %>
2
<script runat="template">
3
private string _outputDirectory = @"c:\temp";
4
[Editor(typeof(System.Windows.Forms.Design.FolderNameEditor), typeof(System.Drawing.Design.UITypeEditor)),
5
Category("Custom"), Description("Output directory.")]
6
public string OutputDirectory
7
{
8
get {return _outputDirectory;}
9
set {_outputDirectory= value;}
10
}
11
</script>
12
13

2

3

4

5

6

7

8

9

10

11

12

13

FileNameEditor重要方法和属性介绍:
公共方法:
名称 |
描述 |
EditValue |
使用由 GetEditStyle 方法提供的编辑器样式编辑指定的对象 |
GetEditStyle |
获取 EditValue 方法所使用的编辑样式 |
支持TerryLee的创业产品Worktile
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com
Worktile,新一代简单好用、体验极致的团队协同、项目管理工具,让你和你的团队随时随地一起工作。完全免费,现在就去了解一下吧。
https://worktile.com
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 写一个简单的SQL生成工具
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)