How to gain access to a network share?
-
Hello, My program needs to access a network share and either copy files from the share or else just execute a program. First I have the client check if it can see the share: if(Directory.Exists("\\\\server\\share") { // do whatever } else { // couldn't see share // provided the share is online // I assume the client does not have proper permission // basically I want to "net use" here. } Is there a function that does this? Or do i need to start a "net use" process? What is the preferred method of gaining access to a remote share through C#? Thanks, Ian
-
Hello, My program needs to access a network share and either copy files from the share or else just execute a program. First I have the client check if it can see the share: if(Directory.Exists("\\\\server\\share") { // do whatever } else { // couldn't see share // provided the share is online // I assume the client does not have proper permission // basically I want to "net use" here. } Is there a function that does this? Or do i need to start a "net use" process? What is the preferred method of gaining access to a remote share through C#? Thanks, Ian
just use it like a normal file resource
-
Hello, My program needs to access a network share and either copy files from the share or else just execute a program. First I have the client check if it can see the share: if(Directory.Exists("\\\\server\\share") { // do whatever } else { // couldn't see share // provided the share is online // I assume the client does not have proper permission // basically I want to "net use" here. } Is there a function that does this? Or do i need to start a "net use" process? What is the preferred method of gaining access to a remote share through C#? Thanks, Ian
As the first post mentioned - just access the file using a UNC path. If you get an
UnauthorizedException
, this means you don't have permissions. The correct way is to prompt the user for credentials and impersonate them using those credentials. For an example of impersonation using .NET, see the documentation for theWindowsIdentity.Impersonate
method in the .NET Framework SDK. If you want to go to the trouble of forming a net use statement (using /user and prompting for the password), you could do that but you'll have to make sure you change all UNC references so that they use the new mapped drive letter.Microsoft MVP, Visual C# My Articles