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.
2条答案
按热度按时间wwwo4jvm1#
如注解中所述,Program Files目录将受到操作系统的保护,并不是放置应用程序生成的文件的理想位置。存储这些文件的最佳位置是应用程序数据文件夹。net框架,它将帮助您通过利用Environment类并使用Environment.SpecialFolder枚举之一获取文件夹的路径来动态定位文件夹。
hc8w905p2#
您可以选择(如上所述)要么更改路径到日志文件,并将其移出受保护的程序文件夹,或者您可以更改NTFS权限,以允许对那些日志文件和文件夹,你想写在“程序文件/你的应用程序”文件夹内的写入/修改。
注意,您Windows服务应用程序没有以SYSTEM运行它可能是NT AUTHORITY\LOCAL SERVICE或NT AUTHORITY\NETWORK SERVICE运行您服务,然后运行进程资源管理器,打开'USER'列并检查它