简体   繁体   English

如何将Visual Studio中的所有源代码导出到文本文件中?

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

I have a relatively large Visual Studio solution. 我有一个相对较大的Visual Studio解决方案。

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 我希望它转到output.txt文件(或任何其他文本格式),并显示如此

//***********************************
//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. 我尝试过Resharper,但它的头文件选项只能包含静态文本(我试过Filename: $FILENAME$ ),模板选项只适用于新创建的类。

Folks, this is a study project, where I have to provide the source code along with a thesis. 伙计们,这是一个研究项目,我必须提供源代码和论文。

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. 备注:如果遇到性能或内存效率低下,请尝试使用StringBuilder并在开始时将其设置为所有文件内容之和。 This will eliminate lots of redundant strings, created in last Select method. 这将消除在最后一个Select方法中创建的大量冗余字符串。

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);
           }
   }

As a none code soloution how about trying a windows command line to merge all files into one. 作为一个无代码解决方案如何尝试使用Windows命令行将所有文件合并为一个。

eg copy /b *.cs newfile.txt 例如copy / b * .cs newfile.txt

https://superuser.com/questions/111825/any-command-line-or-batch-cmd-to-concatenate-multiple-files 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 不可否认它快速而肮脏,但它可能会产生你需要的一些剪裁

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. 它将搜索具有* .cs扩展名的所有文件,进行必要的修改,然后将文件保存到所需位置。 You can loop through directories iteratively using Directory.EnumerateDirectories() . 您可以使用Directory.EnumerateDirectories()迭代地遍历目录。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 Visual Studio Express 2010 项目中的所有源代码提取到单个文件中? - Extracting all source code from visual studio express 2010 project into a single file? 如何在Visual Studio Express 2012中将源代码添加到.dll文件? - How to add source code to .dll file in Visual Studio Express 2012? 如何从Visual Studio中下载的包中查看源代码 - How to view the source code from downloaded packages in Visual Studio Visual Studio 插件显示资源文件中的文本而不是代码 - Visual Studio plugin to show the text from a resource file instead of the code 如何在Visual Studio中保存时从文本文件生成普通的旧代码? - How can I generate plain old code from a text file on save with visual studio? 如何在Visual Studio中查找所有源代码和引用的dll中的类/方法的所有引用? - How to find all reference of a Class/Method in all source code AND referenced dll in visual studio? 如何使用Visual Studio Code查看.dll文件的所有元数据? - How to view all metadata of a .dll file with Visual Studio Code? 如何从 Visual Studio 2019 导出程序? - How to export a program from Visual Studio 2019? 从 MS Visual Studio 复制代码文本并保留源颜色和格式 - Copy the code text from MS Visual Studio and keeping the source color and formatting 有没有办法在 Visual Studio 中自动格式化所有源代码缩进? - Is there a way to automatically format all of your source code indentation in Visual Studio?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM