-3

我希望编写一些 c# 代码,在其中找到字符串中的特定单词并将其从字符串中删除而不删除整个字符串。

我试过了,但它不起作用:

if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    // Read the files
    for (int i = 0; i < openFile.FileNames.Count(); i++ )
    {
        if (openFile.FileNames[i].Contains("Unknown Album"))
        {
            openFile.FileNames[i] = 
                openFile.FileNames[i].Replace("Unknown Album", string.Empty);
        }
    }
}
4

2 回答 2

8

类的.FileNames属性OpenFileDialogreadonly。您必须将值存储在临时字符串中。

http://msdn.microsoft.com/en-us/library/system.windows.forms.filedialog.filenames.aspx

于 2013-01-09T21:08:18.103 回答
0
if (openFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    // Read the files
    for (int i = 0; i < openFile.FileNames.Count; i++ )
    {
        var file = openFile.FileNames[i];
        if (file.Contains("Unknown Album") && file  != "Unknown Album")
        {
            openFile.FileNames[i] = file.Replace("Unknown Album", string.Empty);
        }
    }
}
于 2013-01-09T21:09:05.543 回答