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. Mapping a Drive at runtime

Mapping a Drive at runtime

Scheduled Pinned Locked Moved C#
sysadmincsharpcomhelp
3 Posts 2 Posters 1 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.
  • E Offline
    E Offline
    exhaulted
    wrote on last edited by
    #1

    Hi all, i'm trying to access files on a networked dirve using the UNC name "\\myServer\d$". Unfortunately if i dont have a drive mapped when i try to find files on the server i get an IOException saying that the directory doesn't exist. I found some useful comments at the bottom of an article talking about using WHScript's MapNetworkDrive method. This sounds good but i can't get it to work. Here's my code which is only slightly different from the comment on the article.

    public void InvokeComMember(string member, params object[] param)
    {
    Type type;
    Object o;
    try
    {
    type = Type.GetTypeFromProgID("WScript.Network");
    o = Activator.CreateInstance(type);
    type.InvokeMember(member, BindingFlags.InvokeMethod, null, o, param);
    }
    catch{}
    finally
    {
    type = null;
    o = null;
    }
    }

    The paramaters being passed in are...

    InvokeComMember("MapNetworkDrive", @"\\" + serverName + @"\d$", "", false, userName, password);

    serverName, userName and password are all strings that i've double checked are correct. The exception i get is... System.Exception: Invoke Com Error ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: The network name cannot be found. When i type the same \\"serverName"\d$ into the run window i get a connection fine so i know the serverName variable is correct. Anyone know either... 1. What i'm doing wrong with this or 2. Another way of getting at the files on the UNC'd server. Cheers Kev -- modified at 8:49 Thursday 12th January, 2006

    K 1 Reply Last reply
    0
    • E exhaulted

      Hi all, i'm trying to access files on a networked dirve using the UNC name "\\myServer\d$". Unfortunately if i dont have a drive mapped when i try to find files on the server i get an IOException saying that the directory doesn't exist. I found some useful comments at the bottom of an article talking about using WHScript's MapNetworkDrive method. This sounds good but i can't get it to work. Here's my code which is only slightly different from the comment on the article.

      public void InvokeComMember(string member, params object[] param)
      {
      Type type;
      Object o;
      try
      {
      type = Type.GetTypeFromProgID("WScript.Network");
      o = Activator.CreateInstance(type);
      type.InvokeMember(member, BindingFlags.InvokeMethod, null, o, param);
      }
      catch{}
      finally
      {
      type = null;
      o = null;
      }
      }

      The paramaters being passed in are...

      InvokeComMember("MapNetworkDrive", @"\\" + serverName + @"\d$", "", false, userName, password);

      serverName, userName and password are all strings that i've double checked are correct. The exception i get is... System.Exception: Invoke Com Error ---> System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.IO.FileNotFoundException: The network name cannot be found. When i type the same \\"serverName"\d$ into the run window i get a connection fine so i know the serverName variable is correct. Anyone know either... 1. What i'm doing wrong with this or 2. Another way of getting at the files on the UNC'd server. Cheers Kev -- modified at 8:49 Thursday 12th January, 2006

      K Offline
      K Offline
      Koushik Biswas
      wrote on last edited by
      #2

      http://www.codeproject.com/csharp/mapnetdrive.asp[^] Provides a class using which you can simply call MapDrive() or UnMapDrive(). Koushik Biswas

      E 1 Reply Last reply
      0
      • K Koushik Biswas

        http://www.codeproject.com/csharp/mapnetdrive.asp[^] Provides a class using which you can simply call MapDrive() or UnMapDrive(). Koushik Biswas

        E Offline
        E Offline
        exhaulted
        wrote on last edited by
        #3

        Yeah i found that. The example i gave was from the comments section of that article. The class used in the article requires you to provide a drive letter. The ideas used in the comments section at the bottom of the article givesa a way of doing it without using a drive letter but i can't get it to work. All i want to do is access some files using a unc name and the relevant login cridentials. Cheers Kev

        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