Welcome to Dream.In.Code
Getting VB Help is Easy!

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!




Accessing files with network credentials?

 
Reply to this topicStart new topic

Accessing files with network credentials?

geewhiz
1 Oct, 2007 - 11:05 AM
Post #1

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
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.

CODE
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.
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing Files With Network Credentials?
1 Oct, 2007 - 11:37 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
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

  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

User is online!Profile CardPM
+Quote Post

geewhiz
RE: Accessing Files With Network Credentials?
1 Oct, 2007 - 12:35 PM
Post #3

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
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!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing Files With Network Credentials?
1 Oct, 2007 - 12:44 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
QUOTE(geewhiz @ 1 Oct, 2007 - 01: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!


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.

User is online!Profile CardPM
+Quote Post

geewhiz
RE: Accessing Files With Network Credentials?
2 Oct, 2007 - 10:45 AM
Post #5

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
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!
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Accessing Files With Network Credentials?
2 Oct, 2007 - 10:57 AM
Post #6

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,983



Thanked: 125 times
Dream Kudos: 8600
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
I originally used it for File.Copy, I took that part out and modified it for your saving of a file.
User is online!Profile CardPM
+Quote Post

geewhiz
RE: Accessing Files With Network Credentials?
2 Nov, 2007 - 01:25 PM
Post #7

New D.I.C Head
*

Joined: 20 Aug, 2007
Posts: 31


My Contributions
Hmm, I can't seem to get it to work right. Here is what I'm using.

CODE
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?
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/1/08 08:24PM

Live VB Help!

VB Tutorials

Reference Sheets

VB Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month