Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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#_File_Timer_Converter_Writealltext - Fatal编程技术网

如何确定较小的时间C#

如何确定较小的时间C#,c#,file,timer,converter,writealltext,C#,File,Timer,Converter,Writealltext,我需要一种方法来存储最短的时间来替换任何现有的,但目前我所尝试的[下面]不起作用,有时可能会说2:38.4小于2:20.1 在文本文件中 88:88:8 在表单3文本框中 timerMin timerSec timerMil 写入正确的路径 using (TextReader reader = File.OpenText(pathPlayer + player[id].name + "\\time.txt")) {

我需要一种方法来存储最短的时间来替换任何现有的,但目前我所尝试的[下面]不起作用,有时可能会说2:38.4小于2:20.1

在文本文件中

88:88:8
在表单3文本框中

timerMin
timerSec
timerMil
写入正确的路径

                using (TextReader reader = File.OpenText(pathPlayer + player[id].name + "\\time.txt"))
                {
                    string z = reader.ReadLine();
                    string[] zsplit = z.Split(':');
                    reader.Close();
                    fileMin = Convert.ToInt32(timerMinute.Text);
                    recMin = Convert.ToInt32(zsplit[0]);
                    if (fileMin < recMin)
                    {
                        File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                        newPersonalRecord = true;
                    }
                    else
                    {
                        fileSec = Convert.ToInt32(timerSecond.Text);
                        recSec = Convert.ToInt32(zsplit[1]);
                        if (fileSec < recSec)
                        {
                            File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                            newPersonalRecord = true;
                        }
                        else
                        {
                            fileMil = Convert.ToInt32(timerMili.Text);
                            recMil = Convert.ToInt32(zsplit[1]);
                            if (fileMil < recMil)
                            {
                                File.WriteAllText(pathPlayer + player[id].name + "\\time.txt", timerMinute.Text + ":" + timerSecond.Text + ":" + timerMili.Text);
                                newPersonalRecord = true;
                            }
                            else
                            {

                            }
                        }
                    }

                }
使用(TextReader=File.OpenText(pathPlayer+player[id].name+“\\time.txt”))
{
字符串z=reader.ReadLine();
字符串[]zsplit=z.Split(“:”);
reader.Close();
fileMin=Convert.ToInt32(timermint.Text);
recMin=Convert.ToInt32(zsplit[0]);
if(fileMin
我已经在这方面工作了很长一段时间,我看不出哪里出了问题,帮助将是明智的


谢谢

当您应该比较时间跨度时,您正在比较文本框

如果文件中的字符串未超过一天的时间(直到“23:59:59”) 然后,您可以使用字符串通过执行
TimeSpan.Parse(“18:44:08”)并像这样比较它们

                fileMin = new TimeSpan(0, 0, int.Parse(timerMin), int.Parse(timerSec), int.Parse(timerMil));
                recTimeSpan = TimeSpan.Parse(z);

                if(fileMin > recTimeSpan)
                {// Your code}
                else
                {// Your code}
你总能做到

                recTimeSpan = new TimeSpan(0, 0, int.Parse(zsplit[0]), int.Parse(zsplit[1]), int.Parse(zsplit[2]));

当您应该比较时间跨度时,您正在比较文本框

如果文件中的字符串未超过一天的时间(直到“23:59:59”) 然后,您可以使用字符串通过执行
TimeSpan.Parse(“18:44:08”)并像这样比较它们

                fileMin = new TimeSpan(0, 0, int.Parse(timerMin), int.Parse(timerSec), int.Parse(timerMil));
                recTimeSpan = TimeSpan.Parse(z);

                if(fileMin > recTimeSpan)
                {// Your code}
                else
                {// Your code}
你总能做到

                recTimeSpan = new TimeSpan(0, 0, int.Parse(zsplit[0]), int.Parse(zsplit[1]), int.Parse(zsplit[2]));

您的代码有很多重复,您应该消除这些重复。您还可以使用
TimeSpan
存储值并进行比较

试着这样做:

string filePath = pathPlayer + player[id].name + "\\time.txt";
string z = null;
using (TextReader reader = File.OpenText(filePath))
{
    z = reader.ReadLine();
}
string[] zsplit = z.Split(':');

//Create a timespan from the values read from the .txt file
TimeSpan fileTime = new TimeSpan(0, 0, int.parse(zsplit[0]), int.parse(zsplit[1]), int.parse(zsplit[2]));
//Create a timespan from the values stored in the Textbox controls
TimeSpan inputTime = new TimeSpan(0, 0, int.parse(timerMinute.Text), int.parse(timerSecond.Text), int.parse(timerMili.Text));

//Check if the new TextBox time is faster than the time stored in the file
if(inputTime < fileTime)
{
    //new faster time, so update the file
    File.WriteAllText(filePath, inputTime.ToString("mm:ss:f"));
    newPersonalRecord = true;
}
string filePath=pathPlayer+player[id].name+“\\time.txt”;
字符串z=null;
使用(TextReader=File.OpenText(filePath))
{
z=reader.ReadLine();
}
字符串[]zsplit=z.Split(“:”);
//根据从.txt文件读取的值创建时间跨度
TimeSpan fileTime=newtimespan(0,0,int.parse(zsplit[0])、int.parse(zsplit[1])、int.parse(zsplit[2]);
//从Textbox控件中存储的值创建时间跨度
TimeSpan inputTime=新的TimeSpan(0,0,int.parse(timermiut.Text),int.parse(timerSecond.Text),int.parse(timerMili.Text));
//检查新文本框时间是否比文件中存储的时间快
如果(输入时间<文件时间)
{
//新的更快的时间,所以更新文件
File.writealText(文件路径,inputTime.ToString(“mm:ss:f”);
newPersonalRecord=true;
}
需要注意的几点:

  • 输入没有验证,因此无效数据将使应用程序崩溃(您应该添加验证,请参阅:)。您还需要验证
    z
    是否不为空,并且
    zsplit
    长度至少为
    3
  • 您对
    fileMin
    recMin
    的命名与您的数据源不匹配,我将它们重命名为
    fileTime
    inputTime
    ,以便更清楚
  • 本例检查
    inputTime
    是否小于
    fileTime
    ,如果您希望使用另一种方式,则只需在
    if
    语句中切换它们
  • 本例中使用的
    TimeSpan
    假设分钟分量永远不能大于
    59

您的代码有很多重复,您应该设法消除这些重复。您还可以使用
TimeSpan
存储值并进行比较

试着这样做:

string filePath = pathPlayer + player[id].name + "\\time.txt";
string z = null;
using (TextReader reader = File.OpenText(filePath))
{
    z = reader.ReadLine();
}
string[] zsplit = z.Split(':');

//Create a timespan from the values read from the .txt file
TimeSpan fileTime = new TimeSpan(0, 0, int.parse(zsplit[0]), int.parse(zsplit[1]), int.parse(zsplit[2]));
//Create a timespan from the values stored in the Textbox controls
TimeSpan inputTime = new TimeSpan(0, 0, int.parse(timerMinute.Text), int.parse(timerSecond.Text), int.parse(timerMili.Text));

//Check if the new TextBox time is faster than the time stored in the file
if(inputTime < fileTime)
{
    //new faster time, so update the file
    File.WriteAllText(filePath, inputTime.ToString("mm:ss:f"));
    newPersonalRecord = true;
}
string filePath=pathPlayer+player[id].name+“\\time.txt”;
字符串z=null;
使用(TextReader=File.OpenText(filePath))
{
z=reader.ReadLine();
}
字符串[]zsplit=z.Split(“:”);
//根据从.txt文件读取的值创建时间跨度
TimeSpan fileTime=newtimespan(0,0,int.parse(zsplit[0])、int.parse(zsplit[1])、int.parse(zsplit[2]);
//从Textbox控件中存储的值创建时间跨度
TimeSpan inputTime=新的TimeSpan(0,0,int.parse(timermiut.Text),int.parse(timerSecond.Text),int.parse(timerMili.Text));
//检查新文本框时间是否比文件中存储的时间快
如果(输入时间<文件时间)
{
//新的更快的时间,所以更新文件
File.writealText(文件路径,inputTime.ToString(“mm:ss:f”);
newPersonalRecord=true;
}
需要注意的几点:

  • 输入没有验证,因此无效数据将使应用程序崩溃(您应该添加验证,请参阅:)。您还需要验证
    z
    是否不为空,并且
    zsplit
    长度至少为
    3