.NET 6.0 WPF 项目代码目录结构

更新日期: 2022-10-17 阅读次数: 2970 字数: 924 分类: Windows

使用 Visual Studio 2022 社区版创建,WPF 项目。注意选择的是 .NET 6.0,而不是 .NET Framework。

了解目录结构是为了方便在终端里开发,摆脱 VS。

例如,我新建了一个 I18nTool 的项目:

未编译前目录结构

> tree
.
├── I18nTool
│   ├── App.xaml
│   ├── App.xaml.cs
│   ├── AssemblyInfo.cs
│   ├── I18nTool.csproj
│   ├── I18nTool.csproj.user
│   ├── MainWindow.xaml
│   ├── MainWindow.xaml.cs
│   ├── bin
│   │   └── Debug
│   │       └── net6.0-windows
│   └── obj
│       ├── Debug
│       │   └── net6.0-windows
│       │       ├── App.g.i.cs
│       │       ├── I18nTool.AssemblyInfo.cs
│       │       ├── I18nTool.AssemblyInfoInputs.cache
│       │       ├── I18nTool.GeneratedMSBuildEditorConfig.editorconfig
│       │       ├── I18nTool.assets.cache
│       │       ├── I18nTool.csproj.AssemblyReference.cache
│       │       ├── I18nTool.designer.deps.json
│       │       ├── I18nTool.designer.runtimeconfig.json
│       │       ├── I18nTool_MarkupCompile.i.cache
│       │       ├── MainWindow.g.i.cs
│       │       ├── ref
│       │       └── refint
│       ├── I18nTool.csproj.nuget.dgspec.json
│       ├── I18nTool.csproj.nuget.g.props
│       ├── I18nTool.csproj.nuget.g.targets
│       ├── project.assets.json
│       └── project.nuget.cache
└── I18nTool.sln

9 directories, 23 files

首次编译后的目录结构

.
├── I18nTool
│   ├── App.xaml
│   ├── App.xaml.cs
│   ├── AssemblyInfo.cs
│   ├── I18nTool.csproj
│   ├── I18nTool.csproj.user
│   ├── MainWindow.xaml
│   ├── MainWindow.xaml.cs
│   ├── bin
│   │   └── Debug
│   │       └── net6.0-windows
│   │           ├── I18nTool.deps.json
│   │           ├── I18nTool.dll
│   │           ├── I18nTool.exe
│   │           ├── I18nTool.pdb
│   │           └── I18nTool.runtimeconfig.json
│   └── obj
│       ├── Debug
│       │   └── net6.0-windows
│       │       ├── App.g.cs
│       │       ├── App.g.i.cs
│       │       ├── I18nTool.AssemblyInfo.cs
│       │       ├── I18nTool.AssemblyInfoInputs.cache
│       │       ├── I18nTool.GeneratedMSBuildEditorConfig.editorconfig
│       │       ├── I18nTool.assets.cache
│       │       ├── I18nTool.csproj.AssemblyReference.cache
│       │       ├── I18nTool.csproj.BuildWithSkipAnalyzers
│       │       ├── I18nTool.csproj.CoreCompileInputs.cache
│       │       ├── I18nTool.csproj.FileListAbsolute.txt
│       │       ├── I18nTool.designer.deps.json
│       │       ├── I18nTool.designer.runtimeconfig.json
│       │       ├── I18nTool.dll
│       │       ├── I18nTool.g.resources
│       │       ├── I18nTool.genruntimeconfig.cache
│       │       ├── I18nTool.pdb
│       │       ├── I18nTool_MarkupCompile.cache
│       │       ├── I18nTool_MarkupCompile.i.cache
│       │       ├── I18nTool_MarkupCompile.lref
│       │       ├── I18nTool_ifsy5zia_wpftmp.AssemblyInfo.cs
│       │       ├── I18nTool_ifsy5zia_wpftmp.AssemblyInfoInputs.cache
│       │       ├── I18nTool_ifsy5zia_wpftmp.GeneratedMSBuildEditorConfig.editorconfig
│       │       ├── I18nTool_ifsy5zia_wpftmp.assets.cache
│       │       ├── I18nTool_ifsy5zia_wpftmp.csproj.BuildWithSkipAnalyzers
│       │       ├── MainWindow.baml
│       │       ├── MainWindow.g.cs
│       │       ├── MainWindow.g.i.cs
│       │       ├── apphost.exe
│       │       ├── ref
│       │       │   └── I18nTool.dll
│       │       └── refint
│       │           └── I18nTool.dll
│       ├── I18nTool.csproj.nuget.dgspec.json
│       ├── I18nTool.csproj.nuget.g.props
│       ├── I18nTool.csproj.nuget.g.targets
│       ├── project.assets.json
│       └── project.nuget.cache
└── I18nTool.sln

9 directories, 48 files

编译之后增加了 25 个文件。

核心文件

  • 布局文件:MainWindow.xaml
  • 逻辑文件:MainWindow.xaml.cs

bin 与 obj 目录的区别

参考:

https://stackoverflow.com/questions/5308491/what-are-the-obj-and-bin-folders-created-by-visual-studio-used-for

  • obj 目录:用于存放编译过程中产生的中间文件,例如,每个源码文件都会生成一个 object 文件,但是这些中间文件还没有链接到一起。
  • bin 目录:最终生成的可执行文件或库文件,即,链接后的最终文件。

类似的地方,obj 和 bin 目录都会包含 Debug 目录和 Release 目录,以对于不同的编译选项。

git 管理

推荐使用 VS 里面新建 git 仓库时自带的 gitignore 文件。

其自动屏蔽了 obj 与 bin 目录,实际提交时只有寥寥几个源码/配置文件,还算简洁。

终端编译执行

终端里用 dotnet.exe 命令编译执行,需要进入到与 bin obj 平级的目录下,即,忽略 sln 文件目录。

执行运行

dotnet.exe run 

即可。其会自动生成 obj,bin 目录。

如果在 sln 平级目录执行,会报错:

> dotnet.exe run
找不到要运行的项目。请确保 D:\work\I18nTool 中存在项目,或使用 --project 传递项目路径。

tags: wpf

关于作者 🌱

我是来自山东烟台的一名开发者,有敢兴趣的话题,或者软件开发需求,欢迎加微信 zhongwei 聊聊, 查看更多联系方式