smatter
smatter

Reputation: 29178

Copying the contents of a List<string> to text file while debugging

I need to compare the contents of a List for two runs for my program. What is the easiest way to copy the entire contents of the List manually from Visual Studio to notepad while stepping through the code. I can view the contents in QuickWatch. How can I copy all the elements?

Upvotes: 24

Views: 44808

Answers (5)

testCoder
testCoder

Reputation: 7385

Easiest way:

  1. Open Watch window

  2. Type name of variable which is list

  3. Select items which need ( for whole selection press 'shift' and click on first element and after that click on the last element of the list )

  4. Press Ctrl + C or click right mouse button and select item from drop down 'Copy'

  5. After that you can able to paste your text presentation of list to any text editor.

enter image description here

Upvotes: 7

hashi
hashi

Reputation: 2559

I thinks this solution is better than.

List<string> list = new List<string>();
                list.Add("test1");
                list.Add("test2");
                list.Add("test3");
                list.Add("test4");

                File.WriteAllLines(Application.StartupPath + "\\Text.txt", list.ToArray());
                Process.Start("notepad.exe", Application.StartupPath + "\\Text.txt");

Upvotes: 8

Sven
Sven

Reputation: 22683

Simply type this into the immediate window:

 File.WriteAllLines("foo.txt", yourList);

Or if it's a list of something other than strings:

 File.WriteAllLines("foo.txt", yourList.ConvertAll(Convert.ToString));

Upvotes: 35

CharithJ
CharithJ

Reputation: 47530

Do a QuickWatch. In the quick watch window you can copy the values you want. If you want you can add some code to the top textbox in that window.

enter image description here

Upvotes: 2

Petar Ivanov
Petar Ivanov

Reputation: 93030

You can open the immediate window and run something like:

string.Join(", ", yourList)

or just

yourList

To open the immediate window: Debug -> Windows -> Immediate or the equivalent Ctrl+D, I

Upvotes: 24

Related Questions