Solved

How can I access a network share using userid / password in code

Posted on 2014-09-15
8
954 Views
Last Modified: 2014-10-01
I tried some open source that I found

  using (NetworkShareAccesser.Access("remoteComputername", "domain", "userid", "passoword"))
                        {
                            if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); }
                        }

But this gives me an error - network name not found
0
Comment
Question by:countrymeister
  • 4
  • 4
8 Comments
 
LVL 26

Expert Comment

by:pony10us
ID: 40323440
Did you also get the code for "NetworkShareAccesser"?
0
 
LVL 1

Author Comment

by:countrymeister
ID: 40323514
Yes, of course, How else would my code compile and I would be able to debug and get that error
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40323607
I would verify you have all the correct variables set in the "NetworkShareAccesser" code.  The error you are getting indicates that it can't find the remote computer.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:countrymeister
ID: 40323707
I will try and do that, but do you have a solution to access a network share using a userid password.
0
 
LVL 26

Expert Comment

by:pony10us
ID: 40324165
I usually do it with Net Use:

net use \\server\share /user:name password

If you want to access the share afterwards:

start \\server\share

So you could save this as a batch file:

@echo off
net use \\server\share /user:name password
start \\server\share

Remember to use quotes around the name and/or password if they contain blanks.
0
 
LVL 1

Author Comment

by:countrymeister
ID: 40333358
Pony itried Net use , but it does not work, because I have subdirectories and cannot get to the sub directories
0
 
LVL 26

Accepted Solution

by:
pony10us earned 500 total points
ID: 40333471
Is this just a snippet of code for a larger project?  

Using what I provided would simply open the share in a window. This should allow you to access anything in the share.

If you are attempting to use this as a snippet of a larger piece of code to run unattended then you are correct that as written it will not work.

What you would need is to assign it a drive letter and access it using that letter.

For example:

net use z: \\server\share /user:name password
copy z:\subfolder\file.ext c:\folder
net use z: /delete

Open in new window


Would map the share to z: using the credentials provided, copy a file from the subfolder in the share to a folder on the C: drive and finally disconnect the Z: drive.

Is this closer to what you are wanting?
0
 
LVL 1

Author Closing Comment

by:countrymeister
ID: 40354705
I had to change some of the Network Accessor code and got it to work. Cannot use net use in line project
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question