1.将打开的某个form激活
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == typeof(YourDialogClass)&&form.Text == "TheTitle")
{
form.Activate();
return;
}
}
2.将float数据类型,整数部分至少保留4位,小数部分至少保留8位,不足位用零补全
float c = 3.1415f;
string s = c.ToString("0000.00000000");
3.如何禁止调整窗口的大小
FormBorderStyle 设为FixedSingle 即可。
4.处理事件
Application.DoEvents();
5.DebugOutputException
public static void DebugOutputException(Exception ex)
{
#if X_DEBUG
Debug.WriteLine(ex.ToString() + " " + ex.StackTrace.ToString());
#endif
}
6.判断目录是否存在
Directory.Exists(string path);
7.创建目录
Directory.CreateDirectory(string path);
8.#region
#region 使您可以在使用 Visual Studio 代码编辑器的大纲显示功能时指定可展开或折叠的代码块。 在较长的代码文件中,能够折叠或隐藏一个或多个区域会十分便利,这样,您可将精力集中于当前处理的文件部分。 下面的示例演示如何定义区域:
#region MyClass definition public class MyClass { static void Main() { } } #endregion
9.将f保留digits位小数, 靠近原点
/// <summary>
/// 将f保留digits位小数, 靠近原点
/// </summary>
public double CutOffValue(double f, int digits)
{
double roundDown = 0.0;
//截断保留小数位1位
{
double n = Math.Pow(10, digits);
double valuTemp = Math.Abs(f) * n;
double floorValue = Math.Floor(valuTemp);
roundDown = floorValue / n;
}
if (f < 0)
{
roundDown = -roundDown;
}
return roundDown;
}
10.在指定矩形区域居中绘制文本
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center;
strFormat.LineAlignment = StringAlignment.Center;
g.DrawString(message, font, new SolidBrush(Color.FromArgb(128, Color.Black)), new Rectangle(x, y, width, height), strFormat);
11.获取程序集版本
string assemblyVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
12.获取文件版本
using System.Diagnostics;
FileVersionInfo myFileVersion = FileVersionInfo.GetVersionInfo (System.Windows.Forms.Application.ExecutablePath);
string fileVersion = myFileVersion.FileVersion;
13.无符号整数, 十六进制形式显示, 并且指定位数
byte a = 1234;
a.ToString("X6");
string.Format("{0:X8}", a);
X十六进制字符大写, x十六进制字符小写14.C# 获得当前 进程 或 线程 的ID
如何获得当前进程的Id用:
Process[] processes = Process.GetProcesses();
foreach(Process process in processes)
{
if(process.ProcessName == "进程名"
{
MessageBox.Show(process.Id);
}
}
或
Process processes =Process.GetCurrentProcess
processes.id获得当前进程的ID
--------------------------------------------------------------------------------------
如何获得当前线程的ID用:
Thread.CurrentThread.ManagedThreadId.ToString() (推荐)
或
AppDomain.GetCurrentThreadId().ToString()