我有这个文件夹,我想从中获取一个特定的文件,我怎么能这样做,因为那里可能有多个文件。
我已尝试使用StartsWith
,但无法这样做,有什么办法可以做到吗?
该文件位于CustomerWorkSheet
文件夹中,其名称以customer id
字段值开头。如果我有这条路,我该怎么用:.../uploads/attachments/CustomerWorkSheet/**File Name Here**
IWordDocument doc = new WordDocument();
doc.Open(
System.Web.HttpContext.Current.Server.MapPath
("~/Content/uploads/attachments/CustomerWorkSheet/"),
FormatType.Doc);
我需要这样的东西
if(file.StartsWith(customerId))
但无法获取文件
答案 0 :(得分:0)
var directoryPath = System.Web.HttpContext.Current.Server.MapPath("~/Content/uploads/attachments/CustomerWorkSheet");
var file = Directory.EnumerateFiles(directoryPath).SingleOrDefault(f => f.Contains("CustomerWorkSheet/" + myCustomerId));
if(file != null){
IWordDocument doc = new WordDocument();
doc.open(file, FormatType.Doc);
}
根据您的意见,您正在寻找:
var filename = myDirectoryFiles.SingleOrDefault(f => f.Contains("CustomerWorkSheet/" + myCustomerId + "."));