472,779 Members | 1,941 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 software developers and data experts.

Windows Service cannot create text files?

Hi,
I just wrote a test Windows Service that creates a text file on startup
(please see my code below). The file is never created.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim swLog As StreamWriter = File.CreateText("C:\myLog.txt")
swLog.WriteLine("My Windows Service has just started.")
swLog.Close() : swLog.Flush()
End Sub
I checked the Event Viewer in the Control Panel and I found the following
error associated to my Windows Service:

"Service cannot be started. System.UnauthorizedAccessException: Access to
the path "C:\myLog.txt" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStrea ... etc."
Does anyone know why my Windows Service is unable to create a simple text
file?

Nov 21 '05 #1
3 14589

Jan. 13, 2005

Because you are unauthorized! :) That was a joke. :) What you could do
is create a new seperate folder and then grant the account that your service
runs under the rights to read/write in that folder. You would right click the
folder and on the security tab add the account. If you can't do this or you
want to grant a specific path then it might get a bit more complicated... :(
You would want to strong name your assembly... at the VS command prompt
switch to the application folder and type Sn.exe -k MyKey.pvk ... This will
create the private key necessary to sign the assembly. Then open your project
and go to the AssemblyInfo... Add:

<Assembly: AssemblyKeyFile("c:\...Path Must Be Absolute To Work")>

Then goto your control panel and (depending on your computer this part might
be slightly different) goto the administrative tools and run the .Net 1.1
Configuration tool. Then expand Runtime Security Policy and expand the node
of the scope you want. I would suggest the Machine if your service does not
run under a user account. Then expand Code Groups and then right click
All_Code and click New. (Keep in mind that this will modify your computer's
settings.) What you are going to do is create a code group that only
assemblies with your strong name are allowed in. This group will grant the
filesystem privilage that is specific to a path. Type in a name for the group
such as FilePrivForMyService and a description if desired. Then click next
and then select Strong Name from the drop down box. Then click browse and
select your assembly. This will import the strong name and only assemblies
with this strong name are qualified to be in this group. Then click the Name
and Version checkboxes if you only want assemblies with the exact name and
version to be allowed in this group too. Then click next and select Create A
New Permission set. Then type a name for it (this set will only contain the
FileIO permission for the path you specify). On the next page double click
the File IO or click it and select properties. Then make sure the button for
granting assemblies specific paths is selected. Then under file path enter
your path such as c:\ or c:\myservice\ . Then select (at least the write for
your case) the read/write/append/path disc. permissions that you need. Then
click on OK. Then click next and click finish! That was long! :| Quite a lot
of work to just write to a file! This needs some improvement! I hope this
solves your question and good night!
Joseph MCAD

"Amjad" wrote:
Hi,
I just wrote a test Windows Service that creates a text file on startup
(please see my code below). The file is never created.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim swLog As StreamWriter = File.CreateText("C:\myLog.txt")
swLog.WriteLine("My Windows Service has just started.")
swLog.Close() : swLog.Flush()
End Sub
I checked the Event Viewer in the Control Panel and I found the following
error associated to my Windows Service:

"Service cannot be started. System.UnauthorizedAccessException: Access to
the path "C:\myLog.txt" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStrea ... etc."
Does anyone know why my Windows Service is unable to create a simple text
file?

Nov 21 '05 #2
Thanks for the reply.. but this is definetly not what I'm looking for. If any
settings have to be changed on the "Local System" then they've to be changed
programatically from within the Service. Imagine deploying this Service for a
client with a lenghthy sheet of instructions for his administrator to work on!

Apparently, the problem was much simpler than that. In the "Service Process
Installer" properties, I changed the Account value from "Local Service" to
"Local System" and that did the trick!
On another note, this Windows Service doesn't start automatically! I had to
go to Control Panel --> Admin Tools --> Services, select the Service and then
click on Start. Mind that "Startup Type" in "Services" for my service is set
to "Automatic".
Do you know how to make the service start with Windows?

Amjad

"Joseph MCAD" wrote:

Jan. 13, 2005

Because you are unauthorized! :) That was a joke. :) What you could do
is create a new seperate folder and then grant the account that your service
runs under the rights to read/write in that folder. You would right click the
folder and on the security tab add the account. If you can't do this or you
want to grant a specific path then it might get a bit more complicated... :(
You would want to strong name your assembly... at the VS command prompt
switch to the application folder and type Sn.exe -k MyKey.pvk ... This will
create the private key necessary to sign the assembly. Then open your project
and go to the AssemblyInfo... Add:

<Assembly: AssemblyKeyFile("c:\...Path Must Be Absolute To Work")>

Then goto your control panel and (depending on your computer this part might
be slightly different) goto the administrative tools and run the .Net 1.1
Configuration tool. Then expand Runtime Security Policy and expand the node
of the scope you want. I would suggest the Machine if your service does not
run under a user account. Then expand Code Groups and then right click
All_Code and click New. (Keep in mind that this will modify your computer's
settings.) What you are going to do is create a code group that only
assemblies with your strong name are allowed in. This group will grant the
filesystem privilage that is specific to a path. Type in a name for the group
such as FilePrivForMyService and a description if desired. Then click next
and then select Strong Name from the drop down box. Then click browse and
select your assembly. This will import the strong name and only assemblies
with this strong name are qualified to be in this group. Then click the Name
and Version checkboxes if you only want assemblies with the exact name and
version to be allowed in this group too. Then click next and select Create A
New Permission set. Then type a name for it (this set will only contain the
FileIO permission for the path you specify). On the next page double click
the File IO or click it and select properties. Then make sure the button for
granting assemblies specific paths is selected. Then under file path enter
your path such as c:\ or c:\myservice\ . Then select (at least the write for
your case) the read/write/append/path disc. permissions that you need. Then
click on OK. Then click next and click finish! That was long! :| Quite a lot
of work to just write to a file! This needs some improvement! I hope this
solves your question and good night!
Joseph MCAD

"Amjad" wrote:
Hi,
I just wrote a test Windows Service that creates a text file on startup
(please see my code below). The file is never created.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim swLog As StreamWriter = File.CreateText("C:\myLog.txt")
swLog.WriteLine("My Windows Service has just started.")
swLog.Close() : swLog.Flush()
End Sub
I checked the Event Viewer in the Control Panel and I found the following
error associated to my Windows Service:

"Service cannot be started. System.UnauthorizedAccessException: Access to
the path "C:\myLog.txt" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStrea ... etc."
Does anyone know why my Windows Service is unable to create a simple text
file?

Nov 21 '05 #3

Jan. 14, 2005

I'm sure my users wouldn't like that either! :) I just thought you
should know that running your service as the Local System account is Very
Risky! If your service has a single security flaw that is exploited, then the
attacker could do Anything to your system! This one is not a joke! I'm taking
one of the Microsoft Certified Professional exams on security and this is
never recommended. It is possible to grant your assembly the FileIO
permission in code instead of doing it manually with the instructions I gave
you. (I didn't think of this until you said that changes would have to be
done in code.) You use the System.Security.Policy namespace. This contains
the classes that modify the Machine, User, etc. policies. You would still
want to strong name your assembly before you deploy it. Then after the user
installs the assembly, he/she would then run a .msi file that calls your code
which changes the policy. The steps for changing the policy for the user
would be just like installing an application. I don't know how to change the
policy in code, but I do know that you use that namespace and that you put
your code under a certain method in a class that inherits from Installer.
Then in your deployment project's custom actions you would have it run this
class. I hope you can find some easy examples on how to do this! :) Hope this
helps!
Joseph MCAD

"Amjad" wrote:
Thanks for the reply.. but this is definetly not what I'm looking for. If any
settings have to be changed on the "Local System" then they've to be changed
programatically from within the Service. Imagine deploying this Service for a
client with a lenghthy sheet of instructions for his administrator to work on!

Apparently, the problem was much simpler than that. In the "Service Process
Installer" properties, I changed the Account value from "Local Service" to
"Local System" and that did the trick!
On another note, this Windows Service doesn't start automatically! I had to
go to Control Panel --> Admin Tools --> Services, select the Service and then
click on Start. Mind that "Startup Type" in "Services" for my service is set
to "Automatic".
Do you know how to make the service start with Windows?

Amjad

"Joseph MCAD" wrote:

Jan. 13, 2005

Because you are unauthorized! :) That was a joke. :) What you could do
is create a new seperate folder and then grant the account that your service
runs under the rights to read/write in that folder. You would right click the
folder and on the security tab add the account. If you can't do this or you
want to grant a specific path then it might get a bit more complicated... :(
You would want to strong name your assembly... at the VS command prompt
switch to the application folder and type Sn.exe -k MyKey.pvk ... This will
create the private key necessary to sign the assembly. Then open your project
and go to the AssemblyInfo... Add:

<Assembly: AssemblyKeyFile("c:\...Path Must Be Absolute To Work")>

Then goto your control panel and (depending on your computer this part might
be slightly different) goto the administrative tools and run the .Net 1.1
Configuration tool. Then expand Runtime Security Policy and expand the node
of the scope you want. I would suggest the Machine if your service does not
run under a user account. Then expand Code Groups and then right click
All_Code and click New. (Keep in mind that this will modify your computer's
settings.) What you are going to do is create a code group that only
assemblies with your strong name are allowed in. This group will grant the
filesystem privilage that is specific to a path. Type in a name for the group
such as FilePrivForMyService and a description if desired. Then click next
and then select Strong Name from the drop down box. Then click browse and
select your assembly. This will import the strong name and only assemblies
with this strong name are qualified to be in this group. Then click the Name
and Version checkboxes if you only want assemblies with the exact name and
version to be allowed in this group too. Then click next and select Create A
New Permission set. Then type a name for it (this set will only contain the
FileIO permission for the path you specify). On the next page double click
the File IO or click it and select properties. Then make sure the button for
granting assemblies specific paths is selected. Then under file path enter
your path such as c:\ or c:\myservice\ . Then select (at least the write for
your case) the read/write/append/path disc. permissions that you need. Then
click on OK. Then click next and click finish! That was long! :| Quite a lot
of work to just write to a file! This needs some improvement! I hope this
solves your question and good night!
Joseph MCAD

"Amjad" wrote:
Hi,
I just wrote a test Windows Service that creates a text file on startup
(please see my code below). The file is never created.

Protected Overrides Sub OnStart(ByVal args() As String)
Dim swLog As StreamWriter = File.CreateText("C:\myLog.txt")
swLog.WriteLine("My Windows Service has just started.")
swLog.Close() : swLog.Flush()
End Sub
I checked the Event Viewer in the Control Panel and I found the following
error associated to my Windows Service:

"Service cannot be started. System.UnauthorizedAccessException: Access to
the path "C:\myLog.txt" is denied.
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStrea ... etc."
Does anyone know why my Windows Service is unable to create a simple text
file?

Nov 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Razor Works via .NET 247 | last post by:
I have got a windows service installed through installutil in C#.I have change the login settings from localsystems todomain/user. The user can access the network resources with noproblems but if the...
2
by: Neslihan ERDEM | last post by:
Every body Hi first of all I say Why do I need Windows Service / Every Day I create XML file . I writed a XML web service And .I join this servis Windows service. I create Windows Service that...
2
by: Jesper Stocholm | last post by:
I have created a simple service which just copies a fil to a new file with a new name on certain intervals (the service implements a timer). I have no problems installing the service and the...
2
by: Kiko + | last post by:
Hi, I've been getting this error: Server Error in '/applicationname' Application. ---------------------------------------------------------------------------- ---- Failed to create...
5
by: BigJohn | last post by:
I have a VB.Net 2003 web service to create Acrobat files using Acrobat Professional 7.0.8. The application executes: public class XYZ... Private objBlankDocument As Acrobat.AcroPDDoc Private...
10
by: dermot | last post by:
I have wrriten a small windows service application in visual studio ..net 2003 which listens for incoming FTP files. These files would overwrite over time due to duplicate file names. However any...
4
by: stuart.jones | last post by:
Dear all I have a Windows Service that retrieves some data as XML, applies an XSLT to produce a CSV which is saved to the filesystem as a text file. This all works fine. Depending upon the...
2
by: sebouh181 | last post by:
Hi, I have a Windows Service that copies xml files from a shared folder on the server. I am using System.IO.File.Copy(\\Server-Name\SharedDir) method. But whenever I try to copy files there is...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.