Remove file extension from a file name string

前端 未结 12 1688
一个人的身影
一个人的身影 2020-11-27 14:18

If I have a string saying \"abc.txt\", is there a quick way to get a substring that is just \"abc\"?

I can\'t do an fileName.IndexOf(

12条回答
  •  清酒与你
    2020-11-27 14:45

        /// 
        /// Get the extension from the given filename
        /// 
        /// the given filename ie:abc.123.txt
        /// the extension ie:txt
        public static string GetFileExtension(this string fileName)
        {
            string ext = string.Empty;
            int fileExtPos = fileName.LastIndexOf(".", StringComparison.Ordinal);
            if (fileExtPos >= 0)
                ext = fileName.Substring(fileExtPos, fileName.Length - fileExtPos);
    
            return ext;
        }
    

提交回复
热议问题