使用NetworkConnection类后无法在共享文件夹中搜索

时间:2019-06-27 10:14:52

标签: c# asp.net networking file-sharing

使用fi.GetFiles(searchPattern, SearchOption.AllDirectories后尝试从共享文件夹new NetworkConnection(path, credentials)碰壁。

我正在使用this中的NetworkConnection.csnew NetworkConnection(path, credentials)的调用工作正常,我得到result = 0
但是执行fi.GetFiles(searchPattern, SearchOption.AllDirectories时,我得到了Exception thrown: 'System.NotImplementedException' in System.Private.CoreLib.dll Microsoft.AspNetCore.Hosting.Internal.WebHost:Information: Request finished in 8802.3403ms 500 text/html; charset=utf-8 Failed to load resource: the server responded with a status of 500 () [https://localhost:44373/Materials/GetFiles?MCodeID=24140.00]

~/Materials/GetFiles的完整代码为:

public JsonResult GetFiles(string MCodeID)
{
  if (MCodeID == null)
  {
    throw new ArgumentNullException(nameof(MCodeID));
  }

  List<Files> filelist = new List<Files>();
  string path = @"\\192.168.1.191\Materials Project";
  string searchPattern = MCodeID + "*";
  var credentials = new NetworkCredential("username", "pass", "domain");
  using (new NetworkConnection(path, credentials))
  {
    DirectoryInfo fi = new DirectoryInfo(path + '\\');
    if (fi.GetFiles(searchPattern, SearchOption.AllDirectories).Any())
    {
      foreach (var file in fi.GetFiles(searchPattern, SearchOption.AllDirectories))
      {
        var changeSlash = file.FullName.Replace('\\', '/');
        var nfilepath = changeSlash.Replace("//192.168.1.191/Materials Project", "");
        filelist.Add(new Files
        {
          FCodeID = nfilepath,
          FDescr = file.Name + " | " + Math.Round((Convert.ToDouble(file.Length) / (1024 * 1024)), 2) + " MB",
        });
      }
      filelist.Insert(0, new Files { FCodeID = "0", FDescr = "--Select File--" });
    }
    else
    {
      filelist.Insert(0, new Files { FCodeID = "0", FDescr = "--No File--" });
    }
  }
  return Json(new SelectList(filelist, "FCodeID", "FDescr"));
}

我所缺少的是什么?
在此先感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

我通过为用户提供对共享文件夹的完全访问权限而不仅仅是读取权限来解决上述错误。