School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
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!



Accessing files with network credentials?

Accessing files with network credentials? Rate Topic: -----

#1 geewhiz  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 34
  • Joined: 20-August 07


Dream Kudos: 0

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.
Was This Post Helpful? 0
  • +
  • -


#2 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon
  • View blog
  • 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


Was This Post Helpful? 0
  • +
  • -

#3 geewhiz  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 34
  • Joined: 20-August 07


Dream Kudos: 0

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!
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon
  • View blog
  • 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

View Postgeewhiz, 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.
Was This Post Helpful? 0
  • +
  • -

#5 geewhiz  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 34
  • Joined: 20-August 07


Dream Kudos: 0

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!
Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • apt-get install DIC.bin
  • Icon
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#7 geewhiz  Icon User is offline

  • New D.I.C Head
  • Pip
  • Group: Members
  • Posts: 34
  • Joined: 20-August 07


Dream Kudos: 0

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?
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

4 User(s) are reading this topic
0 members, 4 guests, 0 anonymous users



Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month