How to export all source code from Visual Studio to a text file?

I have a relatively large Visual Studio solution.

I need to export all source code to a text file. I would also like to indicate the file name somewhere. How can i do this?

For example, if I have a type

namespace MyProject.Core { /// <summary> /// Enumerates possible record status /// </summary> public enum RecordType { NonWearTime = 0, WearTime = 1, NotClassified = 2 } } 

I want this to move to the output.txt file (or any other text format) and look like this

 //*********************************** //Filename: RecordType.cs //*********************************** namespace MyProject.Core { /// <summary> /// Enumerates possible record status /// </summary> public enum RecordType { NonWearTime = 0, WearTime = 1, NotClassified = 2 } } 

All other types should be added only to the end of the file. I tried Resharper, but its header file parameters can only contain static text (I tried Filename: $FILENAME$ ), and the template parameter applies only to newly created classes.

People, this is a training project where I have to provide the source code along with the thesis.

+7
source share
4 answers

That should do the trick

 string rootPath = @"path you your root folder"; var header = "***********************************" + Environment.NewLine; var files = Directory.GetFiles(rootPath, "*.cs", SearchOption.AllDirectories); var result = files.Select(path => new { Name = Path.GetFileName(path), Contents = File.ReadAllText(path)}) .Select(info => header + "Filename: " + info.Name + Environment.NewLine + header + info.Contents); var singleStr = string.Join(Environment.NewLine, result); Console.WriteLine ( singleStr ); File.WriteAllText(@"C:\output.txt", singleStr, Encoding.UTF8); 

Remarks: if you experience inefficiency or memory inefficiency, try using StringBuilder and set the Capacity value at the beginning of the sum of all files. This will eliminate the many redundant lines created in the last Select method.

+8
source

I would choose a home solution.

This will help you get the contents of each file into a String.

 using System.IO; ... foreach (string file in Directory.EnumerateFiles(folderPath, "*.cs")) { string contents = File.ReadAllText(file); } 

You have a file name, so you can add it at the beginning.

All you have to do is parse all the directories.

  public void DirSearch(string root) { foreach (string file in Directory.EnumerateFiles(root, "*.cs")) { string contents = File.ReadAllText(file); // Write to your outputfile once you've happened what you want in your header. } foreach (string d in Directory.GetDirectories(root)) { DirSearch(d); } } 
+2
source

How not to smooth the code, how about trying the Windows command line to merge all the files into one.

eg. copy / b * .cs newfile.txt

https://superuser.com/questions/111825/any-command-line-or-batch-cmd-to-concatenate-multiple-files

Admittedly, it's quick and dirty, but it can create what you need with some tailoring

+2
source

I would write a simple console application for this. It will search for all files with the * .cs extension, make the necessary changes and then save the file to the desired location. You can cycle directories with Directory.EnumerateDirectories() .

+1
source

All Articles