2010-11-17 19 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(); 
      } 
     }