error when trying to programmatically access a mapped drive

G

Guest

Hello All,

I am developing an application in visual basic 2005 that will run
continuously in the background on a users workstation. This application will
automatically transfer files from the local machine to a remote server and
vise versa. It checks for the files on a timed interval. I have two problems
with this application that I am having trouble figuring out:

The first problem is that the cmd.exe shell pops up on the screen every time
it maps the drive. The application needs to be completely 'invisible' to the
user. I cannot have something popping up every several minutes while they are
trying to work.

The second issue is that I do not want to let a continuous connection to the
remote server so I have mapped a network drive to the remote server using
process.start. I have provided administrative credentials when creating the
drive. Local users do not have permission to connect to the remote server.
Also, I only want to create the drive when necessary, and then close it when
the current process is finished with it.

The drive mapping shows in My Computer when I check for it. However, when
the code tries to access the drive to transfer the files, I get an error and
the program shuts down.

The error is: system.unauthorizedaccess, eventtype: clr20r3.

I am assuming this has something to do with the user credentials, so I tried
creating the mapped drive using the same command lines in my code using the
command prompt. I was able to successfully create the mapped drive and access
it under the local user account

Any ideas/help would greatly be appreciated.

This is the section of code that I am calling when I want to create the drive:

Try

srvConn = New System.Diagnostics.Process
srvConn.StartInfo.UseShellExecute = False
srvConn.StartInfo.RedirectStandardError = True
srvConn.StartInfo.RedirectStandardOutput = True
srvConn.StartInfo.FileName = ("net.exe")
srvConn.StartInfo.Arguments = "use z: \\server\share" & " " &
My.Settings.rbsSrvPwd & "/user:" & My.Settings.rbsSrvUsr
srvConn.Start()
srvConn.WaitForExit()

Catch ex As Microsoft.VisualBasic.FileIO.MalformedLineException
Microsoft.VisualBasic.FileIO.FileSystem.WriteAllText(My.Settings.logPoll,
"Error: " & ex.Message & " occurred in rbsSrvConnection:rbsConnOpen" &
vbCrLf, True)
End Try
 
G

Guest

After some experimentation, I was able to get it to work. I am posting my
solution in the event that it will help someone else. There were two things I
was doing wrong:

1. I did not provide a redirect for the input. Seems you need to redirect
error, input, and output even if you are not using the data.

2. I was using WaitForExit in the wrong context.

Here is the code that works:

info.StartInfo.FileName = ("net.exe")
info.StartInfo.UseShellExecute = False
info.StartInfo.RedirectStandardInput = True
info.StartInfo.RedirectStandardError = True
info.StartInfo.RedirectStandardOutput = True
info.StartInfo.CreateNoWindow = True
info.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
info.StartInfo.Arguments = "use z: \\server\share " &
My.Settings.rbsSrvPwd & " /user:" & My.Settings.rbsSrvUsr

Process.Start(info.StartInfo)

If anyone sees any flaws please let me know.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top