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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:31:59+00:00 2026-05-25T13:31:59+00:00

I am creating a windows service to save current username and logon time in

  • 0

I am creating a windows service to save current username and logon time in a CSV file. But when I install the service, the file is just created, not details are written to it, and that file is set to read-only mode. what is the problem in my code?

My code:

   Imports System.IO
   Imports System.Text
   Imports System.DirectoryServices
   Imports System.DirectoryServices.AccountManagement
   Imports System.Diagnostics
   Public Class LoginService1

      Protected Overrides Sub OnStart(ByVal args() As String)
        Dim win As System.Security.Principal.WindowsIdentity
        win = System.Security.Principal.WindowsIdentity.GetCurrent()
        Dim Logon_UserName = win.Name.Substring(win.Name.IndexOf("\") + 1)
        Dim pc As New PrincipalContext(ContextType.Machine, My.Computer.Name)
        Dim uc As UserPrincipal = UserPrincipal.FindByIdentity(pc, Logon_UserName)
        Dim str As String = ""
        Try
           Dim sr As New StreamReader("c:\logondetails.csv")
           str = sr.ReadToEnd()
           sr.Close()
           sr.Dispose()
        Catch ex As Exception
        End Try
        Try
           Dim sw As New StreamWriter("c:\logondetails.csv")
           Str += Logon_UserName & "," & uc.LastLogon.Value.ToString
           sw.WriteLine(str)
           sw.Close()
           sw.Dispose()
        Catch ex As Exception
        End Try
   End Sub

   Protected Overrides Sub OnStop()
      ' Add code here to perform any tear-down necessary to stop your service.'
   End Sub

  End Class
  • 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-25T13:32:00+00:00Added an answer on May 25, 2026 at 1:32 pm

    There are many ways to fix what you are doing.

    For 1, you dont need to read in the entire file to append to the end of it. For example

    using(var stream = File.AppendText(fileName))
                {
                stream.Write(string);
                stream.Close();
                }
    

    But you are going to have issues with concurrent access to the file. You’d be better off using a logging framework, such as log4net or microsoft logging block, and then just

    Logger.Info(yourstring);
    

    and be done with it. The framework will handle the file access issues.

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

Sidebar

Related Questions

I just started creating a Windows Service; out of curiousity, selected English (United States)
I have got few questions regarding creating windows service. I have created a windows
I am creating another windows service and my timer is not ticking and I
Im creating a Windows Service and I want to put a dynamic path in
I am creating a Windows Service in C# that processes messages from a queue.
I am creating a windows service and want to know best practices for this.
I am creating a Windows service. When an exception occurrs, I handle it appropriately
I am creating a windows service that starts a number of slave processes. In
I am developing a .NET Windows service that is creating a couple of threads
I am creating Windows service class in Python that will eventually display a Window

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.