469,887 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,887 developers. It's quick & easy.

C#, windows service, can't write to file

17
Hi.
I have a windows service that I wrote in C#. After I successfully compile it, I install it using InstallUtil, then run it with the net start command. I am trying to either log some stuff to a file, or to the Event Viewer in windows. Nothing has worked for me so far.
I am looking for help.
This is the relevant code:

Expand|Select|Wrap|Line Numbers
  1.     public partial class Service1 : ServiceBase
  2.     {
  3.         private Timer configFileTimer;
  4.  
  5.         public Service1()
  6.         {
  7.             InitializeComponent();
  8.         }
  9.  
  10.         protected override void OnStart(string[] args)
  11.         {
  12.             try
  13.             {
  14.                 FileStream fs = new FileStream(@"D:\trackerForService.txt", FileMode.OpenOrCreate, FileAccess.Write);
  15.                 StreamWriter m_streamWriter = new StreamWriter(fs);
  16.                 m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
  17.                 m_streamWriter.Write("TEST!");
  18.                 m_streamWriter.Flush();
  19.                 m_streamWriter.Close();
  20.             }
  21.             catch (System.Exception e)
  22.             {
  23.                 Console.WriteLine(e.ToString());
  24.             }
  25.  
  26.             TimerCallback configDelegate = new TimerCallback(DoConfigFile);
  27.  
  28.             configFileTimer = new Timer(configDelegate, null, 1000, 5000);
  29.         }
  30.     ...
  31.     }
  32.  
The file does not get created at all.
As I said, I also tried writing to the event viewer with EventLog.WriteEntry, but that didn't work either (my entries didn't show up once I checked the viewer, though the default start/stop entries always showup in Event Viewer).

I'd appreciate any help with this. Thank you.
Jul 25 '08 #1
1 11353
r035198x
13,262 8TB
Try it without using a FileStream as explained here.
Also make sure you have write permissions to the output folder.
Jul 26 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

9 posts views Thread by SP | last post: by
3 posts views Thread by Chris Dunaway | last post: by
4 posts views Thread by tshad | last post: by
1 post views Thread by noah.blumenthal | last post: by
2 posts views Thread by Solomon_Man | last post: by
5 posts views Thread by dm3281 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.