当用户单击按钮时,我想生成一个 .txt 文件。问题是我不知道该怎么做?我想要它,所以它使用这种格式:
00000012 <-- at the very start of the text file
2011 11 29 <-- Year 2011, Month 11, Day 29 (the year month and day based on my PC)
0010 or 0054 <-- this is a random number randomly one of these 2 numbers...
123456 <-- Time, Hour 12, Minutes 34, Seconds 56.
所以当我打开文本文件时,它应该是这样的:00000012201111290054123456
我是 C# 新手。我能够通过以下方式在 Visual Basic 上完成此操作:
Public Class Form1
'declare new random object
Dim r As New Random
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'declare string + set initial value
Dim fileString As String = "00000012"
'add formatted date
fileString &= Now.ToString("yyyyMMdd")
'add random 0010 or 0054
fileString &= New String() {"0010", "0054"}(r.Next(0, 2))
'add random 6 digit number
fileString &= r.Next(0, 1000000).ToString("000000")
'write file
IO.File.WriteAllText("C:\Program Files\Code\maincode\maincode.txt", fileString)
End Sub
End Class
我决定将最后一个随机 6 代改为时间。如何在 C# 中做到这一点?