使用 NetworkConnection 后无法在共享文件夹中搜索 class
Can't search in shared folder after I use NetworkConnection class
我在使用 new NetworkConnection(path, credentials)
后尝试从共享文件夹 fi.GetFiles(searchPattern, SearchOption.AllDirectories
时撞墙了。
我正在使用 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"));
}
我缺少什么?
在此先感谢您的帮助!
我通过为用户提供对共享文件夹的完全访问权限而不仅仅是读取权限来解决上述错误。
我在使用 new NetworkConnection(path, credentials)
后尝试从共享文件夹 fi.GetFiles(searchPattern, SearchOption.AllDirectories
时撞墙了。
我正在使用 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"));
}
我缺少什么?
在此先感谢您的帮助!
我通过为用户提供对共享文件夹的完全访问权限而不仅仅是读取权限来解决上述错误。