从网络驱动器连接/复制 [英] Connecting / Copying from network drive
问题描述
不太确定该如何处理.我研究了一下,但很快就提出来了.尝试在工作时连接到网络驱动器并复制最新的文件夹(更新到项目)对我来说,目录以\开头,但是当我将其添加到字符串变量时,它将无法连接,并且在我显示时不会显示尝试检查它.有这个过程吗?
Not totally sure how to approach this. I've researched a bit but I've come up short. Trying to connect to the network drives at work and copy out the newest folder (updates to a project) For me, the dir starts as \ but when i add that to a string variable it won't connect and won't display when i attempt to check it. is there a process to this?
这就是我所拥有的.而且在某种程度上一定是错误的.
This is what i have. And it has to be wrong in someway.
string updir = @"\\NetworkDrive\updates\xxxxx";
public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{
try
{
//check if the target directory exists
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
//copy all the files into the new directory
foreach (FileInfo fi in source.GetFiles())
{
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}
//copy all the sub directories using recursion
foreach (DirectoryInfo diSourceDir in source.GetDirectories())
{
DirectoryInfo nextTargetDir = target.CreateSubdirectory(diSourceDir.Name);
CopyAll(diSourceDir, nextTargetDir);
}
//success here
copyall = true;
}
catch (IOException ie)
{
//handle it here
copyall = false;
}
}
我一直在用它来复制.而且效果很好.
i've been using that to copy. And it works well.
DateTime lastHigh = new DateTime(1900, 1, 1);
string highDir;
foreach (string subdir in Directory.GetDirectories(updir))
{
DirectoryInfo fi1 = new DirectoryInfo(subdir);
DateTime created = fi1.LastWriteTime;
if (created > lastHigh)
{
highDir = subdir;
lastHigh = created;
}
}
,然后找到最新的文件夹.
and that to find the newest folder.
推荐答案
您可以尝试使用以下方法(指定网络共享的访问权限):
You can try with something like this (specifying access rights for the network share ):
string updir = @"\\NetworkDrive\updates\somefile";
AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);
WindowsIdentity identity = new WindowsIdentity(username, password);
WindowsImpersonationContext context = identity.Impersonate();
File.Copy(updir, @"C:\somefile", true);
这篇关于从网络驱动器连接/复制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!