2017-07-07 20 views
0

私はCSharpで非常に新しく、この質問が非常にばかげていることを知っています。コンソールからテキストボックスに出力を変換する方法が必要です。おかげC#コンソール出力をテキストボックス出力に変換する

foreach (DriveInfo d in allDrives) 
{ 
    Console.WriteLine("Drive {0}", d.Name); 
    Console.WriteLine(" Drive type: {0}", d.DriveType); 
    if (d.IsReady == true) 
    { 
     Console.WriteLine(" Volume label: {0}", d.VolumeLabel); 
     Console.WriteLine(" File system: {0}", d.DriveFormat); 
     Console.WriteLine(
      " Available space to current user:{0, 15} bytes", 
      d.AvailableFreeSpace); 

     Console.WriteLine(
      " Total available space:   {0, 15} bytes", 
      d.TotalFreeSpace); 

     Console.WriteLine(
      " Total size of drive:   {0, 15} bytes ", 
      d.TotalSize); 
    } 
    Console.ReadKey(true); 
} 
+1

私はここでWPFタグを見ました。だから、あなたはWPFのテキストボックスを意味しましたか? –

+0

これは、他のプラットフォームに変換するための 'DriveInfo'と値なしの単純な使用です –

+0

このフォーラムでは、"コンソールをWPFに変換する "のようなキーフレーズを検索することを検討したいと思うかもしれません。このように:https://stackoverflow.com/search?q=convert+console+to+wpf –

答えて

2

Console.WriteLineをがあるが、ストリームConsole.Outputある内部StreamWriterのWriteLineメソッドを呼び出します。あなたは何ができるか

はあなたのループ内StringBuilder.ToString()

StringBuilder sb = new StringBuilder(); 
sb.AppendFormat("Drive {0}\n", d.Name); 
sb.AppendFormat(" Drive type: {0}\n", d.DriveType); 
    if (d.IsReady == true) 


    { 
     sb.AppendFormat(" Volume label: {0}\n", d.VolumeLabel); 
     sb.AppendFormat(" File system: {0}\n", d.DriveFormat); 
     sb.AppendFormat(
      " Available space to current user:{0, 15} bytes\n", 
      d.AvailableFreeSpace); 

     sb.AppendFormat(
      " Total available space:   {0, 15} bytes\n", 
      d.TotalFreeSpace); 

     sb.AppendFormat(
      " Total size of drive:   {0, 15} bytes \n", 
      d.TotalSize); 
    } 
txtBox1.Text = sb.ToString(); 

代わりの文字列の結果からTextを設定し、その後StringBuilderのように、別のオブジェクトを使用してのStringBuilderにあなたの結果を書いている、あなただけ追加することができますあなたの新しいテキストラインTextBox

txtBox.Text += String.Format(("Drive {0}\n", d.Name); 
    txtBox.Text += String.Format((" Drive type: {0}\n", d.DriveType); 
    if (d.IsReady == true) 


    { 
     txtBox.Text += String.Format((" Volume label: {0}\n", d.VolumeLabel); 
     txtBox.Text += String.Format((" File system: {0}\n", d.DriveFormat); 
     txtBox.Text += String.Format((
      " Available space to current user:{0, 15} bytes\n", 
      d.AvailableFreeSpace); 

     txtBox.Text +=String.Format((
      " Total available space:   {0, 15} bytes\n", 
      d.TotalFreeSpace); 

     txtBox.Text +=String.Format((
      " Total size of drive:   {0, 15} bytes \n", 
      d.TotalSize); 
    } 
関連する問題