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.
you need a share - but there is a hidden share by default; try \\bob\c$
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.
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:
ASKER
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.S equential) > _
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.S tructLayou t(LayoutKi nd.Sequent ial)> _
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?
Also, when I add
<StructLayout(LayoutKind.S
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.S
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?
ASKER
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.InteropServ ices
Think you also need to import System.Runtime.InteropServ
ASKER
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.InteropSer vices.Stru ctLayout(R untime.Int eropServic es.LayoutK ind.Sequen tial)> _
I also moved:
Dim myNetResource As New NETRESOURCE
myNetResource.dwScope = 2 'RESOURCE_GLOBALNET
myNetResource.dwType = 1 'RESOURCETYPE_DISK
myNetResource.dwDisplayTyp e = 3 'RESOURCEDISPLAYTYPE_SHARE
myNetResource.dwUsage = 1 'RESOURCEUSAGE_CONNECTABLE
myNetResource.LocalName = "P:"
myNetResource.RemoteName = "\\bob\c$"
myNetResource.Provider = Nothing
Dim ret As Integer = WNetAddConnection2(myNetRe source, 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.WinIOErr or(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.Intern alGetFileD irectoryNa mes(String path, String userPathOriginal, String searchPattern, Boolean includeFiles, Boolean includeDirs, SearchOption searchOption)
at System.IO.Directory.GetFil es(String path, String searchPattern, SearchOption searchOption)
at System.IO.Directory.GetFil es(String path)
at FileCopier.FrmFileCopyMain .Button1_C lick(Objec t sender, EventArgs e) in C:\Personal App Development\FileCopier\Fil eCopier\Fi leCopier\F rmFileCopy Main.vb:li ne 62
at System.Windows.Forms.Contr ol.OnClick (EventArgs e)
at System.Windows.Forms.Butto n.OnClick( EventArgs e)
at System.Windows.Forms.Butto n.OnMouseU p(MouseEve ntArgs mevent)
at System.Windows.Forms.Contr ol.WmMouse Up(Message & m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Contr ol.WndProc (Message& m)
at System.Windows.Forms.Butto nBase.WndP roc(Messag e& m)
at System.Windows.Forms.Butto n.WndProc( Message& m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.OnMessa ge(Message & m)
at System.Windows.Forms.Contr ol.Control NativeWind ow.WndProc (Message& m)
at System.Windows.Forms.Nativ eWindow.De buggableCa llback(Int Ptr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Unsaf eNativeMet hods.Dispa tchMessage W(MSG& msg)
at System.Windows.Forms.Appli cation.Com ponentMana ger.System .Windows.F orms.Unsaf eNativeMet hods.IMsoC omponentMa nager.FPus hMessageLo op(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoopInner (Int32 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Thr eadContext .RunMessag eLoop(Int3 2 reason, ApplicationContext context)
at System.Windows.Forms.Appli cation.Run (Applicati onContext context)
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.OnRun( )
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.DoAppl icationMod el()
at Microsoft.VisualBasic.Appl icationSer vices.Wind owsFormsAp plicationB ase.Run(St ring[] commandLine)
at FileCopier.My.MyApplicatio n.Main(Str ing[] Args) in 17d14f5c-a337-4978-8281-53 493378c107 1.vb:line 81
at System.AppDomain.nExecuteA ssembly(As sembly assembly, String[] args)
at System.AppDomain.ExecuteAs sembly(Str ing assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.Hos tingProces s.HostProc .RunUsersA ssembly()
at System.Threading.ThreadHel per.Thread Start_Cont ext(Object state)
at System.Threading.Execution Context.Ru n(Executio nContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHel per.Thread Start()
<System.Runtime.InteropSer
I also moved:
Dim myNetResource As New NETRESOURCE
myNetResource.dwScope = 2 'RESOURCE_GLOBALNET
myNetResource.dwType = 1 'RESOURCETYPE_DISK
myNetResource.dwDisplayTyp
myNetResource.dwUsage = 1 'RESOURCEUSAGE_CONNECTABLE
myNetResource.LocalName = "P:"
myNetResource.RemoteName = "\\bob\c$"
myNetResource.Provider = Nothing
Dim ret As Integer = WNetAddConnection2(myNetRe
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.WinIOErr
at System.IO.Directory.Intern
at System.IO.Directory.GetFil
at System.IO.Directory.GetFil
at FileCopier.FrmFileCopyMain
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Butto
at System.Windows.Forms.Butto
at System.Windows.Forms.Contr
at System.Windows.Forms.Contr
at System.Windows.Forms.Nativ
at System.Windows.Forms.Unsaf
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at System.Windows.Forms.Appli
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at Microsoft.VisualBasic.Appl
at FileCopier.My.MyApplicatio
at System.AppDomain.nExecuteA
at System.AppDomain.ExecuteAs
at Microsoft.VisualStudio.Hos
at System.Threading.ThreadHel
at System.Threading.Execution
at System.Threading.ThreadHel
ASKER
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?
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?
ASKER
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.
That code *should* work, I've used similar code myself before...
There must be some other problem here.
ASKER
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?
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?
ASKER
Yep. That's it.
ASKER
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?
ASKER
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!