C#winform程序只允许运行一个实例的几种方法详解

方法一

使用线程互斥变量. 通过定义互斥变量来判断是否已运行实例.
把program.cs文件里的Main()函数改为如下代码:

  1. using System;
  2. using System.Windows.Forms;
  3. using System.Runtime.InteropServices;
  4. namespace QQ消息管理
  5. {
  6. internal static class Program
  7. {
  8. /// <summary>
  9. /// 应用程序的主入口点。
  10. /// </summary>
  11. //[STAThread]
  12. //static void Main()
  13. //{
  14. // Application.EnableVisualStyles();
  15. // Application.SetCompatibleTextRenderingDefault(false);
  16. // Application.Run(new QQ消息管理());
  17. //}
  18. [DllImport("user32.dll")]
  19. private static extern bool FlashWindow(IntPtr hWnd, bool bInvert);
  20. [DllImport("user32.dll")]
  21. private static extern bool FlashWindowEx(int pfwi);
  22. /// <summary>
  23. /// 应用程序的主入口点。
  24. /// </summary>
  25. [STAThread]
  26. private static void Main()
  27. {
  28. bool runone;
  29. System.Threading.Mutex run = new System.Threading.Mutex(true, "single_test", out runone);
  30. if (runone)
  31. {
  32. run.ReleaseMutex();
  33. Application.EnableVisualStyles();
  34. Application.SetCompatibleTextRenderingDefault(false);
  35. QQ消息管理 frame = new QQ消息管理();
  36. int hdc = frame.Handle.ToInt32(); // write to ...
  37. Application.Run(frame);
  38. IntPtr a = new IntPtr(hdc);
  39. }
  40. else
  41. {
  42. MessageBox.Show("该工具正在运行中,请勿重复启动!","提醒:",MessageBoxButtons.OK,MessageBoxIcon.Information);
  43. return;
  44. //IntPtr hdc = new IntPtr(1312810); // read from...
  45. //bool flash = FlashWindow(hdc, true);
  46. }
  47. }
  48. }
  49. }

说明:程序中通过语句 System.Threading.Mutex run = new System.Threading.Mutex(true, “single_test”, out runone);来创建一个互斥体变量run,其中”single_test”为互斥体名,在此方法返回时,如果创建了局部互斥体或指定的命名系统互斥体,则布尔值runone为true;如果指定的命名系统互斥体已存在,则为 false。已命名的互斥体是系统范围的。

方法二

采用判断进程的方式,我们在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同程,如是没有运行该程序,如果有就就不运行.在C#中应用System.Diagnostics名字空间中的Process类来实现,主要代码如下:

  1. 1,在program.cs文件中添加函数如下:
  2. public static System.Diagnostics.Process RunningInstance()
  3. {
  4. System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
  5. System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcesses();
  6. foreach (System.Diagnostics.Process process in processes) //查找相同名称的进程
  7. {
  8. if (process.Id != current.Id) //忽略当前进程
  9. { //确认相同进程的程序运行位置是否一样.
  10. if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", @"/") == current.MainModule.FileName)
  11. { //Return the other process instance.
  12. return process;
  13. }
  14. }
  15. } //No other instance was found, return null.
  16. return null;
  17. }
  18. 2,把Main()函数改为如下代码:
  19. static void Main()
  20. {
  21. if (RunningInstance() == null)
  22. {
  23. Application.EnableVisualStyles();
  24. Application.SetCompatibleTextRenderingDefault(false);
  25. Application.Run(new Form1());
  26. }
  27. else
  28. {
  29. MessageBox.Show("已经运行了一个实例了。");
  30. }
  31. }

方法三

全局原子法,创建程序前,先检查全局原子表中看是否存在特定原子A(创建时添加的),存在时停止创建,说明该程序已运行了一个实例;不存在则运行程序并想全局原子表中添加特定原子A;退出程序时要记得释放特定的原子A哦,不然要到关机才会释放。C#实现如下:

  1. 1.申明WinAPI函数接口
  2. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  3. public static extern UInt32 GlobalAddAtom(String lpString); //添加原子
  4. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  5. public static extern UInt32 GlobalFindAtom(String lpString); //查找原子
  6. [System.Runtime.InteropServices.DllImport("kernel32.dll")]
  7. public static extern UInt32 GlobalDeleteAtom(UInt32 nAtom); //删除原子
  8. 2.修改Main()函数如下:
  9. static void Main()
  10. {
  11. if (GlobalFindAtom("jiaao_test") == 77856768) //没找到原子"jiaao_test"
  12. {
  13. GlobalAddAtom("jiaao_test"); //添加原子"jiaao_test"
  14. Application.EnableVisualStyles();
  15. Application.SetCompatibleTextRenderingDefault(false);
  16. Application.Run(new Form1());
  17. }
  18. else
  19. {
  20. MessageBox.Show("已经运行了一个实例了。");
  21. }
  22. }
  23. 3.FormClosed事件中添加如下代码:
  24. GlobalDeleteAtom(GlobalFindAtom("jiaao_test"));//删除原子"jiaao_test"

方法四

通过进程判断是否启动:

  1. static class Program
  2. {
  3. /// <summary>
  4. /// 应用程序的主入口点。
  5. /// </summary>
  6. [STAThread]
  7. static void Main()
  8. {
  9. //获取当前进程的ID
  10. int pId = Process.GetCurrentProcess().Id;
  11. bool isRun = false;
  12. foreach (Process p in Process.GetProcessesByName("CallMaster"))
  13. {
  14. //取得当前程序的进程,进行比较
  15. if (Common.GetPath().ToLower() == p.MainModule.FileName.ToLower())
  16. {
  17. if (pId != p.Id)
  18. {
  19. isRun = true;
  20. break;
  21. }
  22. }
  23. }
  24. if (isRun==true)
  25. {
  26. Application.Exit();
  27. return;
  28. }
  29. Application.EnableVisualStyles();
  30. Application.SetCompatibleTextRenderingDefault(false);
  31. Application.Run(new frmMain());
  32. }
  33. }
  34. 利用反射获取当前应用程序的全路径:
  35. public static string GetPath()
  36. {
  37. return System.Reflection.Assembly.GetExecutingAssembly().Location;
  38. }

方法五

通过线程互斥判断是否启动

  1. static class Program
  2. {
  3. private static System.Threading.Mutex mutex;
  4. /// <summary>
  5. /// 应用程序的主入口点。
  6. /// </summary>
  7. [STAThread]
  8. static void Main()
  9. {
  10. Application.EnableVisualStyles();
  11. Application.SetCompatibleTextRenderingDefault(false);
  12. mutex = new System.Threading.Mutex(true, "OnlyRun");
  13. if (mutex.WaitOne(0, false))
  14. {
  15. Application.Run(new MainForm());
  16. }
  17. else
  18. {
  19. MessageBox.Show("程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  20. Application.Exit();
  21. }
  22. }
  23. }

另附

c#中怎样判断一个程序是否正在运行?

  1. if (System.Diagnostics.Process.GetProcessesByName("程序进程中的名称").ToList().Count > 0)
  2. {
  3. //存在
  4. }
  5. else
  6. {
  7. //不存在
  8. }


改变DataGridView的行列方格颜色

DataGridView1.Rows[k].DefaultCellStyle.BackColor = Color.Yellow
DataGridView1.Rows[k].DefaultCellStyle.ForeColor = Color.Yellow

无法编辑

是因为把用户操作的几个选项关掉了,打开删除就可以修改了

字典排序

  1. Dictionary<int, string> dic1Asc = test.OrderBy(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  2. Console.WriteLine("小到大排序");
  3. foreach(KeyValuePair<int,string> k in dic1Asc){
  4. Console.WriteLine("key:" +k.Key +" value:" + k.Value);
  5. }
  6. Console.WriteLine("大到小排序");
  7. Dictionary<int, string> dic1desc = test.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
  8. foreach (KeyValuePair<int, string> k in dic1desc)
  9. {
  10. Console.WriteLine("key:" + k.Key + " value:" + k.Value);
  11. }

在datagridview中添加button按钮

image.png

  1. Button btn = new Button();
  2. dataGridView1.Rows[dataIndex].Cells[3].Value = btn;
  3. //------------------------------------------------------------------------------------------------
  4. /// <summary>
  5. /// 鼠标移进格子的时候
  6. /// </summary>
  7. /// <param name="sender"></param>
  8. /// <param name="e"></param>
  9. private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
  10. {
  11. DataGridView dgv = (DataGridView)sender; //如果是"Link"列,被點選
  12. if (dgv.Columns[e.ColumnIndex].Name == "Column7" && e.RowIndex < dgv.Rows.Count && e.RowIndex > -1)
  13. {
  14. string titlePath = dataGridView1.Rows[e.RowIndex].Cells[3].Tag.ToString();
  15. ImageForm1 imgForm = ImageForm1.GetSingleton(titlePath); // 无参数
  16. imgForm.Show();
  17. imgForm.Activate();
  18. var formX = this.Location.X + this.Width * 3 / 5;
  19. var formY = this.Location.Y + this.Height / 3;
  20. imgForm.Location = new Point(formX, formY);
  21. dataGridView1.Rows[e.RowIndex].Cells[2].Tag = imgForm;
  22. }
  23. }
  24. /// <summary>
  25. /// 鼠标移出格子的时候
  26. /// </summary>
  27. /// <param name="sender"></param>
  28. /// <param name="e"></param>
  29. private void dataGridView1_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
  30. {
  31. DataGridView dgv = (DataGridView)sender; //如果是"Link"列,被點選
  32. if (dgv.Columns[e.ColumnIndex].Name == "Column7" && e.RowIndex < dgv.Rows.Count && e.RowIndex > -1)
  33. {
  34. ImageForm1 imgForm = dataGridView1.Rows[e.RowIndex].Cells[2].Tag as ImageForm1;
  35. imgForm.Hide();
  36. }
  37. }


RichTextBox

滚动光爆到最后一行
richTextBox.ScrollToCaret();

隐藏任务栏图标

this.ShowInTaskbar = false;

Winform窗口起始位置

  1. int x = (System.Windows.Forms.SystemInformation.WorkingArea.Width - this.Size.Width) / 2;
  2. int y = (System.Windows.Forms.SystemInformation.WorkingArea.Height - this.Size.Height) / 2;
  3. this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
  4. this.Location = (Point)new Size(x, y); //窗体的起始位置为(x,y)
  5. System.Windows.Forms.SystemInformation.WorkingArea.Width //屏幕宽度
  6. System.Windows.Forms.SystemInformation.WorkingArea.Height //屏幕高度(去系统任务栏,当显示有任务栏的时候)
  7. this.Size.Width //自己窗体的宽度,
  8. this.Size.Width //自己窗体的高度
  9. this.ClientRectangle.Width //工作区域宽度
  10. this.ClientRectangle.Height //工作区域高度设置窗口初始位置
  11. this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
  12. this.StartPosition = FormStartPosition.CenterParent; //窗体在其父窗体中居中
  13. this.StartPosition = FormStartPosition.CenterScreen; //窗体在当前显示窗口中居中,尺寸在窗体大小中指定
  14. this.StartPosition = FormStartPosition.WindowsDefaultBounds; //窗体定位在windows默认位置,边界也由windows默认决定
  15. this.StartPosition = FormStartPosition.WindowsDefaultLocation; //窗体定位在windows默认位置,尺寸在窗体大小中指定
  16. 通过指定窗体Locaiton来,设定窗体位置
  17. this.StartPosition = FormStartPosition.Manual; //窗体的位置由Location属性决定
  18. this.Location = (Point)new Size(0, 0); //窗体的起始位置为0,0
  19. 创建窗体时, 设置宽度和高度
  20. this.ClientSize = new System.Drawing.Size(x1,y1); //X1 为宽度,Y1为高度
  21. 获取屏幕大小(using System.Drawing)
  22. Rectangle rect = Screen.GetWorkingArea(this);
  23. Point p = new Point(rect.Width,rect.Height);
  24. this.Location = p;

关闭所有子窗口

  1. //关闭所有子窗口,可以执行子窗口的closing方法
  2. FormCollection childCollection = Application.OpenForms;
  3. for (int i = childCollection.Count; i-- > 0;)
  4. {
  5. if (childCollection[i].Name != this.Name) childCollection[i].Close();
  6. }


mysql相关包

dapper 2.0.30
mysql 8.0.20.0

追加文字到txt并换行

  1. if (richTextBox1.Text.Length == 0)
  2. {
  3. return;
  4. }
  5. string logPath = 原卷答案对应核查.日志缓存 + "原卷加编上传日志/";
  6. if (!Directory.Exists(logPath))
  7. {
  8. Directory.CreateDirectory(logPath);
  9. }
  10. string fileName = logPath + DateTime.Now.Year + "年-" + DateTime.Now.Month + "月-" + DateTime.Now.Day + "日" + "-原卷加编上传日志" + ".txt";
  11. if (!File.Exists(fileName))
  12. {
  13. FileInfo logFile = new FileInfo(fileName);
  14. FileStream fs = logFile.Create();
  15. fs.Close();
  16. }
  17. StreamWriter sw = File.AppendText(fileName);
  18. sw.WriteLine(richTextBox1.Text);
  19. sw.Flush();
  20. sw.Close();

删除文件夹下的所有文件

  1. DirectoryInfo dir = new DirectoryInfo(open.SelectedPath);
  2. dir.Delete(true);

递归遍历所有文件

  1. /// <summary>
  2. /// 递归寻找文件
  3. /// </summary>
  4. /// <param name="dir"></param>
  5. /// <param name="list"></param>
  6. private void Director(string dir, List<string> list)
  7. {
  8. DirectoryInfo d = new DirectoryInfo(dir);
  9. FileInfo[] files = d.GetFiles();//文件
  10. DirectoryInfo[] directs = d.GetDirectories();//文件夹
  11. foreach (FileInfo f in files)
  12. {
  13. list.Add(f.FullName);//添加文件路径到列表中
  14. }
  15. //获取子文件夹内的文件列表,递归遍历
  16. foreach (DirectoryInfo dd in directs)
  17. {
  18. Director(dd.FullName, list);
  19. }
  20. }

Bitmap转Bytes数组

  1. //首先是Bitmap 转 MemoryStream
  2. MemoryStream ms = new MemoryStream();
  3. bitmap.save(ms, ImageFormat.Jpeg);
  4. ms.Seek(0, SeekOrigin.Begin); //一定不要忘记将流的初始位置重置
  5. //然后是MemoryStream 转 Byte数组
  6. byte bytes = new byte[ms.Length];
  7. ms.Read(bytes, 0, bytes.Length); //如果上面流没有seek 则这里读取的数据全会为0
  8. ms.Dispose();

转换小数计算

float f = (int)i 1.0000f / (int)j 1.0000f;

判断图片是否为全白或者部分白色

  1. //将PDF页转换成bitmap图形
  2. Image img = doc.SaveAsImage(i);
  3. Bitmap bitmap = new Bitmap(img);
  4. //图片总像素
  5. int intCount = 0;
  6. //按像素遍历
  7. for (int intY = 0; intY < img.Height; intY++)
  8. {
  9. for (int intX = 0; intX < img.Width; intX++)
  10. {
  11. if (bitmap.GetPixel(intX, intY).GetBrightness().Equals(1))
  12. {
  13. intCount += 1;
  14. }
  15. }
  16. }
  17. bitmap.Dispose();
  18. if (intCount < 4000)
  19. {
  20. img.Dispose();
  21. continue;
  22. }

C#获取当前程序运行路径的方法集合

//获取当前进程的完整路径,包含文件名(进程名)。
string str = this.GetType().Assembly.Location;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取新的 Process 组件并将其与当前活动的进程关联的主模块的完整路径,包含文件名(进程名)。
string str = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。
string str = System.Environment.CurrentDirectory;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取当前 Thread 的当前应用程序域的基目录,它由程序集冲突解决程序用来探测程序集。
string str = System.AppDomain.CurrentDomain.BaseDirectory;
result: X:\xxx\xxx\ (.exe文件所在的目录+”\”)
//获取和设置包含该应用程序的目录的名称。(推荐)
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
result: X:\xxx\xxx\ (.exe文件所在的目录+”\”)
//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str = System.Windows.Forms.Application.StartupPath;
result: X:\xxx\xxx (.exe文件所在的目录)
//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str = System.Windows.Forms.Application.ExecutablePath;
result: X:\xxx\xxx\xxx.exe (.exe文件所在的目录+.exe文件名)
//获取应用程序的当前工作目录(不可靠)。
string str = System.IO.Directory.GetCurrentDirectory();
result: X:\xxx\xxx (.exe文件所在的目录)

DateTime用法

C#里内置的DateTime基本上都可以实现这些功能,巧用DateTime会使你处理这些事来变轻松多了
今天
DateTime.Now.Date.ToShortDateString();
昨天,就是今天的日期减一
DateTime.Now.AddDays(-1).ToShortDateString();
明天,同理,加一
DateTime.Now.AddDays(1).ToShortDateString();
本周(要知道本周的第一天就得先知道今天是星期几,从而得知本周的第一天就是几天前的那一天,要注意的是这里的每一周是从周日始至周六止
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek)))).ToShortDateString();
如果你还不明白,再看一下中文显示星期几的方法就应该懂了
由于DayOfWeek返回的是数字的星期几,我们要把它转换成汉字方便我们阅读,有些人可能会用switch来一个一个地对照,其实不用那么麻烦的
string[] Day = new string[] { “星期日”, “星期一”, “星期二”, “星期三”, “星期四”, “星期五”, “星期六” };
Day[Convert.ToInt16(DateTime.Now.DayOfWeek)];
上周,同理,一个周是7天,上周就是本周再减去7天,下周也是一样
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) - 7).ToShortDateString();
下周
DateTime.Now.AddDays(Convert.ToDouble((0 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
DateTime.Now.AddDays(Convert.ToDouble((6 - Convert.ToInt16(DateTime.Now.DayOfWeek))) + 7).ToShortDateString();
本月,很多人都会说本月的第一天嘛肯定是1号,最后一天就是下个月一号再减一天。当然这是对的
一般的写法
DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + “1”; //第一天
DateTime.Parse(DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + “1”).AddMonths(1).AddDays(-1).ToShortDateString();//最后一天
巧用C#里ToString的字符格式化更简便
DateTime.Now.ToString(“yyyy-MM-01”);
DateTime.Parse(DateTime.Now.ToString(“yyyy-MM-01”)).AddMonths(1).AddDays(-1).ToShortDateString();
上个月,减去一个月份
DateTime.Parse(DateTime.Now.ToString(“yyyy-MM-01”)).AddMonths(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
下个月,加去一个月份
DateTime.Parse(DateTime.Now.ToString(“yyyy-MM-01”)).AddMonths(1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-MM-01”)).AddMonths(2).AddDays(-1).ToShortDateString();
7天后
DateTime.Now.Date.ToShortDateString();
DateTime.Now.AddDays(7).ToShortDateString();
7天前
DateTime.Now.AddDays(-7).ToShortDateString();
DateTime.Now.Date.ToShortDateString();
本年度,用ToString的字符格式化我们也很容易地算出本年度的第一天和最后一天
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(1).AddDays(-1).ToShortDateString();
上年度,不用再解释了吧
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(-1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddDays(-1).ToShortDateString();
下年度
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(1).ToShortDateString();
DateTime.Parse(DateTime.Now.ToString(“yyyy-01-01”)).AddYears(2).AddDays(-1).ToShortDateString();
本季度,很多人都会觉得这里难点,需要写个长长的过程来判断。其实不用的,我们都知道一年四个季度,一个季度三个月
首先我们先把日期推到本季度第一个月,然后这个月的第一天就是本季度的第一天了
DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”);
同理,本季度的最后一天就是下季度的第一天减一
DateTime.Parse(DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
下季度,相信你们都知道了。。。。收工
DateTime.Now.AddMonths(3 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”);
DateTime.Parse(DateTime.Now.AddMonths(6 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
上季度
DateTime.Now.AddMonths(-3 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”);
DateTime.Parse(DateTime.Now.AddMonths(0 - ((DateTime.Now.Month - 1) % 3)).ToString(“yyyy-MM-01”)).AddDays(-1).ToShortDateString();
======简单补充======
c#中如何获取时间!
1、DateTime 数字型
System.DateTime currentTime=new System.DateTime();
1.1 取当前年月日时分秒
currentTime=System.DateTime.Now;
1.2 取当前年
int 年=currentTime.Year;
1.3 取当前月
int 月=currentTime.Month;
1.4 取当前日
int 日=currentTime.Day;
1.5 取当前时
int 时=currentTime.Hour;
1.6 取当前分
int 分=currentTime.Minute;
1.7 取当前秒
int 秒=currentTime.Second;
1.8 取当前毫秒
int 毫秒=currentTime.Millisecond;
(变量可用中文)
1.9 取中文日期显示——年月日时分
string strY=currentTime.ToString(“f”); //不显示秒
1.10 取中文日期显示年月
string strYM=currentTime.ToString(“y”);
1.11 取中文日期显示
月日
string strMD=currentTime.ToString(“m”);
1.12 取中文年月日
string strYMD=currentTime.ToString(“D”);
1.13 取当前时分,格式为:14:24
string strT=currentTime.ToString(“t”);
1.14 取当前时间,格式为:2003-09-23T14:46:48
string strT=currentTime.ToString(“s”);
1.15 取当前时间,格式为:2003-09-23 14:48:30Z
string strT=currentTime.ToString(“u”);
1.16 取当前时间,格式为:2003-09-23 14:48
string strT=currentTime.ToString(“g”);
1.17 取当前时间,格式为:Tue, 23 Sep 2003 14:52:40 GMT
string strT=currentTime.ToString(“r”);
1.18获得当前时间 n 天后的日期时间
DateTime newDay = DateTime.Now.AddDays(100);
string strT = DateTime.Now.ToString(“yyyy-MM-dd HH:mm:ss”);
System.DateTime currentTime=new System.DateTime();
currentTime=System.DateTime.Now; //取当前年月日时分秒
string Y=currentTime.Year.ToString(); //取当前年
string M=currentTime.Month.ToString(); //取当前月
string D=currentTime.Day.ToString(); //取当前日
string T=currentTime.Hour.ToString(); //取当前时
string MM=currentTime.Minute.ToString(); //取当前分
string S=currentTime.Second.ToString(); //取当前秒
string SS=currentTime.Millisecond.ToString(); //取当前毫秒
string FileName=Y+M+D+T+MM+S+SS+”.Html”; //联接后,得到长文件名