Here goes the sample code:

using System.IO;
using System.Management;
using System.ServiceProcess;
 
namespace MyTestService
{
    public partial class MyTestService : ServiceBase
    {
        public MyTestService()
        {
            InitializeComponent();
        }
 
        protected override void OnStart(string[] args)
        {
            TextWriter tw 
		= new StreamWriter("c:\\success.txt");
            tw.WriteLine("Successfully created file.");
            tw.WriteLine(CurrentUserName());
            tw.Close();
 
        }
 
        protected override void OnStop()
        {
        }
 
        private string CurrentUserName()
        {
            var s = new ManagementObjectSearcher
		("SELECT UserName FROM Win32_ComputerSystem");
 
            string str = string.Empty;
 
            foreach (ManagementObject moobj in s.Get())
            {
                if (moobj["UserName"] != null)
                {
                    str = moobj["UserName"].ToString();
                }
            }
 
            return str;
        } 
    }
}
VN:F [1.1.6_502]
Rating: 0.0/5 (0 votes cast)
Share:
  • Digg
  • del.icio.us
  • Facebook
  • Google
  • DotNetKicks
  • LinkedIn
  • Live
  • MySpace
  • StumbleUpon
  • Technorati

Related posts:

  1. Microsoft XNA - Quick sample
  2. Return DataTable from Web Service
  3. Create SharePoint 3.0 Custom Properties
  4. Visual SourceSafe Programming
  5. Build DateTime from values