本文介绍了如何在C#2010 windows应用程序中每天从“1”开始生成序列号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 How to generate serial number every day staring from "1" in c# 2010 windows application 我的尝试: 如何每天从c#2010 windows应用程序中的1开始生成序列号What I have tried:How to generate serial number every day staring from "1" in c# 2010 windows application推荐答案 StringBuilder sb = new StringBuilder(DateTime.Now.Format("yyyy");sb.append("{0:D3}", DateTime.Now.DayOfYear);string serial = sb.ToString(); 会给你2016001..2016366(2016年是闰年)that would give you 2016001..2016366 (2016 is/was a leap year) private static int GetSerialNumber(){ string start = "2016-05-14"; return (DateTime.Now - DateTime.Parse(start)).Days;} if (DateTime.Now.Date > Properties.Settings.Default.ReferenceDate.Date){ Properties.Settings.Default.ReferenceDate = DateTime.Now; Properties.Settings.Default.LastSerialNumber = 1;}else{ Properties.Settings.Default.LastSerialNumber++;} 当程序关闭时: When the program closes:Properties.Settings.Default.Save(); 不能发誓语法是100%正确的,但校长应该是正确的。 [更新] 此代码应该可以胜任。 Cannot swear that the syntax is 100% correct, but the principal should be right.[UPDATE]This code should do the job.using System;using System.Windows.Forms;namespace SerialNumberReset{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { tbSerialNumber.Text = GetSerialNumber().ToString(); } private int GetSerialNumber() { if (DateTime.Now.Date > Properties.Settings.Default.ReferenceDate.Date) { Properties.Settings.Default.ReferenceDate = DateTime.Now; return 1; } else { return Properties.Settings.Default.SerialNumber; } } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Properties.Settings.Default.Save(); } private void buttonUpdateSerialNumber_Click(object sender, EventArgs e) { Properties.Settings.Default.SerialNumber = GetSerialNumber() + 1; tbSerialNumber.Text = Properties.Settings.Default.SerialNumber.ToString(); } }} 这篇关于如何在C#2010 windows应用程序中每天从“1”开始生成序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 11-01 02:07