如何在C#2010 windows应用程序中每天从“1”开始生成序列号 [英] How to generate serial number every day staring from “1” in C# 2010 windows application

查看:206
本文介绍了如何在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

推荐答案

windows应用程序 - 你的意思是WinForms?还是控制台?无论哪种方式,你都不会说序列号'包裹'时 - 你可以使用像



windows application - do you mean WinForms ? or Console ?? either way, you dont say when the serial number 'wraps around' - you could just use the something like

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)


一个简单的解决方案让你入门:

A simple solution to get you started:
private static int GetSerialNumber()
{
    string start = "2016-05-14";
    return (DateTime.Now - DateTime.Parse(start)).Days;
}


您需要保留一个参考日期,用于在创建序列号时与当前日期进行比较。

如果当前日期与参考日期不同,则将序列号重置为1.

您还需要存储最后一个序列号,以便关闭应用程序并继续再次启动时序列中的下一个数字。



您可以使用应用程序设置,参见应用程序设置 [ ^ ]



您还可以使用其他方法存储变量,例如文本文件或XML文件。



创建新的序列号时:

You need to keep a reference date that you use to compare with the current date when you create the serial number.
If the current date is different from the reference date, you reset the serial number to 1.
You also need to store the last serial number, so you can close the application and continue with the next number in the sequence when you start it again.

You can use the Application Settings for this, see Application Settings[^]

You can also use other methods to store the variables, e.g. a text file or XML file.

When creating a new serial number:
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%正确的,但校长应该是正确的。< br $>


[更新]

此代码应该可以胜任。



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”开始生成序列号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆