Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8544181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T12:29:34+00:00 2026-06-11T12:29:34+00:00

I want to generate a .txt file when user clicks a button. The issue

  • 0

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#?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T12:29:35+00:00Added an answer on June 11, 2026 at 12:29 pm

    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 Random instance, r, is declared at the form (module) scope, but you only use it inside your Form1_Load procedure: 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 what System.Security.Cryptography.RNGCryptoServiceProvider is 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 about Text.StringBuilder and 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.Format and its custom format strings comes in extremely handy. For example, your original VB.NET code can be rewritten as follows:

    Sub Form1_Load(s As Object, e As EventArgs)
      Using sw As New IO.StreamWriter(IO.Path.Combine(
                Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
                "maincode.txt"))
        sw.WriteLine("00000012{0:yyyyMMdd}{1}{0:HHmmss}", Now,
                     If((New Random).Next(0, 2) = 0, "0010", "0054"))
      End Using
    End Sub
    

    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 of Environment.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:

    public void Form1_Load(object s, EventArgs e)
    {
      using (System.IO.StreamWriter sw = new System.IO.StreamWriter(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "maincode.txt"))) {
        sw.WriteLine("00000012{0:yyyyMMdd}{1}{0:HHmmss}", DateAndTime.Now, (new Random()).Next(0, 2) == 0 ? "0010" : "0054");
      }
    }
    

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a test script, that generate txt file with answers. And have 2
When I click the button (btnPrintTardy) I need to generate a .txt file of
I have a file that I am creating on the fly like this: //
Let's say I have a class of 30 students and want generate every possible
I want to generate a bar graph from from a csv file my data
I have a file named gcc.exe and I have a php page... I want
I would like to use Perl to take a previously generated SPSS syntax file
Basically, I have a file 'blah.txt'. That files gets parsed by a 'compiler' and
I have a file words.txt in which each line is a word, followed by
I have the following context free grammar in a text file 'grammar.txt' S ::=

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.