0

I have the following code where it will retrieve data from stream and print it to text file using streamwriter and bufferedwriter. By default, it will download the file to the download folder directory in your local pc. The issue that I’m facing right now is I would like to change the directory of the downloaded file to some other directory but I was unable to achieve those. Instead I use System.IO.File.WriteAllText to achieve those but that’s not what I want since the code below will download two files, one in download folder and another in document folder.

protected override void AddPlainText(Stream outputStream)
{
    var records = GetData();
    var x = new StringBuilder();
    string stamp = DateTime.Now.ToString("dd-MM-yy_HH-mm-ss", CultureInfo.InvariantCulture);
    string fileName = "reportTxt_" + stamp + ".txt";
    string path = @"C://Users//***//Documents//";
    string path2 = @"C:\Users\***\Documents\";

    using (var stream = new BufferedStream(outputStream))
    {
        var writer = new StreamWriter(stream, Encoding.ASCII);

        foreach (var r in records)
        {
            x.AppendLine(r.ToFixedLengthString());
        }
        var output = x.ToString();
        writer.WriteLine(output);
        writer.Flush();

        //Create File
        System.IO.File.WriteAllText(path + fileName, output);

        //Calling Transfer Method
        SFTP_Connection(fileName, path2);

        //Delete file after transfer
        if (File.Exists(path + fileName))
        {
            File.Delete(path + fileName);
        }
    }
 }

Any advice, tips and help will be much appreciated.

Martijn
  • 11,964
  • 12
  • 50
  • 96
  • Any reason why the file has to be saved to `path`, then copied from `path` to `path2`? Cannot it be saved to `path2` directly? – kennyzx Oct 09 '18 at 07:31
  • @kennyzx Its basically for SFTP Connection where the directory does not recognize "/" but instead it recognize "\" – Hakim Bajuri Oct 09 '18 at 07:40
  • Still there are some things to clarify. 1. `SFTP_Connection` transfers the file in the document folder to a remote place, right? 2. "one in download folder and another in document folder", you mean another file is created in _Downloads_? 3. What is the parameter `outputStream` and how is it created? – kennyzx Oct 09 '18 at 08:03
  • @kennyzx Yes you are right to all. Without System.IO.File.WriteAllText code, the code will still create the txt file but will be placed at the download folder by default. Even if I use filestream, it will create another file. – Hakim Bajuri Oct 09 '18 at 08:27

1 Answers1

0

From your description, I think you may have mistakenly assume the data is read from the stream, in fact, in the code snippet, the stream is used to be written to.

First, data is retrieved from this call

var records = GetData();

And then the data is converted to a string, and the string is written to the BufferedStream using a StreamWriter, and finally written to the outputStream.

(BufferedStream is used as a buffer - thus the name 'BufferedStream' - in order to boost performance of the write operation.)

So one possibility is that the outputStream is a FileStream created by the caller, and it is created in the Downloads folder.

If this is the case, you don't need to pass the outputStream, in fact, don't create it in the first place!

After removing the unnecessary streams, this is all you need

var records = GetData();
var x = new StringBuilder();
string stamp = DateTime.Now.ToString("dd-MM-yy_HH-mm-ss", CultureInfo.InvariantCulture);
string fileName = "reportTxt_" + stamp + ".txt";
string path = @"C://Users//***//Documents//";
string path2 = @"C:\Users\***\Documents\";
foreach (var r in records)
{
    x.AppendLine(r.ToFixedLengthString());
}
//Create File
System.IO.File.WriteAllText(path + fileName, output);

//Calling Transfer Method
SFTP_Connection(fileName, path2);

//Delete file after transfer
if (File.Exists(path + fileName))
{
    File.Delete(path + fileName);
}

Another possibility is the call SFTP_Connection creates an intermediate file in the Downloads folder. Check the implementation of the call for sure.

You need to step into (F10/F11) the code to check at which line the unwanted file is created.

kennyzx
  • 12,845
  • 6
  • 39
  • 83