使用fi.GetFiles(searchPattern, SearchOption.AllDirectories
后尝试从共享文件夹new NetworkConnection(path, credentials)
碰壁。
我正在使用this中的NetworkConnection.cs
。 new 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"));
}
我所缺少的是什么?
在此先感谢您的帮助!
答案 0 :(得分:0)
我通过为用户提供对共享文件夹的完全访问权限而不仅仅是读取权限来解决上述错误。