-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プロパティはreadonlyです。一時的な文字列に値を保存する必要があります。OpenFileDialog

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 に答える