C# 使用Process.Start()从本地计算机到文件共享的RoboCopy抛出拒绝访问错误
我有网络文件共享。网络共享有一个登录帐户,用户在将文件复制到目录时将在该帐户中进行身份验证。首先,用户将使用c#中的C# 使用Process.Start()从本地计算机到文件共享的RoboCopy抛出拒绝访问错误,c#,C#,我有网络文件共享。网络共享有一个登录帐户,用户在将文件复制到目录时将在该帐户中进行身份验证。首先,用户将使用c#中的WindowsImpersonationContext在文件共享中创建目录。创建目录后,用户将文件复制到网络共享中的目录 用户可以在网络共享中创建目录,但当用户通过将文件从本地计算机复制到网络文件共享来启动robocopy时,系统会显示拒绝访问。在文件共享和robocopy中创建目录时,我使用相同的用户帐户模拟to fileshare帐户 请帮帮我。请查找下面的代码。 在将文件复制
WindowsImpersonationContext
在文件共享中创建目录。创建目录后,用户将文件复制到网络共享中的目录
用户可以在网络共享中创建目录,但当用户通过将文件从本地计算机复制到网络文件共享来启动robocopy时,系统会显示拒绝访问
。在文件共享和robocopy中创建目录时,我使用相同的用户帐户模拟to fileshare帐户
请帮帮我。请查找下面的代码。
在将文件复制到网络文件共享目录时,是否可以将robocopy作为其他帐户运行或模拟其他用户帐户
在网络文件共享中创建目录
在Filshare中创建目录时的模拟
机器人技术:
仅在执行robocopy时,当我使用Fileshare登录帐户将文件从本地计算机复制到网络文件时,出现访问被拒绝的错误。
我能够创建目录而没有任何拒绝访问的错误?我也在做类似的事情(但没有使用RoboCopy),对我有效的方法是使用具有所需权限的用户名和密码将源计算机上的驱动器映射到远程计算机。映射驱动器后,您可以复制文件、创建目录等。下面是一些处理映射的代码:
public class DriveSettings
{
private enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
}
private enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
}
private enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010
}
private enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
}
[StructLayout(LayoutKind.Sequential)]
private struct NETRESOURCE
{
public ResourceScope oResourceScope;
public ResourceType oResourceType;
public ResourceDisplayType oDisplayType;
public ResourceUsage oResourceUsage;
public string sLocalName;
public string sRemoteName;
public string sComments;
public string sProvider;
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2
(ref NETRESOURCE oNetworkResource, string sPassword,
string sUserName, int iFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2
(string sLocalName, uint iFlags, int iForce);
public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath, string userName, string password)
{
//Checks if the last character is \ as this causes error on mapping a drive.
if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
{
sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
}
NETRESOURCE oNetworkResource = new NETRESOURCE();
oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
oNetworkResource.sLocalName = sDriveLetter + ":";
oNetworkResource.sRemoteName = sNetworkPath;
//If Drive is already mapped disconnect the current
//mapping before adding the new mapping
if (IsDriveMapped(sDriveLetter))
{
DisconnectNetworkDrive(sDriveLetter, true);
}
WNetAddConnection2(ref oNetworkResource, password, userName, 0);
}
public static int DisconnectNetworkDrive(string sDriveLetter, bool bForceDisconnect)
{
if (bForceDisconnect)
{
return WNetCancelConnection2(sDriveLetter + ":", 0, 1);
}
else
{
return WNetCancelConnection2(sDriveLetter + ":", 0, 0);
}
}
public static bool IsDriveMapped(string sDriveLetter)
{
string[] DriveList = Environment.GetLogicalDrives();
for (int i = 0; i < DriveList.Length; i++)
{
if (sDriveLetter + ":\\" == DriveList[i].ToString())
{
return true;
}
}
return false;
}
}
复制完成后,断开连接:
if (DriveSettings.IsDriveMapped(mapDriveLetter))
{
DriveSettings.DisconnectNetworkDrive(mapDriveLetter, true);
}
用户(运行robocopy的用户)是否有足够的权限访问本地计算机上的文件?我已在本地pc中添加了具有完全权限的用户帐户。您找到答案了吗?
public void RoboCopy(string strSourceFilePath,string DestinationFilePath,string FileName,string UserName,String DomainName,string Password)
{
try
{
System.Security.SecureString password = new System.Security.SecureString();
char[] pass = Password.ToCharArray();
foreach (char c in pass)
password.AppendChar(c);
Process p = new Process();
p.StartInfo = new ProcessStartInfo();
p.StartInfo.FileName = "Robocopy.exe";
p.StartInfo.Arguments = string.Format("\"{0}\" \"{1}\" \"{2}\" \"{3}\" ", strSourceFilePath, DestinationFilePath, FileName,"/R:2" );
p.StartInfo.Domain = DomainName;
p.StartInfo.UserName = UserName;
p.StartInfo.Password = password;
p.StartInfo.UseShellExecute = false; // set this to false so that we can redirect the output
p.StartInfo.RedirectStandardError = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
string result = p.StandardOutput.ReadToEnd();
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();
}
catch (Exception ex)
{
// Setting the Error Description in CommonFunctions Class.
throw ex;
}
}
public class DriveSettings
{
private enum ResourceScope
{
RESOURCE_CONNECTED = 1,
RESOURCE_GLOBALNET,
RESOURCE_REMEMBERED,
RESOURCE_RECENT,
RESOURCE_CONTEXT
}
private enum ResourceType
{
RESOURCETYPE_ANY,
RESOURCETYPE_DISK,
RESOURCETYPE_PRINT,
RESOURCETYPE_RESERVED
}
private enum ResourceUsage
{
RESOURCEUSAGE_CONNECTABLE = 0x00000001,
RESOURCEUSAGE_CONTAINER = 0x00000002,
RESOURCEUSAGE_NOLOCALDEVICE = 0x00000004,
RESOURCEUSAGE_SIBLING = 0x00000008,
RESOURCEUSAGE_ATTACHED = 0x00000010
}
private enum ResourceDisplayType
{
RESOURCEDISPLAYTYPE_GENERIC,
RESOURCEDISPLAYTYPE_DOMAIN,
RESOURCEDISPLAYTYPE_SERVER,
RESOURCEDISPLAYTYPE_SHARE,
RESOURCEDISPLAYTYPE_FILE,
RESOURCEDISPLAYTYPE_GROUP,
RESOURCEDISPLAYTYPE_NETWORK,
RESOURCEDISPLAYTYPE_ROOT,
RESOURCEDISPLAYTYPE_SHAREADMIN,
RESOURCEDISPLAYTYPE_DIRECTORY,
RESOURCEDISPLAYTYPE_TREE,
RESOURCEDISPLAYTYPE_NDSCONTAINER
}
[StructLayout(LayoutKind.Sequential)]
private struct NETRESOURCE
{
public ResourceScope oResourceScope;
public ResourceType oResourceType;
public ResourceDisplayType oDisplayType;
public ResourceUsage oResourceUsage;
public string sLocalName;
public string sRemoteName;
public string sComments;
public string sProvider;
}
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2
(ref NETRESOURCE oNetworkResource, string sPassword,
string sUserName, int iFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2
(string sLocalName, uint iFlags, int iForce);
public static void MapNetworkDrive(string sDriveLetter, string sNetworkPath, string userName, string password)
{
//Checks if the last character is \ as this causes error on mapping a drive.
if (sNetworkPath.Substring(sNetworkPath.Length - 1, 1) == @"\")
{
sNetworkPath = sNetworkPath.Substring(0, sNetworkPath.Length - 1);
}
NETRESOURCE oNetworkResource = new NETRESOURCE();
oNetworkResource.oResourceType = ResourceType.RESOURCETYPE_DISK;
oNetworkResource.sLocalName = sDriveLetter + ":";
oNetworkResource.sRemoteName = sNetworkPath;
//If Drive is already mapped disconnect the current
//mapping before adding the new mapping
if (IsDriveMapped(sDriveLetter))
{
DisconnectNetworkDrive(sDriveLetter, true);
}
WNetAddConnection2(ref oNetworkResource, password, userName, 0);
}
public static int DisconnectNetworkDrive(string sDriveLetter, bool bForceDisconnect)
{
if (bForceDisconnect)
{
return WNetCancelConnection2(sDriveLetter + ":", 0, 1);
}
else
{
return WNetCancelConnection2(sDriveLetter + ":", 0, 0);
}
}
public static bool IsDriveMapped(string sDriveLetter)
{
string[] DriveList = Environment.GetLogicalDrives();
for (int i = 0; i < DriveList.Length; i++)
{
if (sDriveLetter + ":\\" == DriveList[i].ToString())
{
return true;
}
}
return false;
}
}
if (!DriveSettings.IsDriveMapped(mapDriveLetter)) // For example: "K"
{
DriveSettings.MapNetworkDrive(mapDriveLetter, shareName, adminUserName, adminPassword);
}
if (DriveSettings.IsDriveMapped(mapDriveLetter))
{
DriveSettings.DisconnectNetworkDrive(mapDriveLetter, true);
}