有没有更有效的方法来编码?

时间:2015-07-30 06:52:54

标签: c# taglib-sharp

Start:
        if(fileName.Contains("$track"))
            if(musicInfo.Tag.Track.ToString() != "") {
                fileName.Replace("$track", musicInfo.Tag.Track.ToString());
            }
            else {
                switch(System.Windows.Forms.MessageBox.Show("Error: Track # missing from tag info", "Error", System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore, System.Windows.Forms.MessageBoxIcon.Error)) {
                    case System.Windows.Forms.DialogResult.Abort:
                        fileName = "ABORTED";
                        return fileName;
                    case System.Windows.Forms.DialogResult.Retry:
                        goto Start;
                    case System.Windows.Forms.DialogResult.Ignore:
                        fileName.Replace("$track", "");
                }

            }

我想不出更好的方法来写这个,这个代码还有7个块。

1 个答案:

答案 0 :(得分:1)

这个怎么样?

public string GetFileName(string fileName)
{ 
  if(fileName.Contains("$track") && 
     !String.IsNullOrEmpty(musicInfo.Tag.Track.ToString())
    {
        return fileName.Replace("$track", musicInfo.Tag.Track.ToString());
    }

     var userOption = System.Windows.Forms.MessageBox.Show(
     "Error: Track # missing from tag info", "Error", 
     System.Windows.Forms.MessageBoxButtons.AbortRetryIgnore, 
     System.Windows.Forms.MessageBoxIcon.Error)

    switch(userOption) 
    {
      case System.Windows.Forms.DialogResult.Abort:
          return  "ABORTED";
      case System.Windows.Forms.DialogResult.Retry:
          return GetFileName(fileName);
      case System.Windows.Forms.DialogResult.Ignore:
          return fileName.Replace("$track", "");
     }
}