C# 获取文件扩展名并与完整路径合并

C# 获取文件扩展名并与完整路径合并,c#,.net,system.io.file,C#,.net,System.io.file,我有一个web api,如: [Route("api/fileupload/{name}")] public string GetFileWithExtension(string name) { private readonly string _recipientsExcelFilesPath = System.Web.Hosting.HostingEnvironment.MapPath( WebConfigurationManager.AppSetti

我有一个web api,如:

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

       // _recipientsExcelFilesPath  gives absolute URL to the directory
       // name property is a name of file
        // I would like here get absoulute url + path
        **C:\Code\MyProject\Project.WebApi\Data\MyExcelFiles.xlsx**

        }
因此,基本上,此方法采用文件名,并应返回保存在本地系统+扩展名上的文件的完整路径

在web api配置中,我有如下内容:

  <appSettings>
<add key="RecipientsExcelFiles" value="~\\Data\\RecipientsExcelFiles\\" />
</appSettings>


给定一个绝对路径,可以使用
path
GetExtension
方法来分配扩展名,该方法已被记录在案。

这是用于与文件名组合的

 var fileNamePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, fileName);

嗯,文件
C:\code\MyProject\Project.WebApi\Data\MyExcelFiles
没有扩展名。您希望从何处获取值
.xlsx
?只是因为您的查看器上没有已知的扩展名,操作系统和其他一切都会知道它有扩展名。如果您确定“绝对url”的结尾总是有一个文件名,而没有扩展名。您可以通过路径分隔符上一级,列出该目录中的文件,然后与文件名匹配,然后提取有效的扩展名?可能重复的