Link to home
Start Free TrialLog in
Avatar of jbaisden
jbaisden

asked on

Programmatically copying and writting files across a network in vb. net, vb .net 2005



I am writting a program to automate a file copying process to a server machine. We'll call the Server Bob.
My computer is JBaisden. To access Bob, or at least, to access the C drive you must enter a user name and password. I have this information; however, my problem is this: I am using a FolderBrowserDialog (VB .NET 2005) to set the file paths that determine what files are to be backed up and what files are to be copied. This control/dialogue allows me to browse to Bob, but only shows shared folders. I really need to browse to Bob's C drive and work from there. Given that I know the user name and password, how do I go about making this happen?

I have tried using text boxes so the user can enter the folder path manually, but I'm getting a wierd file access error to a file that I'm trying to copy. Also note that this occurrs on Jbaisden when I am not going across a network to access files (This was a test scenerio that I am still having trouble with).

Any idea's, articles, or suggestions would be greatly appreciated.
Avatar of a b
a b

you need a share - but there is a hidden share by default; try \\bob\c$
Avatar of jbaisden

ASKER

See I know that if I type \\bob\c$ in the run path it will work. But the folder I need access to will not be shared. This is exactly what I need a workaround/implementation for.
i think the only other way to do it is to have some kind of server component - something that runs on the other machine, with the security privileges that goes with it, that can enumerate the folders etc. on behalf of your client component.
this seems to be the solution that the malicious-software industry has chosen to get around the same problem that you are having.
a variation on the theme is to run a web server on bob and allow ftp access to c:


Yikes! I was hoping for a better way. I suppose I'll look into some network programming and see if perhaps there is some way to access a machine or some jazz like that. This is far more complicated than I thought. Oh well. Thanks for your help. I'm gonna keep this open to see if anyone else has some ideas, or if you think of anything further. Thanks again!
ASKER CERTIFIED SOLUTION
Avatar of simoncampbell
simoncampbell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for the idea! Is there a way to handle the username and password when doing this; otherwise, I don't think I'll have access?

Also, when I add

   <StructLayout(LayoutKind.Sequential)> _
    Class NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public LocalName As String
        Public RemoteName As String
        Public Comment As String
        Public Provider As String
    End Class 'NETRESOURCE

I get an error. The suggestion from VS is to make it:


    <Runtime.InteropServices.StructLayout(LayoutKind.Sequential)> _
   Class NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        Public LocalName As String
        Public RemoteName As String
        Public Comment As String
        Public Provider As String
    End Class 'NETRESOURCE

But then it highlights LayoutKind.Sequential and says that LayoutKind is undeclared.

Is there an import I am missing? I tried importing Runtime.InteropServices, but that didn't fix this.

Any suggestions?
Nevermind about the username and password. I didn't read the code carefully enough. Now all I need is to figure out those errors I'm getting, which I'm guessing is more of a reference issue than anything.
Yeah, I think you might need to add a reference to Microsoft.VisualBasic to your project.
Think you also need to import System.Runtime.InteropServices
Alright I figured out that first issue with the structlayout was just a syntax issue. It wasn't happy until I made it look like:    
<System.Runtime.InteropServices.StructLayout(Runtime.InteropServices.LayoutKind.Sequential)> _

I also moved:

     Dim myNetResource As New NETRESOURCE
        myNetResource.dwScope = 2 'RESOURCE_GLOBALNET
        myNetResource.dwType = 1 'RESOURCETYPE_DISK
        myNetResource.dwDisplayType = 3 'RESOURCEDISPLAYTYPE_SHARE
        myNetResource.dwUsage = 1 'RESOURCEUSAGE_CONNECTABLE
        myNetResource.LocalName = "P:"
        myNetResource.RemoteName = "\\bob\c$"
        myNetResource.Provider = Nothing
        Dim ret As Integer = WNetAddConnection2(myNetResource, Nothing,
Nothing, 0)

to the page load section. I filled in the real server, password, and user name and it picked up the C$ drive alright; however, it wouldn't let me browse or view anything in the C drive. The drive was merely selectable in the open folder dialogue. I went ahead and used the path it had found so far, which was \\bob\C, but this generated the following error. It's curious. I know I have admin prvilleges on the server with this username and password. Any further suggestions would be of great value. Thanks!


System.IO.IOException was unhandled
  Message="The system detected a possible attempt to compromise security. Please ensure that you can contact the server that authenticated you.
"
  Source="mscorlib"
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.Directory.InternalGetFileDirectoryNames(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
       at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)
       at System.IO.Directory.GetFiles(String path)
       at FileCopier.FrmFileCopyMain.Button1_Click(Object sender, EventArgs e) in C:\Personal App Development\FileCopier\FileCopier\FileCopier\FrmFileCopyMain.vb:line 62
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnClick(EventArgs e)
       at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.ButtonBase.WndProc(Message& m)
       at System.Windows.Forms.Button.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
       at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       at System.Windows.Forms.Application.Run(ApplicationContext context)
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
       at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
       at FileCopier.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
       at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Now this is interesting...I don't know what exactly it is, but I am guessing that I had to connect to the server prior to running this code. After I did that, I could access anything on the C drive I wished. Of course, now I have two minor issues. They don't prevent the program from working, but it would be good if I could:

1) Add a check to see if there is a network drive that is already mapped to this location. If there is, then there's no point in adding it again. If there isn't, I need to find a drive letter that is not used so that I can add this drive under that alias.
2) This isn't mandatory as I think everyone will want to keep this drive mapped anyhow, but in case they would prefer to have it removed, I'd like to be able to remove the mapping for the newly created drive.

Mmm, not seen that before...
Is that the same error you were getting before, when trying to access the files using the UNC path?

Is this a web or forms app?
Also, what happens if you use your program to set up the share, then browse it using normal Windows Explorer?

When I was using the UNC paths, I was getting a FileIO error but not the "Message="The system detected a possible attempt to compromise security. Please ensure that you can contact the server that authenticated you." " message.


Just tested accessing the drive via Windows EXplorer and it works fine.
Mmm, not sure then.
That code *should* work, I've used similar code myself before...
There must be some other problem here.


Just to clarify, the code is working, save for the aforementioned changes I made. Also, I am using visual studio .net 2005. I just wanted to make that clear. Let me try adding a reference to Visual Basic to see if that makes a difference.
That won't make any difference - if it was missing a reference it wouldn't even run.

So it's all fine, apart from checking to see if the drive has already been mapped, and if so, mapping to a different local name?


Yep. That's it.

This is so odd its almost funny. We've been having some network issues over on our servers here. Ironicly one of the culprits had to do with Bob. One of our guys fixed whatever the network issue was and now the drive mapping code isn't working. I've stepped through it. No errors are being thrown. The code simply just stopped working. Me = confused. Don't suppose anyone would like to wager a guess here?

Okay, I was wrong. I goofed some of the code. I had changed myNetResource.LocalName = "P:" to myNetResource.LocalName = "(test)P:" and then it stopped working. I will assign points now. You have done more than enough to help with this. I would appreciate any direction you might be able to give on checking to see if the drive has already been mapped, and if so, mapping to a different local name. If need be, I can make another question for that topic. Thanks again guys! Cheers!