0

I'm very new in CSharp and I know you found this question very stupid, I need an idea how to convert the output from console to textbox. thanks

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);
}
3
  • 1
    I saw WPF tag here. So, did you mean WPF textbox? Commented Jul 7, 2017 at 2:25
  • this is only simple use of DriveInfo and no value to convert to any other platform Commented Jul 7, 2017 at 2:28
  • You probably want to consider searching key phrases like "convert console to WPF" on this forum. Like this: stackoverflow.com/search?q=convert+console+to+wpf Commented Jul 7, 2017 at 2:28

1 Answer 1

2

Console.WriteLine is calls a WriteLine on an internal StreamWriter whose stream is the Console.Output.

What you can do is use another object, like a StringBuilder and write your results to the StringBuilder and then set the Text from the string result of 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();

Alternately in your loop, you can just add new lines of text to your 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);
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.