相关疑难解决方法(0)

将多个文件合并为单个文件

码:

static void MultipleFilesToSingleFile(string dirPath, string filePattern, string destFile)
{
    string[] fileAry = Directory.GetFiles(dirPath, filePattern);

    Console.WriteLine("Total File Count : " + fileAry.Length);

    using (TextWriter tw = new StreamWriter(destFile, true))
    {
        foreach (string filePath in fileAry)
        {
            using (TextReader tr = new StreamReader(filePath))
            {
                tw.WriteLine(tr.ReadToEnd());
                tr.Close();
                tr.Dispose();
            }
            Console.WriteLine("File Processed : " + filePath);
        }

        tw.Close();
        tw.Dispose();
    }
}
Run Code Online (Sandbox Code Playgroud)

我需要优化它,因为它非常慢:平均大小为40 - 50 Mb XML文件的45个文件需要3分钟.

请注意:45个平均45 MB的文件只是一个例子,它可以是大小n的文件m数,其中n有数千个m,平均可以是128 Kb.简而言之,它可以变化.

您能否提供有关优化的任何观点?

.net c# file-io copy

16
推荐指数
1
解决办法
4万
查看次数

变量初始化/声明中的逗号

我偶然发现了一段代码如下:

    void check()
    {
        int integer = 7;

        //integer2 is not declared anywhere
        int check = integer, integer2;

        //after running
        //check = 7
        //integer = 7
        //integer2 = 0
    }
Run Code Online (Sandbox Code Playgroud)

逗号的目的是什么?

c# variables variable-declaration

6
推荐指数
1
解决办法
973
查看次数

在C#中设置int时,逗号运算符/分隔符的机制是什么?

我正在分析此stackoverflow问题的接受答案中的代码示例,其中包含以下代码块:

public static void SplitFile(string inputFile, int chunkSize, string path)
{
    const int BUFFER_SIZE = 20 * 1024;
    byte[] buffer = new byte[BUFFER_SIZE];

    using (Stream input = File.OpenRead(inputFile))
    {
        int index = 0;
        while (input.Position < input.Length)
        {
            using (Stream output = File.Create(path + "\\" + index))
            {
                int remaining = chunkSize, bytesRead;
                while (remaining > 0 && (bytesRead = input.Read(buffer, 0,
                        Math.Min(remaining, BUFFER_SIZE))) > 0)
                {
                    output.Write(buffer, 0, bytesRead);
                    remaining -= bytesRead;
                }
            }
            index++;
            Thread.Sleep(500); // …
Run Code Online (Sandbox Code Playgroud)

c# operators comma

3
推荐指数
1
解决办法
180
查看次数

标签 统计

c# ×3

.net ×1

comma ×1

copy ×1

file-io ×1

operators ×1

variable-declaration ×1

variables ×1