不完全确定如何解决这个问题。我做了一些研究,但还是不够。尝试连接到工作中的网络驱动器并复制出最新的文件夹(更新到项目)对我来说,目录以 \ 开头,但是当我将其添加到字符串变量时,它不会连接,并且在我时不会显示尝试检查一下。这有一个过程吗?
这就是我所拥有的。它一定在某种程度上是错误的。
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;
}
}
我一直用它来复制。而且效果很好。
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;
}
}
然后找到最新的文件夹。
您可以尝试这样的操作(指定网络共享的访问权限):
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)