Join 136,094 VB Programmers for FREE! Get instant access to thousands of VB experts, tutorials, code snippets, and more! There are 1,593 people online right now. Registration is fast and FREE... Join Now!
Does anyone know access a file through VB with a username and password attached to the command line? Here is some code to better illustrate what I'm talking about.
For example, if I want to write one of my richtextboxes into a text file that is located across the network.
Can I put a username and password along with that command? Sometimes the remote PC asks for the username and password but I would like to hardcode that into my command.
In VB6 here is a solution that will connect you to the UNC Share (with credentials you provide), save your file, then disconnect from the share. This requires Win32 API's
CODE
Private Declare Function WNetAddConnection2 Lib "mpr.dll" Alias "WNetAddConnection2A" (lpNetResource As NETRESOURCE, ByVal lpPassword As String, ByVal lpUserName As String, ByVal dwFlags As Long) As Long
Private Declare Function WNetCancelConnection2 Lib "mpr.dll" Alias "WNetCancelConnection2A" (ByVal lpName As String, ByVal dwFlags As Long, ByVal fForce As Long) As Long
Private Const RESOURCETYPE_DISK = &H1
Private Type NETRESOURCE dwScope As Long dwType As Long dwDisplayType As Long dwUsage As Long lpLocalName As String lpRemoteName As String lpComment As String lpProvider As String End Type
Private Sub WriteToNetworkDrive() Dim networkResource As NETRESOURCE, Dim lon As Long
With networkResource .dwType = RESOURCETYPE_DISK .lpLocalName = "" .lpRemoteName = "\\MyServer\MyShare\MyFolder" .lpProvider = "" End With
' access the unc here ' save your file NewsCrawlNew.SaveFile "\\NetworkPC1\textfiles\TextFile1.txt", rtfText
'IGNORE THE NEXT 3 LINES IF YOU 'DONT NEED TO WRITE TO A FILE ALREADY 'ON THE NETWORK ' write file Dim ff As Integer ff = FreeFile Open "\\MyServer\MyShare\MyFolder\Somefile.txt" For Output As #ff Print #ff, "Write Text to file on share" Close #ff
Wow, I had no idea there would be that much to it. I would have never figured that out on my own. Thanks for the help!
Well "technically" one wouldn't have to go that in depth for your quandary, but I'm of the opinion of you're going to do something you might as well do it right. There are other ways of accomplishing the same task, but this one cleans up after you and closes your connections.
I agree 100%. It's really more than I could have hoped for. I probably won't get a chance to really start messing around with it until next week, but I'm assuming it will work fine with the 'FileCopy' so that I can also pull files from that remote location and place them into RichTextBoxes. Thanks again!
' access the unc here ' save your file FileCopy FileNameLocation, "C:\Temp\Snipe\Snipe1.via" FileCopy "C:\Temp\Snipe\Snipe1.via", "\\Graphics\Graphics\Test\Snipe1.via"
'IGNORE THE NEXT 3 LINES IF YOU 'DONT NEED TO WRITE TO A FILE ALREADY 'ON THE NETWORK ' write file 'Dim ff As Integer 'ff = FreeFile 'Open "\\Graphics\Graphics\Test\Snipe1.via" For Output As #ff 'Print #ff, "Write Text to file on share" 'Close #ff
lon = WNetCancelConnection2(UNC, 0, True) End Sub
The double FileCopy you see was just a test to make sure my FileNameLocation was generated correctly. It is working fine up to the transfer to the remote PC. I get:
QUOTE
Microsoft Visual Basic Run-time error '75': Path/File Access Error
geewhiz/geewhiz is an account on my Graphics PC with rights to the shared folder. On my remote PC, that I'm sending from, I am logged in as a different user. Did I goof up on the server names?