C# 获取程序编译时间并设置为程序名称

将程序的编译时间加到标题栏

效果如下:

在这里插入图片描述

代码

this.Text = "车轮滚滚向西行" + string.Format(" 【{0:r}】", System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location) ); 

知识点

设置程序标题

this.Text = ...;

获取生成时间

System.IO.File.GetLastWriteTime(this.GetType().Assembly.Location)

时间格式转换

string.Format(" 【{0:r}】", ...);

更多格式请参考:

//C#  datetime 格式化
DateTime dt = DateTime.Now;
Label1.Text = dt.ToString();//2005-11-5 13:21:25
Label2.Text = dt.ToFileTime().ToString();//127756416859912816
Label3.Text = dt.ToFileTimeUtc().ToString();//127756704859912816
Label4.Text = dt.ToLocalTime().ToString();//2005-11-5 21:21:25
Label5.Text = dt.ToLongDateString().ToString();//2005年11月5日
Label6.Text = dt.ToLongTimeString().ToString();//13:21:25
Label7.Text = dt.ToOADate().ToString();//38661.5565508218
Label8.Text = dt.ToShortDateString().ToString();//2005-11-5
Label9.Text = dt.ToShortTimeString().ToString();//13:21
Label10.Text = dt.ToUniversalTime().ToString();//2005-11-5 5:21:25
//2005-11-5 13:30:28.4412864
Label1.Text = dt.Year.ToString();//2005
Label2.Text = dt.Date.ToString();//2005-11-5 0:00:00
Label3.Text = dt.DayOfWeek.ToString();//Saturday
Label4.Text = dt.DayOfYear.ToString();//309
Label5.Text = dt.Hour.ToString();//13
Label6.Text = dt.Millisecond.ToString();//441
Label7.Text = dt.Minute.ToString();//30
Label8.Text = dt.Month.ToString();//11
Label9.Text = dt.Second.ToString();//28
Label10.Text = dt.Ticks.ToString();//632667942284412864
Label11.Text = dt.TimeOfDay.ToString();//13:30:28.4412864
Label1.Text = dt.ToString();//2005-11-5 13:47:04
Label2.Text = dt.AddYears(1).ToString();//2006-11-5 13:47:04
Label3.Text = dt.AddDays(1.1).ToString();//2005-11-6 16:11:04
Label4.Text = dt.AddHours(1.1).ToString();//2005-11-5 14:53:04
Label5.Text = dt.AddMilliseconds(1.1).ToString();//2005-11-5 13:47:04
Label6.Text = dt.AddMonths(1).ToString();//2005-12-5 13:47:04
Label7.Text = dt.AddSeconds(1.1).ToString();//2005-11-5 13:47:05
Label8.Text = dt.AddMinutes(1.1).ToString();//2005-11-5 13:48:10
Label9.Text = dt.AddTicks(1000).ToString();//2005-11-5 13:47:04
Label10.Text = dt.CompareTo(dt).ToString();//0
//Label11.Text = dt.Add(?).ToString();//问号为一个时间段
Label1.Text = dt.Equals("2005-11-6 16:11:04").ToString();//False
Label2.Text = dt.Equals(dt).ToString();//True
Label3.Text = dt.GetHashCode().ToString();//1474088234
Label4.Text = dt.GetType().ToString();//System.DateTime
Label5.Text = dt.GetTypeCode().ToString();//DateTime

Label1.Text = dt.GetDateTimeFormats('s')[0].ToString();//2005-11-05T14:06:25
Label2.Text = dt.GetDateTimeFormats('t')[0].ToString();//14:06
Label3.Text = dt.GetDateTimeFormats('y')[0].ToString();//2005年11月
Label4.Text = dt.GetDateTimeFormats('D')[0].ToString();//2005年11月5日
Label5.Text = dt.GetDateTimeFormats('D')[1].ToString();//2005 11 05
Label6.Text = dt.GetDateTimeFormats('D')[2].ToString();//星期六 2005 11 05
Label7.Text = dt.GetDateTimeFormats('D')[3].ToString();//星期六 2005年11月5日
Label8.Text = dt.GetDateTimeFormats('M')[0].ToString();//11月5日
Label9.Text = dt.GetDateTimeFormats('f')[0].ToString();//2005年11月5日 14:06
Label10.Text = dt.GetDateTimeFormats('g')[0].ToString();//2005-11-5 14:06
Label11.Text = dt.GetDateTimeFormats('r')[0].ToString();//Sat, 05 Nov 2005 14:06:25 GMT

Label1.Text =? string.Format("{0:d}",dt);//2005-11-5
Label2.Text =? string.Format("{0:D}",dt);//2005年11月5日
Label3.Text =? string.Format("{0:f}",dt);//2005年11月5日 14:23
Label4.Text =? string.Format("{0:F}",dt);//2005年11月5日 14:23:23
Label5.Text =? string.Format("{0:g}",dt);//2005-11-5 14:23
Label6.Text =? string.Format("{0:G}",dt);//2005-11-5 14:23:23
Label7.Text =? string.Format("{0:M}",dt);//11月5日
Label8.Text =? string.Format("{0:R}",dt);//Sat, 05 Nov 2005 14:23:23 GMT
Label9.Text =? string.Format("{0:s}",dt);//2005-11-05T14:23:23
Label10.Text = string.Format("{0:t}",dt);//14:23
Label11.Text = string.Format("{0:T}",dt);//14:23:23
Label12.Text = string.Format("{0:u}",dt);//2005-11-05 14:23:23Z
Label13.Text = string.Format("{0:U}",dt);//2005年11月5日 6:23:23
Label14.Text = string.Format("{0:Y}",dt);//2005年11月
Label15.Text = string.Format("{0}",dt);//2005-11-5 14:23:23?
Label16.Text = string.Format("{0:yyyyMMddHHmmssffff}",dt); //yyyymm等可以设置,比如Label16.Text = string.Format("{0:yyyyMMdd}",dt);
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要将WinForm程序设置为默认打开指定类型的文件,您需要在注册表中进行相应的设置,具体步骤如下: 1. 打开您的WinForm项目,添加一个新的Windows Form,例如“FileViewerForm”。 2. 在“FileViewerForm”上添加一个“Label”控件,用来显示文件名。 3. 在“FileViewerForm”的代码文件中,添加以下代码: ```csharp using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormDefaultApp { public partial class FileViewerForm : Form { public FileViewerForm(string filePath) { InitializeComponent(); label1.Text = filePath; } } } ``` 在这个例子中,我们使用“Label”控件来显示文件名,并在构造函数中将文件名作为参数进行传递。 4. 在“FileViewerForm”的设计视图中,右键单击表单并选择“属性”。找到“窗体”属性中的“文本”属性,并将其设置为“文件查看器”。 5. 添加一个新的类,例如“Program.cs”,并在其中添加以下代码: ```csharp using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace WinFormDefaultApp { static class Program { [STAThread] static void Main(string[] args) { if (args.Length > 0) { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new FileViewerForm(args[0])); } } } } ``` 在这个例子中,我们在“Main”方法中检查了命令行参数的数量。如果有参数,则创建一个新的“FileViewerForm”窗体,并将第一个参数作为文件路径传递给它进行显示。 6. 在“WinFormDefaultApp”项目中,右键单击“添加新项”,选择“安装程序”并添加一个新的安装程序项目。 7. 在“安装程序”项目中,打开“File Types”(文件类型)视图,并添加一个新的文件类型。设置文件类型的名称、描述和文件扩展名。 8. 在“文件类型”视图中,选择“打开操作”,添加一个新的打开操作,并设置其路径为您的应用程序的可执行文件路径(例如“C:\Program Files\MyApp\MyApp.exe”),并在末尾添加一个空格和“%1”参数。 9. 将您的WinForm项目编译为可执行文件。 10. 在“安装程序”项目中,右键单击“发布”并生成安装程序。 11. 运行安装程序并按照提示完成安装。现在您的WinForm应用程序已经设置为默认打开指定类型的文件了。 要获取打开文件的信息,您可以在“FileViewerForm”的构造函数中对文件进行处理。例如,您可以使用以下代码获取文件名、路径和扩展名: ```csharp public FileViewerForm(string filePath) { InitializeComponent(); label1.Text = filePath; string fileName = Path.GetFileName(filePath); string fileDir = Path.GetDirectoryName(filePath); string fileExt = Path.GetExtension(filePath); } ``` 在这个例子中,我们使用了“System.IO”命名空间中的“Path”类来获取文件名、路径和扩展名。然后,您可以对文件进行进一步的处理或显示。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

车轮滚滚向西行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值