【问题标题】:String from file names - How remove the extension?文件名中的字符串 - 如何删除扩展名?
【发布时间】:2014-09-15 13:04:03
【问题描述】:

我正在开发一个 Windows Phone 应用程序。我在Isolated storage 上保存了许多文件,我需要在 ListBox 上显示它们(文件名)。 我的代码工作正常,但文件名显示为扩展名 (filename.doc)。我想显示不带扩展名的文件名 (filename)。

我的代码:

IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();

string directory = "./preVenda/*.*";
string[] filenames = myIsolatedStorage.GetFileNames(directory);

List0.Items.Add(filenames);

我尝试使用“删除”方法,但不起作用。

【问题讨论】:

    标签: c# windows-phone filenames isolatedstorage


    【解决方案1】:

    你可以使用Path.GetFileNameWithoutExtension

    foreach(string file in filenames)
        List0.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file));
    

    此外,您可以尝试不使用显式循环(我认为 list0 是 System.Windows.Control.ListBox

    list0.ItemsSource = filenames.Select(d => Path.GetFileNameWithoutExtension(d)).ToList();
    

    (警告。现在无法测试)

    【讨论】:

    • 感谢支持,但收到错误:'System.Windows.Shapes.Path' does not contain a definition for 'GetFileNameWithoutExtension'
    • @ReneSá 使用System.IO.Path
    • 将 using 指令添加到您的代码 using System.IO; 或在 Path 类之前添加命名空间。 (System.IO.Path.GetFileNameWithoutExtension(.....))
    • @Steve 现在可以正常工作了!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2017-08-27
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 2012-06-29
    • 2015-08-25
    • 2023-03-10
    相关资源
    最近更新 更多