2010-11-17 77 views
0

我试图获取远程系统时间(基于another post),并最终与本地系统时间设置为相同的远程系统时间。但我“想要做的值的一些显示和数据的时间值的差异(本地VS远程的)。但是,当我尝试做远程系统输出我得到一个错误的日期格式ParseExact这不是一个有效的日期时间。现在虽然我试图做到这一点在C#中,我很开放的另一种语言,我可以使用VS 2010获取并解析远程日期时间价值

这里写的是我使用至今的代码。

private void GetTime_Click(object sender, EventArgs e) 
{ 
    var st = DateTime.Now.ToString("ddd MMM dd hh:mm:ss yyy"); 

    System.Net.Sockets.TcpClient t = new System.Net.Sockets.TcpClient("10.10.10.10", 13); 
    System.IO.StreamReader rd = new System.IO.StreamReader(t.GetStream()); 
    var rt = rd.ReadLine(); 

    DateTime startTime = ParseDateTime(st) ?? DateTime.Now; 
    DateTime endTime = ParseDateTime(rt) ?? DateTime.Now; 
    TimeSpan span = endTime.Subtract(startTime); 
    var ts = span.Seconds; 

    remoteTime.Text = rt; 
    systemTime.Text = st; 
    timeDiff.Text = ts.ToString(); 

    rd.Close(); 
    t.Close(); 
} 

public static DateTime? ParseDateTime(string value) 
{ 
    CultureInfo culture = CultureInfo.CreateSpecificCulture("en-US"); 
    DateTimeStyles styles = DateTimeStyles.None; 

    return DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyy", culture, styles); 
} 

回答

1

非常奇怪的错误。

尝试增加其他Ÿ到你的代码作为一年有4个didgets。我的作品。

return DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyyy", culture, styles); 
+0

虽然这确实工作之类的,这个问题是远程时间被读为周三11月17日10:29:00 2010'或者我有多么格式说'DDD MMM DD HH:MM:SS YYY '。所以,当它尝试'TryParse'失败并跳过它。 – 2010-11-17 15:30:37

+0

即使通过ToString格式也有3年转换为4位数字。 yyy'的'如何输出为4位必须 – skyfoot 2010-11-17 17:24:16

+0

奇怪,我完全匹配的值传递,但解析它,你必须通过'yyyy'。从来没有,你知道了。 – 2010-11-17 18:01:33

0

为您的日期时间格式字符串的所有引用添加一个额外的'y'。包括该行:

var st = DateTime.Now.ToString("ddd MMM dd hh:mm:ss yyy"); 

这行:

return DateTime.ParseExact(value, "ddd MMM dd hh:mm:ss yyy", culture, styles); 

而且如果/当你当你在代码做的TryParse以后。

+0

你不需要它在st变量上,因为它会被转换为yyyy。您在ParseExact上需要它,因为格式字符串必须与输入字符串完全匹配。 – skyfoot 2010-11-17 17:19:44

0
class RemoteSystemTime 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       string machineName = "vista-pc"; 

       System.Diagnostics.Process proc = new System.Diagnostics.Process(); 
       proc.StartInfo.UseShellExecute = false; 
       proc.StartInfo.RedirectStandardOutput = true; 
       proc.StartInfo.FileName = "net"; 
       proc.StartInfo.Arguments = @"time \\" + machineName; 
       proc.Start(); 
       proc.WaitForExit(); 

       List<string> results = new List<string>(); 
       while (!proc.StandardOutput.EndOfStream) 
       { 
        string currentline = proc.StandardOutput.ReadLine(); 
        if (!string.IsNullOrEmpty(currentline)) 
        { 
         results.Add(currentline); 
        } 
       } 

       string currentTime = string.Empty; 
       if (results.Count > 0 && results[0].ToLower().StartsWith(@"current time at \\" +            machineName.ToLower() + " is ")) 
       { 
        currentTime = results[0].Substring((@"current time at \\" + machineName.ToLower() + " is        ").Length); 

        Console.WriteLine(DateTime.Parse(currentTime)); 
        Console.ReadLine(); 
       } 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       Console.ReadLine(); 
      } 
     }