Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如果时间匹配,播放音频文件_C# - Fatal编程技术网

C# 如果时间匹配,播放音频文件

C# 如果时间匹配,播放音频文件,c#,C#,当字符串格式的textboxordertostart.Text中的时间等于系统时间时,为什么不能播放此音频文件Textboxordertostart从DateTimePicker时间中减去分钟后获取时间 我的代码如下 SoundPlayer myplayer = new SoundPlayer(); myplayer.SoundLocation= (@"c:\users\woolsvalley\documents\visual studio 2015\Projects\WindowsForms

当字符串格式的
textboxordertostart.Text
中的时间等于系统时间时,为什么不能播放此音频文件
Textboxordertostart
DateTimePicker
时间中减去分钟后获取时间

我的代码如下

SoundPlayer myplayer = new SoundPlayer();

myplayer.SoundLocation= (@"c:\users\woolsvalley\documents\visual studio 2015\Projects\WindowsFormsApplication17\WindowsFormsApplication17\Alarm.wav");


if (DateTime.Now.ToString("hh:mm tt") == textBox_ordertostart.Text)
{ 
    myplayer.Play(); 
}
此代码引发空异常

string formatString = "yyyHHmmss";
        string sample = textBox_ordertostart.Text;
        DateTime dt = DateTime.ParseExact(sample, formatString, null);
        if (DateTime.Now == dt)
        { myplayer.Play(); }
这不太管用

if (DateTime.Now == DateTime.Parse(textBox_ordertostart.Text))
        { myplayer.Play(); }

您进行比较的方式是一种非常“脆弱”的比较方式,因为它取决于用户以您期望的格式键入时间。例如,当我测试这个时,我得到了以下结果:

string datetime = DateTime.Now.ToString("hh:mm tt");

// False
Console.WriteLine(datetime == "2:06 PM");

        // False
        Console.WriteLine(datetime == "2:06 P.M.");
        // False
        Console.WriteLine(datetime == "2:06");

        // False
        Console.WriteLine(datetime == "02:06 P.M.");

        // True
        Console.WriteLine(datetime == "02:06 PM");
如果您将它解析为DateTime对象,然后执行ToString,那么它就不会那么脆弱了。请参见此扩展方法,例如:

public static bool DayMinuteEqual(this string otherDate)
    {
        // We have to strip out the "." character if present (e.g. 2:05 P.M.)
        DateTime otherDateObj = DateTime.Parse(otherDate.Replace(".", ""));

        return DateTime.Now.ToString("hh:mm tt") == otherDateObj.ToString("hh:mm tt");
    }
现在我得到了我期望的结果:

// True
        Console.WriteLine("2:20 PM".DayMinuteEqual());

        // True
        Console.WriteLine("2:20 P.M.".DayMinuteEqual());

        // False, but we'd expect it due to the omission of the "P.M."
        Console.WriteLine("2:20".DayMinuteEqual());

        // True
        Console.WriteLine("02:20 P.M.".DayMinuteEqual());

        // True
        Console.WriteLine("02:20 PM".DayMinuteEqual());

显然,这对用户以“完美”格式输入日期的依赖性要小得多(但仍然要求他们对正确的格式有一定的了解)。

进行比较的方式是一种非常“脆弱”的比较方式,因为它依赖于用户以您期望的格式键入时间。例如,当我测试这个时,我得到了以下结果:

string datetime = DateTime.Now.ToString("hh:mm tt");

// False
Console.WriteLine(datetime == "2:06 PM");

        // False
        Console.WriteLine(datetime == "2:06 P.M.");
        // False
        Console.WriteLine(datetime == "2:06");

        // False
        Console.WriteLine(datetime == "02:06 P.M.");

        // True
        Console.WriteLine(datetime == "02:06 PM");
如果您将它解析为DateTime对象,然后执行ToString,那么它就不会那么脆弱了。请参见此扩展方法,例如:

public static bool DayMinuteEqual(this string otherDate)
    {
        // We have to strip out the "." character if present (e.g. 2:05 P.M.)
        DateTime otherDateObj = DateTime.Parse(otherDate.Replace(".", ""));

        return DateTime.Now.ToString("hh:mm tt") == otherDateObj.ToString("hh:mm tt");
    }
现在我得到了我期望的结果:

// True
        Console.WriteLine("2:20 PM".DayMinuteEqual());

        // True
        Console.WriteLine("2:20 P.M.".DayMinuteEqual());

        // False, but we'd expect it due to the omission of the "P.M."
        Console.WriteLine("2:20".DayMinuteEqual());

        // True
        Console.WriteLine("02:20 P.M.".DayMinuteEqual());

        // True
        Console.WriteLine("02:20 PM".DayMinuteEqual());

显然,这不太依赖于用户以“完美”的格式输入日期(但仍然要求他们对正确的格式有一定的了解)。

谢谢大家。这段代码有效,我只需要在更新事件中提出它

private void timer1_Tick(object sender, EventArgs e)
    {   label_time1.Text = DateTime.Now.ToString("hh:mm tt");
        mplayer = new SoundPlayer();
        mplayer.SoundLocation = (@"c:\users\woolsvalley\documents\visual studio 2015\Projects\WindowsFormsApplication17\WindowsFormsApplication17\Alarm.wav");
        if((DateTime.Now.ToString("HH:mm tt") == ((textBox_ordertostart.Text))))
        { mplayer.Play(); }

谢谢各位。这段代码有效,我只需要在更新事件中提出它

private void timer1_Tick(object sender, EventArgs e)
    {   label_time1.Text = DateTime.Now.ToString("hh:mm tt");
        mplayer = new SoundPlayer();
        mplayer.SoundLocation = (@"c:\users\woolsvalley\documents\visual studio 2015\Projects\WindowsFormsApplication17\WindowsFormsApplication17\Alarm.wav");
        if((DateTime.Now.ToString("HH:mm tt") == ((textBox_ordertostart.Text))))
        { mplayer.Play(); }

textBox\u ordertostart.Text的值是多少?您在调试器中查看了代码吗?我会在条件行上设置一个断点,然后手动检查
DateTime.Now.ToString(“hh:mm tt”)
返回的内容。您应该能够将其与您在文本框中键入的值进行比较,并很容易看出它们不匹配的原因。您的
textbox\u顺序是否开始。Text
返回类似于此
00:00 PM
?正如我在回答中提到的,这是一种非常“严格”的比较方法。您需要首先将文本框的内容解析为日期时间,然后比较它们就容易多了。谢谢大家!在我的表单中有一个标签,它将系统时间作为DateTime.Now.ToString(“hh:mmtt”)来显示系统时间。因为标签和文本框都有字符串格式,所以我也比较了它们,但仍然没有播放文件。我将尝试将文本解析为datetime。让大家知道,
textBox\u ordertostart.Text的值是多少?您在调试器中查看了代码吗?我会在条件行上设置一个断点,然后手动检查
DateTime.Now.ToString(“hh:mm tt”)
返回的内容。您应该能够将其与您在文本框中键入的值进行比较,并很容易看出它们不匹配的原因。您的
textbox\u顺序是否开始。Text
返回类似于此
00:00 PM
?正如我在回答中提到的,这是一种非常“严格”的比较方法。您需要首先将文本框的内容解析为日期时间,然后比较它们就容易多了。谢谢大家!在我的表单中有一个标签,它将系统时间作为DateTime.Now.ToString(“hh:mmtt”)来显示系统时间。因为标签和文本框都有字符串格式,所以我也比较了它们,但仍然没有播放文件。我将尝试将文本解析为datetime。让你知道,伙计们。。。