环境变量是操作系统中存储的动态值,可以被应用程序和系统进程使用。在C#中,我们可以通过多种方式读取、设置和管理环境变量。
C#using System.Collections;
namespace AppEnvironment
{
internal class Program
{
static void Main(string[] args)
{
// 获取特定的系统环境变量
string systemPath = Environment.GetEnvironmentVariable("PATH");
Console.WriteLine("系统PATH:" + systemPath);
// 获取所有环境变量
foreach (DictionaryEntry env in Environment.GetEnvironmentVariables())
{
Console.WriteLine($"{env.Key} = {env.Value}");
}
}
}
}

Windows 通知是与用户进行交互的重要方式。通过 Microsoft.Toolkit.Uwp.Notifications,我们可以轻松实现丰富的通知功能,包括基本通知、图片通知、交互式通知等。
PowerShellInstall-Package Microsoft.Toolkit.Uwp.Notifications
C#using Microsoft.Toolkit.Uwp.Notifications;
using Windows.UI.Notifications;
using Windows.Data.Xml.Dom;
项目中对版本要求,一定是Windows10以上版本。

首先,我们创建一个通知辅助类来封装常用的通知功能:
C#public class NotificationHelper
{
/// <summary>
/// 发送基本通知
/// </summary>
/// <param name="title">通知标题</param>
/// <param name="content">通知内容</param>
public static void SendBasicNotification(string title, string content)
{
var builder = new ToastContentBuilder()
.AddText(title)
.AddText(content);
builder.Show();
}
}
C#public static void SendBasicNotification(string title, string content)
{
var builder = new ToastContentBuilder()
.AddText(title)
.AddText(content);
builder.Show();
}

CustomScrollListView 是一个基于 C# 和 GDI+ 的自定义控件,继承自 Control 类,能够实现具有列头和项目的滚动列表视图。此控件支持多列展示,并可以通过定时器实现自动滚动效果,使得用户可以流畅地查看列表内容。
在 Windows Forms 应用程序开发中,有时我们需要创建更加美观、灵活的界面控件。这篇文章将深入解析一个自定义的 CustomPanel 控件,它不仅继承了标准 Panel 的功能,还增加了 Bootstrap 风格的颜色主题和丰富的定制选项。
CustomPanel.cs。首先,在 CustomPanel.cs 文件中,引用必要的命名空间并创建 CustomPanel 类:
C#using System;
using System.Drawing;
using System.Windows.Forms;
namespace AppControls
{
// 定义 Bootstrap 颜色枚举
public enum BootstrapColors
{
Primary,
Secondary,
Success,
Danger,
Warning,
Info,
Light,
Dark,
White,
Black
}
public class CustomPanel : Panel
{
// 标题属性
public string Title { get; set; } = "默认标题";
// 背景颜色属性(使用颜色枚举)
private BootstrapColors _backgroundColor = BootstrapColors.Light;
public BootstrapColors BackgroundColor
{
get => _backgroundColor;
set
{
_backgroundColor = value;
Invalidate(); // 触发重绘
}
}
// 实际的背景颜色
private Color ActualBackgroundColor => GetBootstrapColor(_backgroundColor);
// 图标颜色属性
public Color IconColor { get; set; } = Color.White;
// 标题字体属性
public Font TitleFont { get; set; } = new Font("Arial", 16, FontStyle.Bold);
// 标题字体大小属性
public float TitleFontSize
{
get => TitleFont.Size;
set => TitleFont = new Font(TitleFont.FontFamily, value, TitleFont.Style);
}
// 新增图标属性
private Image _panelIcon;
public Image PanelIcon
{
get => _panelIcon;
set
{
_panelIcon = value;
Invalidate(); // 触发重绘
}
}
// 图标是否可见
public bool ShowIcon { get; set; } = false;
// 获取对应的 Bootstrap 颜色
private Color GetBootstrapColor(BootstrapColors color)
{
return color switch
{
BootstrapColors.Primary => Color.FromArgb(0, 123, 255),
BootstrapColors.Secondary => Color.FromArgb(108, 117, 125),
BootstrapColors.Success => Color.FromArgb(40, 167, 69),
BootstrapColors.Danger => Color.FromArgb(255, 7, 2),
BootstrapColors.Warning => Color.FromArgb(255, 193, 7),
BootstrapColors.Info => Color.FromArgb(23, 162, 184),
BootstrapColors.Light => Color.FromArgb(248, 249, 250),
BootstrapColors.Dark => Color.FromArgb(52, 58, 64),
BootstrapColors.White => Color.FromArgb(255, 255, 255),
BootstrapColors.Black => Color.FromArgb(0, 0, 0),
_ => Color.LightBlue, // 默认颜色
};
}
// 重写 OnPaint 方法以自定义绘制
protected override void OnPaint(PaintEventArgs e)
{
// 设置高质量绘制
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit;
base.OnPaint(e);
// 绘制背景
using (SolidBrush brush = new SolidBrush(ActualBackgroundColor))
{
e.Graphics.FillRectangle(brush, e.ClipRectangle);
}
// 绘制图标(竖线)
using (Pen pen = new Pen(IconColor, 3)) // 设置颜色和宽度
{
e.Graphics.DrawLine(pen, new Point(5, 5), new Point(5, 25)); // 绘制竖线
}
// 计算标题的高度和居中 Y 坐标
SizeF titleSize = e.Graphics.MeasureString(Title, TitleFont); // 获取标题的尺寸
float titleY = (25 - titleSize.Height) / 2 + 5; // 计算标题的 Y 坐标,使其居中
// 绘制标题
using (SolidBrush textBrush = new SolidBrush(Color.White))
{
e.Graphics.DrawString(Title, TitleFont, textBrush, new PointF(20, titleY)); // 绘制标题
}
// 绘制右侧图标
if (ShowIcon && PanelIcon != null)
{
// 计算图标的位置(右侧居中)
int iconSize = 64;
int iconX = Width - iconSize - 10; // 距离右边缘10像素
int iconY = (Height - iconSize) / 2;
// 绘制图标
e.Graphics.DrawImage(PanelIcon, new Rectangle(iconX, iconY, iconSize, iconSize));
}
}
}
}

在 C# 的多线程编程中,线程可以分为前台线程(Foreground Thread)和后台线程(Background Thread)。这两种线程在创建、使用和资源管理方面有着各自的特点。本文将详细介绍前台线程和后台线程的特点、应用场景以及具体示例,帮助开发者更好地理解这两个概念。
以下是创建前台线程和后台线程的基本用法示例:
C#using System.Threading;
Thread foregroundThread = new Thread(new ThreadStart(SomeMethod)); // 创建前台线程
foregroundThread.Start(); // 启动线程