在被访问之后,拒绝访问该路径?

时间:2015-12-01 14:35:11

标签: c# .net networking

在我目前的程序中,我有包含

的主要方法
using (NetworkShareAccessor.Access(---credentials etc---)
{
       (string latest = new DirectoryInfo(---folder on the network---).GetDirectories().OrderByDescending(d => d.LastWriteTimeUTC).First().ToString();

       CopyFiles(latest, "---folder---", "---file name---");
}

这成功访问了网络文件夹,我知道,因为如果我选择Console.WriteLine(最新),它会将正确的文件夹输出到控制台,证明已经访问了fodler。但是,在我的CopyFiles方法中,我得到了未处理的异常,该异常表明:

  

拒绝访问路径'---路径位置---'

我需要使用外部方法,并且不能简单地将它全部放在Main()中,因为它将在别处使用并重复使用,因此拥有它自己的方法并传入包含文件夹的不同参数更有意义名称和文件名。

我已经尝试将CopyFiles方法中的所有内容放在它自己的using语句中,我在主方法中使用过,但这也不起作用。这里仅供参考,这是CopyFiles方法中使用的代码

static void CopyFiles(string mostRecentFolder, string installerFolder, string installerName)
    {
        string sourcePath = string.Format(@"\\---network directory---\{0}\{1}", mostRecentFolder,
            installerFolder, installerName);
        string targetPath = string.Format(@"C:\Temp\");

        if (!Directory.Exists(targetPath))
            Directory.CreateDirectory(targetPath);

        string sourceFile = Path.Combine(sourcePath, installerName);
        string destFile = Path.Combine(targetPath, installerName);

        File.Copy(sourcePath, destFile, true);
    }

2 个答案:

答案 0 :(得分:1)

你有: File.Copy(sourcePath,destFile,true);

尝试:  File.Copy( sourceFile ,destFile,true);

答案 1 :(得分:0)

尝试在路径中包含您的文件名,这似乎是您出错的原因。