只在c#中弹出一次消息框

时间:2015-11-02 17:10:16

标签: c#

我已在下面附上我的代码。我试图让消息框在弹出一个消息后消失,直到应用程序中的另一个动作触发它。

using (VIModel vi = new VIModel())
{
    var VideoFiles = vi.VI_VIDEO_FILES.Where(a => a.SEGMENT_ID == segmentId);
    foreach (var vf in VideoFiles)
    {
        string location = vf.URL;
        location = location.Replace('/', '\\');

        string[] smain = location.Split('\\');
        string unamemain = smain.Last().ToString();


        string fileToCopySource = Path.Combine(inputDirectory, location);
        string fileToCopyDestination = Path.Combine(outputDirectory, unamemain);

        foreach (char c in fileToCopySource)
        {
            if (fileToCopySource.Contains(c))
            {
                // notify user that the inputDirectory isn't valid
                MessageBox.Show("FOO");
            }
        }
        if (!File.Exists(fileToCopySource))
        {
            // notify user that file doesn't exist
            MessageBox.Show("FOO");
        }

        //File.Copy(inputDirectory + location, outputDirectory + unamemain, true);
        File.Copy(fileToCopySource, fileToCopyDestination, true);
        lbConsole.Items.Add("Moved " + location + " from: " + inputDirectory + " to " + fileToCopyDestination);
    }
}

1 个答案:

答案 0 :(得分:3)

您的代码多次发出警报 - 每次发出一次"警报"它找到的条件,即输入目录无效。您需要做的是记录任何导致需要提醒的条件,然后在循环之外仅提醒一次的事实:

bool needAlert = false;

foreach (char c in fileToCopySource)
{
    if (fileToCopySource.Contains(c))
    {
        needAlert = true;
        break; // no more looping needed.
    }
}

if(needAlert) MessageBox.Show("FOO"); // notify user that the inputDirectory isn't valid