File IO Behaviour

I have a windows Service that writes an XML file to a specific location.

The details are as follows:

I am using VS 2005 Beta 2
I am running on XP SP2
The service runs using the local system account
I am writing to a mapped drive which is actually a UNIX server (using Samba).
The xml is created with the XmlTextWriter class.

The problem is I get the following exception every time  I try to write to the mapped drive:

Exception thrown:System.IO.DirectoryNotFoundException: Could not find a part of the path 'm:\00000001.xml'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at System.Xml.XmlTextWriter..ctor(String filename, Encoding encoding)

The mapped drive is valid, I can access it via Windows Explorer and copy files to and from it with no problems.

If I run the same XML creation code from a Windows Forms application writing to the same location, I get no problems writing to the mapped drive.

There seems to be a difference in behaviour if the application is  Windows Service or a Windows Exe.

Can anybody please help me




Answer this question

File IO Behaviour

  • atoast

    Marinos,

    Are you running this service under your own account or under the system account
    If you are running it as system and the Windows Forms app as yourself, that is very likely to be the cause of the differing behaviour.  It's likely that your system account either doesn't have the drive mapped, or does not have privileges to access the drive.  Try accessing the share under it's UNC name (\\machinename\sharename) and see if that changes anything.

    Also, writing to a mapped drive from a service is not good behaviour.  The service may start up before any user logs on and will not have drive mappings, so will fail.  UNC's with services are the way to do it properly.

    Dave


  • jena

    Thanks Dave,

    I tried running the service using my account, the behavious though is the same. The problem regarding using the UNC name is that being a UNIX box it doesn't work like a Windows machine.





  • Prakash Raman

    Hi,

    Goto Control Panel->Administrative Tools->Services.

    In the MMC for Services, locate your service.http://16.209.13.91/AspNetApp/WebForm1.aspx
    Click Properties and goto the LogOn Tab. Here make sure you specify your account details with the password.

    That should solve the problem.

    Regards,
    Vikram

  • newbie777

    Marinos,

    Surely SAMBA takes care of those details and presents a UNC for you to use   Otherwise, how can you map a drive to it
    The golden rule when running as an unattended process (i.e. a service) is that you must not use anything that will only be available after a user has logged on, such as mapped drives.  This is because a service will run on the machine before any user logon occurs, no matter what the user account it is running under.

    Dave

  • File IO Behaviour