1

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;
}
4

2 回答 2

13

System.IO.Path有一个GetFileNameWithoutExtension听起来像你正在寻找的方法:

返回指定路径字符串的文件名,不带扩展名。

于 2013-07-01T07:31:44.937 回答
2

目前尚不清楚您使用的是哪个 API,但似乎您在 e.Fullname 变量中有一个文件名。

要清理文件名,您可以使用System.IO.Path 类中的方法。在您的情况下,您将使用System.IO.Path.GetFileNameWithoutExtension方法。

于 2013-07-01T07:32:35.530 回答