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
}