【问题标题】:fetching the name of a file and displaying it on the title window获取文件名并将其显示在标题窗口中
【发布时间】:2015-07-03 05:31:55
【问题描述】:

我正在尝试用 C# 制作像 VLC 这样的媒体播放器。我想在当前加载的标题窗口上显示文件的名称。我不知道该怎么做。我对此进行了很多搜索,但我无法从链接Set a taskbar text different from the Window title in wpfcan I add text box beside the title of WPF window 中得到正确答案。

我在获取文件名时也遇到了问题。获取文件名时,我得到的不仅仅是文件名,而是整个路径。我没有从链接How to get only filenames within a directory using c#? 得到这个想法

 mediaElement.Source = new Uri(loadfile.FileName);
 this.Title =loadfile.FileName;

现在文件名不是文件名而是完整路径。如何仅获取文件名。 任何帮助都是可观的。

【问题讨论】:

  • @tmack 但是它获取的路径呢。我只想获取文件名而不是整个路径。
  • var fi = new FileInfo("path/to/file.ext");字符串文件名 = fi.Name;
  • 感谢您的帮助@tmack

标签: c# wpf


【解决方案1】:

您可以在后面的代码中将窗口标题绑定到一个属性。 例如:

<Window title={Binding ToMyProperty} /> etc...

【讨论】:

    【解决方案2】:

    要从文件路径中获取文件名,您可以使用 System.IO 命名空间中的 Path.GetFileName 方法。这将从提供​​的路径返回文件名。

    我已经修改了您提供的示例,按照您的描述进行:

    mediaElement.Source = new Uri(loadfile.FileName);
    this.Title = System.IO.Path.GetFileName(loadfile.FileName);
    

    希望对您有所帮助。

    【讨论】:

      猜你喜欢
      • 2020-05-31
      • 1970-01-01
      • 2013-12-24
      • 2015-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多