Bob,
I do appreciate your attempts to be helpful, but you are wrong on two
points:
1) You do not "understand [what I've written] perfectly."
2) Services CAN, as I described previously, see mapped drives. For proof,
perform the following simplified test (example created using C#, VS2005,
and
.NET 2.0 Framework)...
First, create a new Windows Service with the following in its
ServiceBase.OnStart() method:
//=========== Begin Code Snippet ==============
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
proc.StartInfo.FileName = "net";
proc.StartInfo.Arguments = "use w: \\\\serverA\\share1";
proc.Start();
proc.WaitForExit();
System.IO.StreamWriter sw = new
System.IO.StreamWriter("w:\\MappedDriveOutput.txt", false); //note the
reference to mapped drive w:
sw.AutoFlush = true;
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo("w:");
//again,
note the reference to mapped drive w:
System.IO.FileSystemInfo[] files = dir.GetFileSystemInfos();
foreach (System.IO.FileSystemInfo info in files)
{
sw.WriteLine(info.FullName);
}
sw.Close();
sw.Dispose();
//=========== End Code Snippet ==============
Once you have created your service, install it and run under
DOMAIN\Administrator user account context (making sure
DOMAIN\Administrator
has read/write rights to \\serverA\share1).
Make sure there are files in share1 on serverA.
Start the service. The service shall attempt to map drive w: to
\\serverA\share1 as DOMAIN\Administrator, created a file on "w:", and
write a
directory listing of "w:" to that file before finishing its OnStart()
method.
If you do not get the same results, let me know where it is failing and
what
error you are getting and we can try to reconcile. Perhaps there's some
subtle difference in our environments, such as O/S version, etc. (I'm
running
on Win2k3, by the way).
Otherwise, if you DO get the appropriate output file, please explain how
the
service you created was able to read from and write to "w:" if "w:" was
not
mapped and available to the service?
Hopefully, this will move us beyond this sticking point and get back to
the
question I'm asking about the ProcessStartInfo.WorkingDirectory property
not
accepting a reference to a mapped drive while running as a service.
Bob Milton said:
No I understood perfectly. A windows service runs with no users
logged
on. Mapped drives are attached to logged on users. Ergo, services NEVER
see
mapped drives. That is your problem.
Bob
message
Bob,
With respect, I think you've missed the point of my question and
previous
comments - perhaps through a miscommunication of my own.
I'll attempt to clarify one last time...Here's the scenario:
1) I've written a Windows Service (C# and .NET Framework 2.0). This
Windows
Service has its properties set such that it runs under the context of a
particular user account. For simplicity's sake, assume this user to be
the
Domain Administrator account (DOMAIN\Administrator).
2) In the service's OnStart() event, the first thing I do is map a
network
drive by executing the Start() method of a System.Diagnostics.Process
object
whose ProcessStartInfo object has been configured such that my service
essentially executes the following command: "net use w:
\\serverA\share1".
From this point forward, the mapped network drive "w:" IS available to
my
service. Bob, while you are correct that by default no drives are
automatically mapped when a Windows Service starts, you are incorrect
in
implying that a service cannot map a network drive or access a mapped
drive
once the service is running - it can. I'll be glad to send you example
code
if you would like to try it out for yourself.
3) Now that the drive "w:" is mapped and accessible to my service in a
general way (i.e., I can perform a directory listings on "w:", I can
write
files to "w:", I can read files from "w:", etc.), I create a new
Process
object. With this new Process object, I'd like to explicitly set its
ProcessStartInfo.WorkingDirectory property to a path on "w:" (remember,
"w:"
IS available to my service at this point in time).
4) When I set the WorkingDirectory to any path on "w:", the call to
Start()
fails. However, when I set the WorkingDirectory to any path on "c:",
the
Start() method of the Process object executes just fine. Hence, my
problem.
Hopefully, I've made it clear that I'm not having trouble with or
confused
about mapping or accessing a mapped network drive from within a
service.
I
can prove that my service maps the drive and is accessible for things
such
as
directory listings, file creation, etc.
Rather, what I seek is an understanding why it seems the
ProcessStartInfo.WorkingDirectory doesn't like to be given a path that
incorporates a mapped drive when the Process object's Start() method is
called from within a service. Make more sense?
:
A windows service starts before anyone logs on. At that time,
there
are
no mapped drives. When you log on, you can see w:, but your service
has
failed by then.
Services are designed that way on purpose - all the computer has
to
do
is start up. SQL Server is a great example - you can set it up, put
the
computer in a closet, and leave it alone and still use SQL from a
client
machine.
Bob
message
Thanks for the reply Bob. However, I have two comments/questions...
1) While running as a Windows Service, I can map a drive and access
it
like
a local drive - it's just ProcessStartInfo.WorkingDirectory fails
when
I
set
it to a mapped drive. So, when you say "there are NO mapped drives"
I
would
argue that there are...once you map them. Did I misunderstand your
statement?
2) In my code, ProcessStartInfo.WorkingDirectory doesn't like UNC
paths
either. Have you experienced something different?
:
When a windows service starts, no one is logged into the
computer.
Until
someone logs in, there are NO mapped drives! So, you will have to
use
UNC
paths (//server/share1/dir1).
Bob
message
Just discovered a critical piece of the puzzle...
The Start() method fails, as described below, only when running
as a
Windows
Service. When I try the same thing from s straight Console App
while
logged
in as the service account user, Start() succeed. However, when
running
as
a
Windows Service it doesn't seem I can set WorkingDirectory to any
mapped
drives...even though I have confirmed that the mapped drive does
exist
and
is
accessible to the service account.
So, a refinement of my question should read "Has anyone
successfully
set
ProcessStartInfo.WorkingDirectory to a mapped network drive while
running
as
a Windows Service?"
:
Has anyone successfully called the Start() method on a
System.Diagnostics.Process object with the object's
ProcessStartInfo.WorkingDirectory set to a path on a mapped
network
drive?
For example, if I have "w:" mapped to "\\serverA\share1" and set
ProcessStartInfo.WorkingDirectory = "w:\dir1", my calls to
Process.Start()
always fail with the error "The directory name is invalid".
Whereas, if I do the same thing with WorkingDirectory =
"c:\dir1",
calls
to
Process.Start() always succeed.
Before anyone asks, yes the directory "dir1" does exist on w:
and
is
readable under the user context calling the Process.Start()
method.
If no one has successfully executed Process.Start() with
WorkingDirectory
pointing to a mapped network drive, does anyone have any (MS)
reference
article confirming that this is not possible and perhaps
explaining
why?
Thanks in advance.