I try to move the file, but gives this error:
System.UnauthorizedAccessException: Path access denied.
in System.IO .__ Error.WinIOError (Int32 errorCode, String maybeFullPath)
in System.IO .__ Error.WinIOError ()
in System.IO.FileInfo.MoveTo (String destFileName)
To move the file, I have this code:
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);
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);
}
}
To personalize, I:
[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);
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;
}
}
If I set the folder / file to permissions for everyone, this works, but I do not want this. What am I doing wrong?