Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# e.Fullname without the extension

Tags:

c#

When I use e.Fullname or e.Name on a file, it gives me the full name + extension, but I just want the name of the file without like:".txt" after the name.

How do I show just the file name without the extension behind it? Is there a particular code for it?

Do I have to get the extension from the e.fullname apart and just use the piece that remains as the name without extension? Which one of these should I do?

Like at this:

 private void fileSystemWatcher1_Changed(object sender, System.IO.FileSystemEventArgs e)
    {
        listBox1.Items.Add("File changed> " + e.FullPath + " -Date:" + DateTime.Now);

        name = e.Name;
        extension = Path.GetExtension(e.FullPath);
        size = e.Name.Length;
}

2 Answers

System.IO.Path has a GetFileNameWithoutExtension method which sounds like it does what you're looking for:

Returns the file name of the specified path string without the extension.

like image 116
Damien_The_Unbeliever Avatar answered Nov 25 '25 23:11

Damien_The_Unbeliever


It is not clear which API you are using but it seems that you have a file name in the e.Fullname variable.

To clean up the file name you can use methods from the System.IO.Path class. In your case you would use the System.IO.Path.GetFileNameWithoutExtension method.

like image 31
Rune Grimstad Avatar answered Nov 26 '25 01:11

Rune Grimstad



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!