We have a method that accesses a network share. This method works fine when called directly, but we get a System.IO.IOException when it is called via reflecton. It appear that the user context is not available to the reflected code (see stack trace below). Is there a way to prevent this?
System.Reflection.TargetInvocationException: Exception has been thrown by
the target of an invocation. ---> System.IO.IOException: Logon failure:
unknown user name or bad password.
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.Directory.InternalGetFileDirectoryNames(String path,
String userPathOriginal, String searchPattern, Boolean includeFiles,
Boolean includeDirs, SearchOption searchOption)
at System.IO.Directory.GetDirectories(String path, String searchPattern,
SearchOption searchOption)
this works
Library.Class obj =new Library.Class();
obj.Execute(serverPath);
this does not work
Assembly assembly = Assembly.LoadFile(@"pathTo\Library.dll");
Type type = assembly.GetType("Library.Class");
MethodInfo executeMethod = type.GetMethod("Execute");
object classInstance = Activator.CreateInstance(type, null);
object[] parameterArray = new object[] { serverPath};
executeMethod.Invoke(classInstance, parameterArray);
Where Library.Class.execute is defined as
public void Execute(string serverPath){
string[] directories = Directory.GetDirectories(serverPath,
"1.*", SearchOption.TopDirectoryOnly);
foreach (var directory in directories) {
Console.WriteLine(directory);
}
}
and serverPath is a network share that required the user enter credentials.
—–Update 1——-
This appears to be somewhat environmental–I have at least one test machine where everything works. I’ll be doing some more testing to determine what differences matter.
This appears to have been some sort of fluke environmental issue. We have not been able to reproduce the problem since the test machine was restarted.