What's Here?
- Members: 340,161
- Replies: 920,578
- Topics: 154,962
- Snippets: 4,855
- Tutorials: 1,257
- Total Online: 4,028
- Members: 127
- Guests: 3,901
|
Welcome to Dream.In.Code |
|
|
Become an Expert!
Join 340,161 Programmers for FREE! Get instant access to thousands  of experts, tutorials, code snippets, and more! There are 4,028 people online right now. Registration is fast and FREE... Join Now!
Chat LIVE With a Expert
|
Accessing files with network credentials?
Accessing files with network credentials?
Rate Topic:
   
Posted 01 October 2007 - 11:05 AM
Hello everyone.
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.
NewsCrawlNew.SaveFile "\\NetworkPC1\textfiles\TextFile1.txt", rtfText
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.

- apt-get install DIC.bin
-
-
Group:
Admins
-
Posts:
16,214
-
Joined:
26-July 07
Dream Kudos: 12400
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery
Posted 01 October 2007 - 11:37 AM
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
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
lon = WNetAddConnection2(networkResource, "MyPassword", "MyUserName", 0)
' 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
lon = WNetCancelConnection2(UNC, 0, True)
End Sub
Posted 01 October 2007 - 12:35 PM
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!

- apt-get install DIC.bin
-
-
Group:
Admins
-
Posts:
16,214
-
Joined:
26-July 07
Dream Kudos: 12400
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery
Posted 01 October 2007 - 12:44 PM
geewhiz, on 1 Oct, 2007 - 01:35 PM, said:
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.
Posted 02 October 2007 - 10:45 AM
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!

- apt-get install DIC.bin
-
-
Group:
Admins
-
Posts:
16,214
-
Joined:
26-July 07
Dream Kudos: 12400
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery
Posted 02 October 2007 - 10:57 AM
I originally used it for File.Copy, I took that part out and modified it for your saving of a file.
Posted 02 November 2007 - 01:25 PM
Hmm, I can't seem to get it to work right. Here is what I'm using.
Private Sub WriteToNetworkDrive()
Dim networkResource As NETRESOURCE
Dim lon As Long
Dim FileNameLocation As String
FileNameLocation = Dir1.Path + "\" + File1.FileName
With networkResource
.dwType = RESOURCETYPE_DISK
.lpLocalName = ""
.lpRemoteName = "\\Graphics\Graphics\Test"
.lpProvider = ""
End With
lon = WNetAddConnection2(networkResource, "geewhiz", "geewhiz", 0)
' 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?
4 User(s) are reading this topic
0 members, 4 guests, 0 anonymous users
|
Be Social
Programming
Web Development
Reference Sheets
Bye Bye Ads
Monthly Drawing
Top Contributors
Top 10 Kudos This Month
|