[英]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.