【发布时间】:2011-02-26 08:45:48
【问题描述】:
我正在尝试移动文件,但出现此错误:
System.UnauthorizedAccessException:访问路径被拒绝..
在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.__Error.WinIOError()
在 System.IO.FileInfo.MoveTo(String destFileName)
要移动文件,我有以下代码:
public void MssFile_Move (string ssPath, string ssDestinationDirectoryPath, string ssDomain, string ssUsername, string ssPassword, out string ssError_message) {
IntPtr admin_token = IntPtr.Zero;
ssError_message = "";
try
{
DoImpersonateLoggedOnUser( ssDomain
, ssUsername
, ssPassword
, out ssError_message
, out admin_token);
FileInfo fi = new FileInfo(ssPath);
//Destination Directory does not exist ?
if ( !Directory.Exists(Path.GetDirectoryName(ssDestinationDirectoryPath)))
Directory.CreateDirectory(Path.GetDirectoryName(
ssDestinationDirectoryPath));
fi.MoveTo (ssDestinationDirectoryPath);
DoRevertToSelf(ssDomain);
}
catch (System.Exception se)
{
int ret = Marshal.GetLastWin32Error();
ssError_message += "Win32Error: " + ret + "\n";
ssError_message += se.ToString();
}
finally
{
if (admin_token != IntPtr.Zero)
CloseHandle(admin_token);
}
}
冒充我有:
[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string lpszDomain,
string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);
[DllImport("advapi32.DLL")]
public static extern bool ImpersonateLoggedOnUser(IntPtr hToken); //handle to token for logged-on user
public void DoImpersonateLoggedOnUser ( string ssDomain
, string ssUsername
, string ssPassword
, out string ssError_message
, out IntPtr admin_token)
{
IntPtr phToken = IntPtr.Zero;
admin_token = IntPtr.Zero;
ssError_message = "";
if (ssDomain != "")
{
if (LogonUser(ssUsername, ssDomain, ssPassword, 9, 0, out phToken) != 0)
{
ImpersonateLoggedOnUser(phToken);
}
else
{
int nErrorCode = Marshal.GetLastWin32Error();
ssError_message = "Operation Failed, error: " + nErrorCode;
}
admin_token = phToken;
}
}
如果我将文件夹 /file 设置为每个人的权限,它可以工作,但我不希望这样。我做错了什么?
【问题讨论】:
-
您是在 Windows 7 还是 Vista 上运行它?还是在 XP 上?
-
服务器运行的是windows 2003
-
你的程序是在网络服务器上运行还是在本地运行?
-
您究竟从哪里得到异常?您确定被模拟的用户对文件和目标路径具有权限吗?
-
文件和文件夹不是只读的,我是在网络服务器上运行的
标签: c# permissions