简体   繁体   中英

Get a specific file from folder

I've this folder and I want to get a specific file from it, how can I do that given that there could be more than one file in there.

I've tried to use StartsWith but wasn't able to do so, is there any way this could be done?

The file is in the CustomerWorkSheet folder and its name starts with the customer id field value. What should I use if I have this path: .../uploads/attachments/CustomerWorkSheet/**File Name Here**

IWordDocument doc = new WordDocument();
doc.Open(
      System.Web.HttpContext.Current.Server.MapPath
             ("~/Content/uploads/attachments/CustomerWorkSheet/"), 
      FormatType.Doc);

I need some thing like that

if(file.StartsWith(customerId))

but couldn't get the file

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);
}

Based on your comments, you are looking for this:

var filename = myDirectoryFiles.SingleOrDefault(f => f.Contains("CustomerWorkSheet/" + myCustomerId + "."));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM