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?