Solved

Windows Service FileSystemWatcher access problems

Posted on 2007-04-10
8
1,617 Views
Last Modified: 2013-11-07
objective: To have a windows service running on a windows 2003 server (server 1) that listens for file changes and mirrors these changes onto server 2.

current implementation:

private const string SERVER_TWO = "\\\\server\\share$\\";

private void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
        {
            System.Threading.Thread.Sleep(5000);
            try
            {
                string outFile = SERVER_TWO + e.Name;
                if (System.IO.File.Exists(outFile))
                    System.IO.File.Delete(outFile);
                System.IO.File.Copy(e.FullPath, outFile);
            }
            catch (Exception ex)
            {
                //writes exception to file
            }
        }

implementation: I install this windows service just fine.  I have tried running it under the local system, which didn't work obviously.  However, I have also tried running it under my account which has full access to the entire network and still no success.

error: System.UnauthorizedAccessException: Access to the path '\\server\share$\subdir\test.txt' is denied.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
   at WebServerSynch.ServiceMain.fileSystemWatcher1_Created(Object sender, FileSystemEventArgs e)

I have searched for this and the solution always seems to be that the account the web service is running under lacks permissions.  In this case, that just isn't true.  Any suggestions?
0
Comment
Question by:okerupt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 3

Expert Comment

by:JipFromParis
ID: 18883088
I would suggest running with your own account for test purpose, then once fixed, trying to move to NETWORK SERVICE account which is the prefered account for such a service (local system is far to powerful for that). Now let's assume you are running with your own account.
Connect on SERVER_TWO computer and check that you have write access to both the share itself AND the underlying directory. In order to perform the check start the Administrative Tools / Computer Mangement program. Select the System Tools / Shared Folders / Shares tree node on the left. In the right pane, tight click the target share and check last two panes (Share Permissions AND Security). You should have write access on both.
Another easy interactive way to perform the test is to map the share in Windows Explorer (Tools / Map network drive menu item) then to try to drag an drop a file in the newly mapped share. If the drop succeed, you might assume you have correct permissions.

Can you proceed please and acknowledge your results.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18883122
When I tested it on my network, I wrote a quick demo;

System.IO.File.Copy(@"C:\test.bmp",@"\\10.0.0.6\c$\test.bmp");

And it worked without a problem... can you access this "directory" as you wrote it \\server\share$\subdir\, via explorer for instance?  If so, then I'm curious what account your service is running under.
0
 

Author Comment

by:okerupt
ID: 18883184
I have tried running it under the local system account, which didn't work obviously.  However, I have also tried running it under my account which has full access to the entire network and still no success.

Also, yes I can access this \\server\share$\subdirectory just fine in windows explorer with my user account.  I ran the same code as a windows program and it worked fine so something is very strange here.
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 

Author Comment

by:okerupt
ID: 18883191
Also, I add files to these server shares from my local computer all the time so my account having access is pretty much unquestionable.
0
 
LVL 5

Expert Comment

by:Yttribium
ID: 18883202
Does the release compiled version have this problem?  Or while you're debugging?
0
 

Author Comment

by:okerupt
ID: 18883232
This is the released version.  Also, I am not aware of how to debug a windows service using the visual studio 2005 IDE.  When I try, it gives me the "has to be installed using installutil" error.  
0
 
LVL 5

Accepted Solution

by:
Yttribium earned 250 total points
ID: 18883292
When you run "Lusrmgr.msc", what are the permissions under the IUSR_Computername account etc?
0
 
LVL 3

Assisted Solution

by:JipFromParis
JipFromParis earned 250 total points
ID: 18883560
WARNING : The configuration settings given below are powerful. It is discouraged to apply them on a production server "as is" without previous review by a specialist.

Now that we have cleared the basic access checks, we must fall back to the infamous .Net CAS (Code Access Security) which is a potential nightmare. These permissions are manageable from the ".Net Framework 2.0 Configuration" program to be found in the Start / Administrative Tools menu. Once there, you should go to My Computer / Runtime Security Policy / Machine / Code Groups / All_Code and expand it. Right click and create new group. On first panel giving it a meaningfull name and select Next. On the second panel, choose a URL condition type. An edit box will appear below in which you should type :

file://\\server2-name\share-name\*

where you will replace both the server name and the share name with appropriate names. On third and last pane, choose the "Full Trust" permission set then acknowledge and confirm. Now, try again to run your service. Hope this will resolve the problem. If so, you should dive into CAS configuration and devise a more restrictive policy than full trust then modify the code group you created and downgrade the trust level.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

615 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question