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 3754354
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T09:31:55+00:00 2026-05-19T09:31:55+00:00

I am new to C# and have code that will obtain the userName from

  • 0

I am new to C# and have code that will obtain the userName from the LogIn and begin a program in the background if this is the users first time loging into the system. However, today I noticed when running the program and checking my log file that the program skips adding data to the file that is also created on the initial run. Any run after the initial run, the userName is included into the .log file and if the userName does not match what is in the .log file the file is overwritten to include the new userName. Can someone please help me figure out what happened or if I’m missing something?

Thank you in advance.

 using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Diagnostics;

    namespace User
    {
        public partial class Form1 : Form
        {
            public const string dir = @"C:\Numara";
            public const string path = dir + @"\Audit.log";
            public const string TrackIT = @"C:\Program Files\Numara Software\Numara Track-It!\Track-It! Server\Audit32.exe";

            public Form1()
            {
                InitializeComponent();
            }

            private void Form1_Load(object sender, EventArgs e)
            {
                //returns user name
                //label1.Text = System.Environment.UserName.ToString();
                string userName = System.Environment.UserName;  //user name

                if (!Directory.Exists(dir))
                    //directory does not exist
                    //create it
                    Directory.CreateDirectory(dir);  //creates directory


                //by this point directory is created

                //now check file
                if (!File.Exists(path))
                    //file does not exist, so create it
                    File.Create(path);


                //Read data from the .log file
                string line = System.IO.File.ReadAllText(path);

                //if the name of the logged in user
                //is the same as the user name of the text file
                //then exit program

--------- the debugging stops here and skips to end the program on the first run---------

                if (line == userName) 
                    Application.Exit();

                else
                //clear fields and write new name to file and begin audit
                {
                    //clears fields
                    using (FileStream stream = new FileStream(@"C:\Numara\Audit.log", FileMode.Create))
                    {
                        using (TextWriter writer = new StreamWriter(stream))
                        {
                            //writer.Write("");
                            writer.Write(userName);
                        }
                        // writes new name to file
                    }

                      StreamReader textIn =
                      new StreamReader(
                      new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read));


                    //begins audit

                    Process.Start(TrackIT, "/Q");
                    Application.Exit();
                    }        
                }
            }
        }
  • 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-05-19T09:31:56+00:00Added an answer on May 19, 2026 at 9:31 am

    File.Create creates a file and returns a FileStream, so it does not close it. Next you want to read it, but it is locked by the File.Create. So exception appears which is not handled and your application quits. Second times the file is already created, so it skips creating and locking it.

    So you need to close the file by putting it into using block:
    using (File.Create(path))
    {
    }

    Also, you should put all the stuff into try catch blocks to handle IO exceptions – you can never know if one occurs.

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

Sidebar

Related Questions

I have some code that effectively does this : File file = new File(C:\\Program
I have a bit of code that looks like this: text = reg.Replace(text, new
Right now, I have code that looks something like this: Timer timer = new
I have some code that creates a new site in SharePoint. Upon browsing to
I have the following snippet of code that's generating the Use new keyword if
I have created a new solution with a minimal amount of code that represents
I have code like this: var newMsg = new Msg { Var1 = var1,
I have this code :- using (System.Security.Cryptography.SHA256 sha2 = new System.Security.Cryptography.SHA256Managed()) { .. }
I have see code like this Dim s as something = new something Dim
Suppose I have this code: var myArray = new Object(); myArray["firstname"] = "Bob"; myArray["lastname"]

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.