Reputation: 4364
I'm very new to .Net, C# and Visual Studio. Now I want to develop a Windows Service application using C#. The Windows Service needs to write some data to some file. I'm using Visual Studio 2010. I'm using Windows XP as my operating system. I created a windows service.its installer etc.
When I install the Windows Service in the folder other than non-default folder (other than C drive), after running service it will create the file and write the text file success.
When I install the service in the default location (that when the time of installation the i cannot the default path) (please refer the attached image)
Then after installing then I run the service. But the file is not created and there is no exception thrown. The following are the codding snippet for file.
string logPath = AppDomain.CurrentDomain.BaseDirectory + "log\CastrolSdWindowsService_Log.txt";
fs = new FileStream(logPath, FileMode.Append, FileAccess.Write);
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
I cannot find why the file is not created in C drive.
Upvotes: 1
Views: 5446
Reputation: 645
You can chose between (as wrote above) either change path to log files and move them outside protected program file folder, or you can change NTFS permissions to allow WRITE/MODIFY on those log files and folders that you want to write inside "program file/your app" folder.
Note, that your windows service application did not run as SYSTEM. it may be NT AUTHORITY\LOCAL SERVICE or NT AUTHORITY\NETWORK SERVICE Run your service then run process explorer, switch on column "USER" and check it.
Upvotes: 0
Reputation: 1214
As stated in the comments the Program Files directory is going to be protected by the operating system and isn't the ideal place to place your application generated files. The best place to store these files is in the application data folder. There are some helper methods in the .net framework that will help you locate the folder dynamically by utilizing the Environment class and using one of the Environment.SpecialFolder enumerations to get the path to the folder.
Upvotes: 1