C#生成流水號,序列號,不重復,使用本地txt記錄。


需建立本地文本,(@"..\liushuihao\AAA.txt"),修改此路徑為你保存的txt本地文本路徑。此方法只是提供一種思路,具體格式還需自己修改。

public void sn(string hh)//返回值hh就是流水號。
        {
            //流水號
            //LAyyyyMMdd   0001(序列號)
            string liushuihao;
            string aa = "LA";//固定字符
            string bb = DateTime.Now.ToString("yyyyMMdd");//年月日
            try
            {
                string[] allLines = File.ReadAllLines(@"..\liushuihao\AAA.txt");//讀取文本記錄最后一行數據
                if (allLines == null) { MessageBox.Show("為空"); return; }
                string lastestLine = allLines[allLines.Length - 1];
                string gudingzifu = lastestLine.Substring(0, lastestLine.Length - 4);

                Console.WriteLine(gudingzifu);
                if (gudingzifu == aa + bb)
                {
                    string xuliehao = lastestLine.Substring(10);
                    int xuliehao1 = Convert.ToInt16(xuliehao);
                    xuliehao1++;
                    string xuliehao2 = xuliehao1.ToString("0000");//將1轉換成0001
                    liushuihao = aa + bb + xuliehao2;
                    hh = liushuihao;
                    FileStream fs = new FileStream(@"..\liushuihao\AAA.txt", FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(hh);//寫入你此次生成的字符串
                    //fs.Close();
                    sw.Close();
                }
                else
                {
                    hh = aa + bb + "0001";
                    FileStream fs = new FileStream(@"..\liushuihao\AAA.txt", FileMode.Append);
                    StreamWriter sw = new StreamWriter(fs);
                    sw.WriteLine(hh);//寫入你此次生成的字符串
                    //fs.Close();
                    sw.Close();
                }
            }
            catch
            {
                MessageBox.Show("文本為空!!!");
            }
        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM