Remove file extension from a file name string

前端 未结 12 1604
一个人的身影
一个人的身影 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 15:03

    String.LastIndexOf would work.

    string fileName= "abc.123.txt";
    int fileExtPos = fileName.LastIndexOf(".");
    if (fileExtPos >= 0 )
     fileName= fileName.Substring(0, fileExtPos);
    
    0 讨论(0)
  • The Path.GetFileNameWithoutExtension method gives you the filename you pass as an argument without the extension, as should be obvious from the name.

    0 讨论(0)
  • 2020-11-27 15:09

    if you want to use String operation then you can use the function lastIndexOf( ) which Searches for the last occurrence of a character or substring. Java has numerous string functions.

    0 讨论(0)
  • 2020-11-27 15:09

    I know it's an old question and Path.GetFileNameWithoutExtensionis a better and maybe cleaner option. But personally I've added this two methods to my project and wanted to share them. This requires C# 8.0 due to it using ranges and indices.

    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);
    }
    
    0 讨论(0)
  • 2020-11-27 15:09
            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!!!");
                }
            }
    
    0 讨论(0)
  • 2020-11-27 15:10

    I used the below, less code

    string fileName = "C:\file.docx";
    MessageBox.Show(Path.Combine(Path.GetDirectoryName(fileName),Path.GetFileNameWithoutExtension(fileName)));
    

    Output will be

    C:\file

    0 讨论(0)
提交回复
热议问题