0

I need to read 2 files and somehow combine them and response them both as 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"
and 2nd file contains: "what a beautiful day"

my response should be:
"hello"
"what a beautiful day"

Thanks in advance

Vyas Bharghava
  • 6,372
  • 9
  • 39
  • 59
barak
  • 147
  • 1
  • 3
  • 17
  • You can't do it in the same Read operation. I suggest create a subroutine to read files and make 2 calls, one to each file and combine the responses in a single string variable. – David BS Aug 13 '16 at 21:01
  • Can you please show me an example how to do it? – barak Aug 13 '16 at 21:05
  • What is the problem in opening another stream on the second file, read it to end and append the content of the second file to the content of the first file? – Steve Aug 13 '16 at 21:08
  • Are the files auto-incremented ones? I mean, have each of them many lines and you need only the last one, or each file has only one line? Try: file.readalltext(sourcefile) to get all lines of text. See https://msdn.microsoft.com/en-us/library/system.io.file.readalltext(v=vs.110).aspx – David BS Aug 13 '16 at 21:09
  • @Steve I did it according to your comment, its working but is there no proper way to do it? – barak Aug 13 '16 at 21:29
  • The proper method is two open two stream readers and read one line from the 1st reader and then read one line from 2nd readers. Putting the code into a loop will simulate a two port application (each reader being one port). – jdweng Aug 13 '16 at 21:56

4 Answers4

1
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);
            }
        }
    }
}
Marco
  • 22,856
  • 9
  • 75
  • 124
David BS
  • 1,822
  • 1
  • 19
  • 35
  • 1
    You could reduce nesting, if you combine both if clauses and negate them. `if(!File.Exists(File1) || !File.Exists(File2)) return; string appendText = File.ReadAllText(File1) + File.ReadAllText(File2);` – Marco Aug 13 '16 at 22:04
1

FileStream is also a disposable object like StreamReader. Best to wrap that in a using statement too. 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();
        }
    }
}
Bobby Caldwell
  • 150
  • 2
  • 3
1

seems like your question needs asp.net tag

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

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

Slai
  • 22,144
  • 5
  • 45
  • 53
0

This is what I did, not sure its the right way using 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;
    }
barak
  • 147
  • 1
  • 3
  • 17