Search code examples
c#streamwriterbufferedstream

C# Change downloaded file directory


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.


Solution

  • 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.