你能解释为什么DirectoryInfo

你能解释为什么DirectoryInfo

本文介绍了你能解释为什么DirectoryInfo.GetFiles产生这种IOException异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有产生连接到唯一的Windows 2003 Server的网络上时,下面的错误Novell网络上运行的WinForms客户端 - 服务器应用程序:

I have a WinForms client-server app running on a Novell network that produces the following error when connecting to the lone Windows 2003 Server on the network:

TYPE: System.IO.IOException
MSG: Logon failure: unknown user name or bad password.

SOURCE: mscorlib
SITE: WinIOError

  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.DirectoryInfo.GetFiles(String searchPattern,
    SearchOption searchOption)
  at System.IO.DirectoryInfo.GetFiles(String searchPattern)
  at Ceoimage.Basecamp.DocumentServers.ClientAccessServer.SendQueuedFiles(
    Int32 queueId, Int32 userId, IDocQueueFile[] queueFiles)
  at Ceoimage.Basecamp.ScanDocuments.DataModule.CommitDocumentToQueue(
    QueuedDocumentModelWithCollections doc, IDocQueueFile[] files)

客户的网络管理员管理通过手动同步工作站用户名和密码与服务器上的本地用户的Windows服务器连接。有关错误的奇怪的是,用户可以前后都错误后写入服务器,都没有明确的登录。

The customer's network admin manages the Windows Server connection by manually synchronizing the workstation username and password with a local user on the server. The odd thing about the error is that the user can write to the server both before and after the error, all without explicitly logging on.

你能解释为什么发生错误,并提供一个解决方案?

Can you explain why the error occurs and offer a solution?

推荐答案

试图访问Windows服务器的文件系统在不同的领域时,我有同样的问题。的问题是,用户帐户,该程序下运行没有访问到远程服务器。 Windows不会在幕后额外的工作,使您使用Windows资源管理器,因为它猜中您远程的凭据将匹配您的本地凭据看起来浑然一体。

I have this same problem when trying to access the file system of a windows server in a different domain. The problem is that the user account that the program is running under does not have access to the remote server. Windows does extra work behind the scenes to make it look seamless when you use Windows Explorer because it guesses that your remote credentials will match your local credentials.

如果您在本地驱动器映射到远程服务器,然后使用你的code本地映射的驱动器,你不应该有问题。如果您无法映射驱动器,但你可以在c凭据硬$ C $用于远程服务器,那么你可以使用这个code:

If you map a drive locally to the remote server, then use the locally mapped drive in your code, you shouldn't have the problem. If you can't map a drive, but you can hard code the credentials to use for the remote server, then you can use this code:

using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace Company.Security
{
    public class ImpersonateUser : IDisposable
    {
    	[DllImport("advapi32.dll", SetLastError=true)]
    	private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

    	[DllImport( "kernel32", SetLastError = true )]
    	private static extern bool CloseHandle(IntPtr hObject);

    	private IntPtr userHandle = IntPtr.Zero;
    	private WindowsImpersonationContext impersonationContext;

    	public ImpersonateUser( string user, string domain, string password )
    	{
    		if ( ! string.IsNullOrEmpty( user ) )
    		{
    			// Call LogonUser to get a token for the user
    			bool loggedOn = LogonUser( user, domain, password,
    				9 /*(int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS*/,
    				3 /*(int)LogonProvider.LOGON32_PROVIDER_WINNT50*/,
    				out userHandle );
    			if ( !loggedOn )
    				throw new Win32Exception( Marshal.GetLastWin32Error() );

    			// Begin impersonating the user
    			impersonationContext = WindowsIdentity.Impersonate( userHandle );
    		}
    	}

    	public void Dispose()
    	{
    		if ( userHandle != IntPtr.Zero )
    			CloseHandle( userHandle );
    		if ( impersonationContext != null )
    			impersonationContext.Undo();
    	}
    }
}

然后你就可以这样做访问远程服务器:

Then you can access the remote server by doing this:

using ( new ImpersonateUser( "UserID", "Domain", "Password" ) )
{
    // Any IO code within this block will be able to access the remote server.
}

这篇关于你能解释为什么DirectoryInfo.GetFiles产生这种IOException异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 21:41