I want to generate a .txt file when user clicks a button. The issue is I am not sure how to do it? I would like to have it so it uses this format:
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.
so when I open the text file it should be something like this:
00000012201111290054123456
I am new to C#. I was able to accomplish this on visual basic with this:
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
I have decided to change the last random 6 generation to the time instead. How to do it in C#?
The easiest way to solve your problem would be to use
String.Format, which works pretty much the same way in VB.NET as it does in C#.Before getting to that, though, it’s worth addressing some issues with your VB.NET code; fixing these will make your C# code a lot better as well:
Your
Randominstance,r, is declared at the form (module) scope, but you only use it inside yourForm1_Loadprocedure: it’s a good idea to always keep your variable scope as limited as possible, in other words keep your declarations “as close to” the usage as you can;While on the subject of
System.Random: do keep in mind that this actually creates a quite predictable (time-based) series of numbers, so it shouldn’t be used for anything security-related (that’s whatSystem.Security.Cryptography.RNGCryptoServiceProvideris for). Your usage of Random isn’t very strong anyway, and I’m not sure of your actual use case, but this is always something to keep in mind;You use comments that almost literally describe what your code does, which is rather pointless: only add comments to add additional insight into why your code does the things it does, not how;
Strings in .NET are immutable, which means that each time you use the
&operator, you create a new string, leaving the old one to linger around until garbage collection kicks in. That gets really expensive after a while: instead, learn aboutText.StringBuilderand use it whenever needed;You create a file under
C:\Program Files. Apart from the fact that that directory may have a different name on different versions of Windows (not to mention Linux, in case you’re running on Mono), users don’t have permission to write there on any non-legacy (i.e. post-XP) versions of Windows. So, if you do this, your program will crash when distributed: if you learn to use the proper file locations early on, that will save you lots of trouble.Anyway, on to your question: whenever you want to create a string with lots of parameters and/or formatting,
String.Formatand its custom format strings comes in extremely handy. For example, your original VB.NET code can be rewritten as follows:When executed, this should create
C:\Users\YourUserName\AppData\Roaming\maincode.txt(or somewhere else if you’re on XP or certain localized versions of Windows: check the value ofEnvironment.GetFolderPath(Environment.SpecialFolder.ApplicationData)in the debugger to find out).Translating this to C# is trivial: if you use one of the many VB.NET to C# translators available online, it should give you something like this:
Basically, it added some semicolons and curly braces for you, included fully-qualified namespaces and rewrote the ternary condition: the .NET Framework bits are identical.
Hope this helps, and good luck with your programming efforts!