简体   繁体   English

C#如何使用Stream读取2个文件并将响应都设为1

[英]C# how to read 2 files using Stream and response both as 1

I need to read 2 files and somehow combine them and response them both as 1. 我需要读取2个文件,然后以某种方式将它们组合并以1作为响应。
I don't want to create a new file containing both files text. 我不想创建一个包含两个文件文本的新文件。
This is my code to response my main file, 这是我响应主文件的代码,

FileStream fs = File.OpenRead(string.Format("{0}/neg.acc",
                              Settings.Default.negSourceLocation));
using (StreamReader sr = new StreamReader(fs))
{
   string jsContent = sr.ReadToEnd();
   context.Response.Write(jsContent);
}

I need my 2nd file to be read right after the main is done. 完成主程序后,我需要立即读取第二个文件。

An easy way to explain it: 一种简单的解释方式:
lets assume main file contains : "hello" 假设主文件包含:“ hello”
and 2nd file contains: "what a beautiful day" 第二个文件包含:“多么美好的一天”

my response should be: 我的回答应该是:
"hello" “你好”
"what a beautiful day" “多美好的一天”

Thanks in advance 提前致谢

using System;
using System.IO;
using System.Text;

class Test
{
    public static void Main()
    {
        string File1 = @"c:\temp\MyTest1.txt";
        string File2 = @"c:\temp\MyTest2.txt";

        if (File.Exists(File1))
        {
            string appendText = File.ReadAllText(File1);
            if (File.Exists(File2))
            {
                appendText += File.ReadAllText(File2);
            }
        }
    }
}

FileStream is also a disposable object like StreamReader. FileStream还是StreamReader之类的一次性对象。 Best to wrap that in a using statement too. 最好也将其包装在using语句中。 Also to make the code a little more reusable, place the code to read the text file into its own method, something like: 另外,为了使代码更具可重用性,请将用于读取文本文件的代码放入其自己的方法中,例如:

public static string CombineFilesText(string mainPath, string clientPath)
{
    string returnText = ReadTextFile(mainPath);
    returnText += ReadTextFile(clientPath);

    return returnText;
}

private static string ReadTextFile(string filePath)
{
    using (FileStream stream = File.OpenRead(filePath))
    {
        using (StreamReader reader = new StreamReader(stream))
        {
            return reader.ReadToEnd();
        }
    }
}

seems like your question needs asp.net tag 似乎您的问题需要使用asp.net标签

context.Response.WriteFile(Settings.Default.negSourceLocation + "/neg.acc");
context.Response.WriteFile(Settings.Default.negSourceLocation + "/neg2.acc");

https://msdn.microsoft.com/en-us/library/dyfzssz9 https://msdn.microsoft.com/zh-CN/library/dyfzssz9

This is what I did, not sure its the right way using C#, 这就是我所做的,不确定使用C#的正确方法,

 static public string CombineFilesText(string mainPath, string clientPath)
    {
        string returnText = "";

        FileStream mfs = File.OpenRead(mainPath);
        using (StreamReader sr = new StreamReader(mfs))
        {
            returnText += sr.ReadToEnd();
        }
        FileStream cfs = File.OpenRead(clientPath);
        using (StreamReader sr = new StreamReader(cfs))
        {
            returnText += sr.ReadToEnd();
        }

        return returnText;
    }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM