Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to gain access to a network share?

How to gain access to a network share?

Scheduled Pinned Locked Moved C#
sysadminquestioncsharptutorial
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Throckmorton
    wrote on last edited by
    #1

    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

    V H 2 Replies Last reply
    0
    • T Throckmorton

      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

      V Offline
      V Offline
      Verdant123
      wrote on last edited by
      #2

      just use it like a normal file resource

      1 Reply Last reply
      0
      • T Throckmorton

        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

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        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 the WindowsIdentity.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

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups