C#获取文件扩展名-技术&分享

来源:互联网 发布:淘宝店铺违规考试答案 编辑:程序博客网 时间:2024/05/29 12:15

方法一:

string fileFullName="D:\OneKey.exe";Path.GetExtension(fileFullName);
指定的路径的扩展名(包含句点“.”)、null 或 String.Empty 如果 path 为 null,则 GetExtension 返回 null 如果 path 不具有扩展名信息,则 GetExtension返回 String.Empty

 
方法二:

/// <summary>/// Hzfile/// 获取指定文件的扩展名/// </summary>/// <param name="filePath">指定文件的绝对路径</param>/// <returns>返回文件的扩展名,如果文件扩展名为空或文件路径错误则返回空</returns>public string GetFileExtension(string filePath){    string filePathExtension = "";    try    {        if (Directory.Exists(filePath))        {              filePathExtension = filePath.Substring(filePath.LastIndexOf(".") + 1);        }    }    catch (Exception ex)    {        MessageBox.Show(ex.Message);        return "";    }     return filePathExtension;}


0 0