Mapping a Drive at runtime
-
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
andpassword
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 -
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
andpassword
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, 2006http://www.codeproject.com/csharp/mapnetdrive.asp[^] Provides a class using which you can simply call
MapDrive()
orUnMapDrive()
. Koushik Biswas -
http://www.codeproject.com/csharp/mapnetdrive.asp[^] Provides a class using which you can simply call
MapDrive()
orUnMapDrive()
. Koushik BiswasYeah 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