从文件名字符串中删除文件扩展名

时间:2011-09-09 01:19:06

标签: c# string parsing

如果我有一个字符串"abc.txt",是否可以快速获取仅"abc"的子字符串?

我无法执行fileName.IndexOf('.'),因为文件名可能是"abc.123.txt"或其他东西,我显然只是想摆脱扩展名(即"abc.123")。

12 个答案:

答案 0 :(得分:326)

Path.GetFileNameWithoutExtension方法为您提供了作为参数传递但没有扩展名的文件名,从名称中可以看出这一点。

答案 1 :(得分:209)

为此目的,框架中有一个方法,它将保留除扩展名之外的完整路径。

System.IO.Path.ChangeExtension(path, null);

如果只需要文件名,请使用

System.IO.Path.GetFileNameWithoutExtension(path);

答案 2 :(得分:51)

您可以使用

string extension = System.IO.Path.GetExtension(filename);

然后手动删除扩展程序:

string result = filename.Substring(0, filename.Length - extension.Length);

答案 3 :(得分:24)

String.LastIndexOf可以工作。

string fileName= "abc.123.txt";
int fileExtPos = fileName.LastIndexOf(".");
if (fileExtPos >= 0 )
 fileName= fileName.Substring(0, fileExtPos);

答案 4 :(得分:11)

如果你想创建没有扩展名的完整路径,你可以这样做:

Path.Combine( Path.GetDirectoryName(fullPath), Path.GetFileNameWithoutExtension(fullPath))

但我正在寻找更简单的方法来做到这一点。有没有人有任何想法?

答案 5 :(得分:3)

我使用了下面的代码


string fileName = "C:\file.docx";
MessageBox.Show(Path.GetDirectoryName(fileName)+"\\"+Path.GetFileNameWithoutExtension(fileName));
   
//输出将是“C:\ file”

答案 6 :(得分:1)

如果你想使用String操作,那么你可以使用函数lastIndexOf()来搜索最后一次出现的字符或子字符串。 Java有很多字符串函数。

答案 7 :(得分:1)

你可能不会问UWP api。 但在UWP中,file.DisplayName是没有扩展名的名称。希望对别人有用。

答案 8 :(得分:1)

我知道这是一个古老的问题,Path.GetFileNameWithoutExtension是一个更好甚至更干净的选择。但是我个人已将这两种方法添加到我的项目中,并希望共享它们。由于使用范围和索引,因此需要C#8.0。

public static string RemoveExtension(this string file) => ReplaceExtension(file, null);

public static string ReplaceExtension(this string file, string extension)
{
    var split = file.Split('.');

    if (string.IsNullOrEmpty(extension))
        return string.Join(".", split[..^1]);

    split[^1] = extension;

    return string.Join(".", split);
}

答案 9 :(得分:-1)

    /// <summary>
    /// Get the extension from the given filename
    /// </summary>
    /// <param name="fileName">the given filename ie:abc.123.txt</param>
    /// <returns>the extension ie:txt</returns>
    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;
    }

答案 10 :(得分:-2)

此实施应该有效。

string file = "abc.txt";
string fileNoExtension = file.Replace(".txt", "");

答案 11 :(得分:-3)

        private void btnfilebrowse_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            //dlg.ShowDialog();
            dlg.Filter = "CSV files (*.csv)|*.csv|XML files (*.xml)|*.xml";
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                string fileName;
                fileName = dlg.FileName;
                string filecopy;
                filecopy = dlg.FileName;
                filecopy = Path.GetFileName(filecopy);
                string strFilename;
                strFilename = filecopy;
                 strFilename = strFilename.Substring(0, strFilename.LastIndexOf('.'));
                //fileName = Path.GetFileName(fileName);             

                txtfilepath.Text = strFilename;

                string filedest = System.IO.Path.GetFullPath(".\\Excels_Read\\'"+txtfilepath.Text+"'.csv");
               // filedest = "C:\\Users\\adm\\Documents\\Visual Studio 2010\\Projects\\ConvertFile\\ConvertFile\\Excels_Read";
                FileInfo file = new FileInfo(fileName);
                file.CopyTo(filedest);
             // File.Copy(fileName, filedest,true);
                MessageBox.Show("Import Done!!!");
            }
        }