### 一些很酷的 .Net 使用技巧 在 .Net 框架的开发过程中,掌握一些实用且酷炫的小技巧可以极大地提升开发效率,并让代码更加优雅。以下是一些值得学习和使用的技巧: #### 1\. 获取系统文件夹路径 在 .Net 开发中,经常需要访问特定的系统文件夹,例如用户的文档文件夹。使用 `System.Environment` 类的 `GetFolderPath` 方法可以轻松实现这一点。例如,获取个人文件夹(通常为“我的文档”)的路径: ```csharp string personalFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); ``` `SpecialFolder` 枚举定义了许多系统特殊文件夹,如桌面、应用程序数据文件夹等。 #### 2\. 获取正在执行的 exe 文件的路径 在 .Net 应用程序中,有时需要知道当前正在运行的应用程序的位置。有两种方式可以做到这一点: 1. **使用 `Application` 类**: ```csharp string exePath = Application.ExecutablePath; ``` 2. **使用反射**: ```csharp string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location; ``` 两种方法各有优势:`Application.ExecutablePath` 更加简洁明了,而反射方法可以在非 Windows 平台上使用。 #### 3\. 检测操作系统的版本 为了确保应用程序兼容不同的操作系统版本,开发者可能需要知道当前运行的操作系统版本。这可以通过 `Environment` 类的 `OSVersion` 属性实现: ```csharp OperatingSystem os = Environment.OSVersion; MessageBox.Show(os.Version.ToString()); MessageBox.Show(os.Platform.ToString()); ``` 其中 `Version` 属性返回操作系统的主要版本号和次要版本号,而 `Platform` 返回平台类型。 #### 4\. 从完整文件名中提取文件名或扩展名 在处理文件路径时,可能需要单独获取文件名或扩展名。这可以通过 `System.IO.Path` 类的方法来完成: - 获取文件名:`System.IO.Path.GetFileName(path)` - 获取不带扩展名的文件名:`System.IO.Path.GetFileNameWithoutExtension(path)` - 获取文件扩展名:`System.IO.Path.GetExtension(path)` 例如,如果有一个文件名为 `example.txt` 的路径,可以这样使用: ```csharp string path = "C:\\path\\to\\file\\example.txt"; string fileName = Path.GetFileName(path); // "example.txt" string fileNameWithoutExt = Path.GetFileNameWithoutExtension(path); // "example" string extension = Path.GetExtension(path); // ".txt" ``` #### 5\. Vb 和 C# 的语法差异 虽然两者都是 .Net 支持的语言,但它们之间存在显著的语法差异。例如,在变量声明方面,Vb.NET 使用 `Dim` 关键字,而 C# 使用 `var` 或具体的数据类型。此外,Vb.NET 支持隐式类型的变量声明,而 C# 则有更严格的类型检查。 #### 6\. 获取系统信息 开发者可以通过 `System.Windows.Forms.SystemInformation` 类的静态属性来获取当前计算机的一些基本信息,例如用户名、是否联网、显示器数量等: ```csharp string userName = SystemInformation.UserName; bool isNetworkAvailable = SystemInformation.IsNetworkAvailable; int monitorCount = SystemInformation.MonitorCount; ``` #### 7\. Main 方法的 `[STAThread]` 特性 在 WinForms 应用程序中,`Main` 方法通常需要标记为 `[STAThread]`。这一特性指示当前应用程序将使用单线程模型来处理 UI 操作。这对于确保 GUI 元素正确更新非常重要。 #### 8\. 读取 CSV 文件 对于简单的文本数据读写,.Net 提供了多种方法。一种常见的方式是通过 ODBC 连接来读取 CSV 文件。示例连接字符串如下: ```csharp string connectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};Dbq=" + csvFilePath + ";Extensions=asc,csv,tab,txt;PersistSecurityInfo=False"; using (OdbcConnection connection = new OdbcConnection(connectionString)) { // 连接并读取 CSV 文件 } ``` #### 9\. 获取磁盘开销信息 获取磁盘空间信息对于监控系统性能非常重要。.Net 提供了一种方法来调用底层的 `kernel32.dll` 函数 `GetDiskFreeSpaceEx` 来实现这一点: ```csharp public sealed class DriveInfo { [DllImport("kernel32.dll", EntryPoint = "GetDiskFreeSpaceExA")] private static extern long GetDiskFreeSpaceEx(string lpDirectoryName, out long lpFreeBytesAvailableToCaller, out long lpTotalNumberOfBytes, out long lpTotalNumberOfFreeBytes); public static long GetInfo(string drive, out long available, out long total, out long free) { return GetDiskFreeSpaceEx(drive, out available, out total, out free); } public static DriveInfoSystem GetInfo(string drive) { long result, available, total, free; result = GetDiskFreeSpaceEx(drive, out available, out total, out free); return new DriveInfoSystem(drive, result, available, total, free); } } public struct DriveInfoSystem { public readonly string Drive; public readonly long Result; public readonly long Available; public readonly long Total; public readonly long Free; public DriveInfoSystem(string drive, long result, long available, long total, long free) { this.Drive = drive; this.Result = result; this.Available = available; this.Total = total; this.Free = free; } } ``` 以上技巧覆盖了 .Net 开发中的一些基本但非常实用的功能,从简单的文件操作到复杂的系统信息检索,这些技巧都能帮助开发者编写出高效、可靠的代码。
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![crx](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/octet-stream](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![avatar-default](https://csdnimg.cn/release/downloadcmsfe/public/img/lazyLogo2.1882d7f4.png)
![avatar](https://profile-avatar.csdnimg.cn/default.jpg!1)
- 粉丝: 0
- 资源: 14
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助
![voice](https://csdnimg.cn/release/downloadcmsfe/public/img/voice.245cc511.png)
![center-task](https://csdnimg.cn/release/downloadcmsfe/public/img/center-task.c2eda91a.png)
最新资源
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)
![dialog-icon](https://csdnimg.cn/release/downloadcmsfe/public/img/green-success.6a4acb44.png)