2

I want to add the path of map network drive into C# code and be linked when the user click on it it automatically open the drive path my code is like

IWshNetwork_Class network = new IWshNetwork_Class();
network.MapNetworkDrive("Z:", @"\\ms\temp");
string message1 = @"[1] Go to C:\" + Environment.NewLine + "[2] Replace Folder \"myTeX\" with myTeX exist in" + network + Environment.NewLine + "[3] Open Start menu-->All Programs-->MiKTeX 2.8-->Maintenance-->Settings-->Click on \"Refresh FNDB\" button then wait the process and try again, Good Luck ";
richTextBox2.Text = message1;

but this code was not working correctly as the name appeared in the message1 was like

[1] Go to C:\
[2] Replace Folder "myTeX" with myTeX exist inSystem.__ComObject
[3] Open Start menu-->All Programs-->MiKTeX 2.8-->Maintenance-->Settings-->Click on "Refresh FNDB" button then wait the process and try again, Good Luck 

so System._ComObject should be \\ms\temp and linked as when the user click on it go to the drive \\ms\temp

Filburt
  • 17,626
  • 12
  • 64
  • 115
MohamedSayed
  • 147
  • 1
  • 4
  • 14
  • 1
    Well you're adding the `network` instance to your string. This object doesn't contain the drive letter you mapped in the second line. You'll need to retrieve the collection of drives and access the mapped drive from there. – Filburt Apr 14 '13 at 08:58
  • @Filburt thank you for the help but really i don know how to do that am new to C# – MohamedSayed Apr 14 '13 at 09:10
  • 1
    You are using a fairly primitive scripting object. You'll need to use its [EnumNetworkDrives method](http://msdn.microsoft.com/en-us/library/t9zt39at%28v=vs.84%29.aspx) to retrieve the mapping. – Hans Passant Apr 14 '13 at 14:35

1 Answers1

0

Instead of scripting object you gain much more control if you use the native WINAPI calls to achieve what you want. These kind of solutions give you much better control and with the help of sites like http://pinvoke.net you can always find a c# wrapper or create one easily. And if a solution exists within one of .Net Framwork classes you often find a link to that.

You basically need two functions from the WINAPI:

This solution consist of the method to map and get the info and some helpers

Method for mapping the drive and showing info

var mapResult = WNetAddConnection2(
    new NETRESOURCE {
    Scope = ResourceScope.Connected,
    ResourceType = ResourceType.Disk,
    DisplayType = ResourceDisplaytype.Generic,
    Usage = 0,
    LocalName = @"Z:",
    RemoteName = @"\\ms\temp",
    Comment = "from csharp",
    Provider = null
    }
    , null
    , null
    , 0);
if (mapResult!=0)
{
    throw new Exception("AddConnection failed");
    // >0? check  http://msdn.microsoft.com/en-us/library/windows/desktop/ms681383(v=vs.85).aspx
}

// get the remote connection path
int remoteBufLen = 0; // start with a 0 length buffer
StringBuilder remotePath = null;
var connRes = ERROR_MORE_DATA;  
// call twice if the buffer is too small
for (int t=0 ; t<2 && connRes == ERROR_MORE_DATA; t++)
{
    remotePath = new StringBuilder(remoteBufLen);
    // and error is returned 
    // and remoteBufLen holds the required size
    connRes = WNetGetConnection(@"Z:", remotePath, ref remoteBufLen);
}
if (connRes != 0)
{
   throw new Exception("getconnetion failed");
}

string message1 = String.Format(@"[1] Go to C:\{0}[2] Replace Folder ""myTeX"" with myTeX exist in {1}{0}[3] Open Start menu-->All Programs-->MiKTeX 2.8-->Maintenance-->Settings-->Click on ""Refresh FNDB"" button then wait the process and try again, Good Luck "
, Environment.NewLine, remotePath);

richTextBox2.Text = message1;

Helpers

I used http://pinvoke.net to find the c# declarations for the WINAPI calls

// http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx
const int ERROR_MORE_DATA =0xEA;

// http://www.pinvoke.net/default.aspx/mpr.WNetGetConnection
[DllImport("mpr.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern int WNetGetConnection(
    [MarshalAs(UnmanagedType.LPTStr)] string localName, 
    [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, 
    ref int length);

//http://www.pinvoke.net/default.aspx/mpr.VVNetAddConnection2
[DllImport("mpr.dll")]    
public static extern int WNetAddConnection2(NETRESOURCE netResource,
    string password, string username, uint flags);


[StructLayout(LayoutKind.Sequential)]
public class NETRESOURCE
{
     public ResourceScope Scope;
     public ResourceType ResourceType;
     public ResourceDisplaytype DisplayType;
     public int Usage;
     public string LocalName;
     public string RemoteName;
     public string Comment;
     public string Provider;
}

public enum ResourceScope : int
{
     Connected = 1,
     GlobalNetwork,
     Remembered,
     Recent,
     Context
}

public enum ResourceType : int
{
    Any = 0,
     Disk = 1,
     Print = 2,
     Reserved = 8,
}

public enum ResourceDisplaytype : int
{
     Generic = 0x0,
     Domain = 0x01,
     Server = 0x02,
     Share = 0x03,
     File = 0x04,
     Group = 0x05,
     Network = 0x06,
     Root = 0x07,
     Shareadmin = 0x08,
     Directory = 0x09,
     Tree = 0x0a,
     Ndscontainer = 0x0b
}
Community
  • 1
  • 1
rene
  • 41,474
  • 78
  • 114
  • 152