0

I have a web api like:

[Route("api/fileupload/{name}")]
public string GetFileWithExtension(string name)
{
    private readonly string _recipientsExcelFilesPath = System.Web.Hosting.HostingEnvironment.MapPath(
    WebConfigurationManager.AppSettings["RecipientsExcelFiles"]);

    // _recipientsExcelFilesPath contains the absolute URI for a directory
    // the name argument is the name of a file
    // I would like to get the absolute url + path
    // For example,
    // C:\Code\MyProject\Project.WebApi\Data\MyExcelFiles.xlsx
 }

So basically, this method takes a name of the file and should return full path of that file saved on the local system + extension.

In web api config I have something like:

<appSettings>
    <add key="RecipientsExcelFiles" value="~\\Data\\RecipientsExcelFiles\\" />
</appSettings>
Blarghedy
  • 47
  • 8
ruud
  • 210
  • 4
  • 12
  • 8
    Um, the file `C:\Code\MyProject\Project.WebApi\Data\MyExcelFiles` *has* no extension. Where do you expect to get the value `.xlsx` from? – Jon Skeet Aug 17 '16 at 07:49
  • Just cos you've not got known extensions on your viewer, the OS and everything else would know its got an extension – BugFinder Aug 17 '16 at 07:52
  • If you are sure the 'absolute url' will always have a filename at the end without an extension. You could to go up one level by path separator and list the files in that directory then match against filename then extract the valid extension? – White Dragon Aug 17 '16 at 07:53
  • Possible duplicate of [Get file type in .NET](http://stackoverflow.com/questions/1437382/get-file-type-in-net) – Mars Aug 17 '16 at 07:55

2 Answers2

0

Given an absolute path, the extension can be separated with the GetExtension method of Path, which is documented here.

Codor
  • 17,447
  • 9
  • 29
  • 56
-3

This is for combine with filename

 var fileNamePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);
Liam
  • 27,717
  • 28
  • 128
  • 190
Sajidur Rahman
  • 613
  • 6
  • 9