C# 如何使用 Stream 读取 2 个文件并将响应都作为 1
C# how to read 2 files using Stream and response both as 1
我需要读取 2 个文件并以某种方式组合它们并将它们都作为 1 个响应。
我不想创建包含两个文件文本的新文件。
这是我响应主文件的代码,
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);
}
我需要在主文件完成后立即读取我的第二个文件。
一个简单的解释方法:
假设主文件包含:"hello"
第二个文件包含:"what a beautiful day"
我的回复应该是:
"hello"
"what a beautiful day"
提前致谢
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);
}
}
}
}
这是我所做的,不确定它是使用 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;
}
FileStream 和StreamReader 一样也是一次性对象。最好也将其包装在 using 语句中。另外,为了使代码更具可重用性,将读取文本文件的代码放入其自己的方法中,例如:
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();
}
}
}
您的问题似乎需要 asp.net 标签
context.Response.WriteFile(Settings.Default.negSourceLocation + "/neg.acc");
context.Response.WriteFile(Settings.Default.negSourceLocation + "/neg2.acc");
我需要读取 2 个文件并以某种方式组合它们并将它们都作为 1 个响应。
我不想创建包含两个文件文本的新文件。
这是我响应主文件的代码,
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);
}
我需要在主文件完成后立即读取我的第二个文件。
一个简单的解释方法:
假设主文件包含:"hello"
第二个文件包含:"what a beautiful day"
我的回复应该是:
"hello"
"what a beautiful day"
提前致谢
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);
}
}
}
}
这是我所做的,不确定它是使用 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;
}
FileStream 和StreamReader 一样也是一次性对象。最好也将其包装在 using 语句中。另外,为了使代码更具可重用性,将读取文本文件的代码放入其自己的方法中,例如:
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();
}
}
}
您的问题似乎需要 asp.net 标签
context.Response.WriteFile(Settings.Default.negSourceLocation + "/neg.acc");
context.Response.WriteFile(Settings.Default.negSourceLocation + "/neg2.acc");