使用C#将文件从本地驱动器复制到共享驱动器
我想将文件从本地驱动器复制到共享网络路径。使用C#将文件从本地驱动器复制到共享驱动器
我曾尝试以下方法:
string remoteUserName =
WebConfigurationManager.AppSettings["remoteUsername"].ToString();
string remotePassword =
WebConfigurationManager.AppSettings["remotePassword"].ToString();
string remoteDomain =
WebConfigurationManager.AppSettings["remoteDomain"].ToString();
string remoteFilePath =
WebConfigurationManager.AppSettings["remoteFilePath"].ToString();
using (var impersonation = new
ImpersonatedUser(remoteUserName, remoteDomain, remotePassword))
{
CreateErrorLog("Logged in successfully - User and password are correct.",
"Action" + " - " + "controllerName");
string filePath = remoteFilePath;
string fileName = "txt.txt";
StreamWriter SW1;
FileIOPermission myPerm = new
FileIOPermission(FileIOPermissionAccess.AllAccess, filePath + fileName);
myPerm.Assert();
SW1 = System.IO.File.CreateText(filePath + fileName);
}
好的,我们来看看这段代码吧。首先让我们简化构建路径。我们有一个网络路径和一个本地路径。根据你当前的代码的网络路径是建立与几个变量comboBox1,comboBox2和Environment.UserName,让我们做一点点不同:
var networkPath = Path.Combine(@"\\network",
comboBox1.SelectedItem as string,
comboBox2.SelectedItem as string,
Environment.UserName);
那将放置\每个那些字符串之间正确的(即如果已经有一个反斜杠,它不会添加一个,但如果有必要的话)。
现在让我们做同样的本地路径:
var localPath = Path.Combine(@"C:\Users",
Environment.UserName,
"test",
label5.Text);
好了,我们快到了,但我们也有一个替代的网络路径:
var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
comboBox1.SelectedItem as string,
comboBox2.SelectedItem as string,
Environment.UserName,
label5.Text);
现在,一件事这已经是我怀疑的道路了,这是\ filestroage,实际上拼错了。现在,如果文件夹拼写的方式很好,但我想知道它是拼写错误。所以只要看看。好的,让我们继续,现在我们建立了所有三条路径,它更容易阅读,并且我们可以轻松输出这些字符串以确保它们是正确的。我们来看看逻辑。它说,如果networkPath存在然后将其保存在那里,但是,如果它不存在,那么创建它并将它保存到alternativeNetworkPath。所以让我们这样做:
if (Directory.Exists(networkPath))
{
File.Copy(localPath, networkPath);
}
else
{
Directory.CreateDirectory(networkPath);
File.Copy(localPath, alternativeNetworkPath);
}
好吧,够简单了吗?但是你声明Directory.Exists即使存在也会返回true。这是非常期望的不是吗?如果目录存在,那么这个方法肯定会返回true,否则返回false。然后你用Directory.CreateDirectory表示,上面的行表示无法找到网络名 - 这只能表示路径构造错误。
所以在分解之后,底线是这样的,构建的路径必须离开tidge。但是,使用这个新模型,您应该能够更轻松地将这些路径拉出来。所以整个方法,在我的脑海里,会是这个样子:
var networkPath = Path.Combine(@"\\network",
comboBox1.SelectedItem as string,
comboBox2.SelectedItem as string,
Environment.UserName);
var localPath = Path.Combine(@"C:\Users",
Environment.UserName,
"test",
label5.Text);
var alternativeNetworkPath = Path.Combine(@"\\atlanta2-0\it-documents\filestroage",
comboBox1.SelectedItem as string,
comboBox2.SelectedItem as string,
Environment.UserName,
label5.Text);
if (Directory.Exists(networkPath))
{
File.Copy(localPath, networkPath);
}
else
{
Directory.CreateDirectory(networkPath);
File.Copy(localPath, alternativeNetworkPath);
}
所以现在让我们来看看在这些变量的路径,你的问题应该立刻洗掉。
网络路径是由完整的通用命名约定,UNC路径\\Server\Share\drive\file
访问。如果您具有这些类型的凭据或访问网络的权限,则可以使用File.Copy方法移动文件。
这是行不通的?你是否收到错误信息?如果是这样,它是什么? –
您可以简单地使用Process.Start作为其他用户,请参阅http://stackoverflow.com/questions/4624113/start-a-net-process-as-a-different-user或者将您的模拟更改为使用WindowsIdentity。 Impersonatehttp://stackoverflow.com/questions/125341/how-do-you-do-impersonation-in-net,并使用以下内容来检查你的ipmersonated ientity https://msdn.microsoft.com/en-us/library/ aa347790(v = vs.110).aspx –
[跨网络模拟文件副本](http:// stackoverflow。com/questions/8149648 /模拟文件复制跨网络) –