Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a relatively large Visual Studio solution.

I need to export all the source code into a text file. I would also want to include a file name somewhere. How can I do so?

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 go to the output.txt file (or any other text format) and appear like so

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

All the other types shall just be appended to the end of the file. I tried Resharper, but its header file options can only contain static text (I tried Filename: $FILENAME$) and the template option only applies to the newly created classes.

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

like image 962
oleksii Avatar asked Mar 21 '13 11:03

oleksii


4 Answers

This 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 performance or memory inefficiencies, try to use StringBuilder instead and set it's Capacity at the start to the sum of all files contents. This will eliminate lots of redundant strings, created in last Select method.

like image 163
Ilya Ivanov Avatar answered Nov 09 '22 05:11

Ilya Ivanov


I would go for a homemade solution.

This helps you get into a String the content of each file.

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

You have the filename, so you can append it at the beginning.

All you still need to do is to parse through all 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);
           }
   }
like image 38
LaGrandMere Avatar answered Nov 09 '22 05:11

LaGrandMere


As a none code soloution how about trying a windows command line to merge all files into one.

e.g. copy /b *.cs newfile.txt

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

Admittedly its quick and dirty, but it might produce what you require with some tailoring

like image 28
kevchadders Avatar answered Nov 09 '22 04:11

kevchadders


I would write a simple console app to do that. It would search for all files with a *.cs extension, make the necessary modification and then save the file to the desired location. You can loop through directories iteratively using Directory.EnumerateDirectories().

like image 39
MarcF Avatar answered Nov 09 '22 05:11

MarcF