Renci.SshNet.SftpClient.CreateText C# (CSharp) Method

CreateText() public method

Creates or opens a file for writing text using the specified encoding.

If the target file already exists, it is overwritten. It is not first truncated to zero bytes.

If the target file does not exist, it is created.

is null. Client is not connected. The method was called after the client was disposed.
public CreateText ( string path, Encoding encoding ) : StreamWriter
path string The file to be opened for writing.
encoding Encoding The character encoding to use.
return StreamWriter
        public StreamWriter CreateText(string path, Encoding encoding)
        {
            CheckDisposed();

            return new StreamWriter(OpenWrite(path), encoding);
        }

Same methods

SftpClient::CreateText ( string path ) : StreamWriter

Usage Example

        public override void Send(string overrideValue)
        {
            overrideValue = overrideValue.Replace("\r", "").Replace("\n", "");

            try
            {
                int port = 22;
                var hostname = Settings.SFTPHostname;
                if (hostname.StartsWith("sftp://")) {
                    hostname = hostname.Replace("sftp://", "");
                }
                if (hostname.Contains(":")) {
                    var parts = hostname.Split(':');
                    hostname = parts[0];
                    port = int.Parse(parts[1]);
                }

                ConnectionInfo conn = new ConnectionInfo(hostname, port, Settings.SFTPUsername, new AuthenticationMethod[1] {
                    new PasswordAuthenticationMethod(Settings.SFTPUsername, Settings.SFTPPassword)
                });

                using (var client = new SftpClient(conn))
                {
                    client.Connect();

                    var file = client.CreateText(currentDespatch.SalesOrderNumber + ".csv");
                    try {
                        file.WriteLine(overrideValue);
                    }
                    finally {
                        file.Close();
                    }

                    var path = Path.Combine(Path.GetDirectoryName(Settings.LabelDirectory), "MHI.csv");
                    File.AppendAllText(path, string.Format("\"{0}\",{1}\n", DateTime.Now.ToLongTimeString(), overrideValue));

                    client.Disconnect();
                }
                WriteLog(string.Format("Sent request to {0} tracking number {1} service type {2}...", PluginName, Settings.MHIPrefix + currentDespatch.SalesOrderNumber, currentDespatch.ServiceTypeName), Color.Black);

                var ps = new PrinterSettings { PrinterName = Settings.PrinterName };
                var maxResolution = ps.PrinterResolutions.OfType<PrinterResolution>()
                                                         .OrderByDescending(r => r.X)
                                                         .ThenByDescending(r => r.Y)
                                                         .First();

                PrintDocument pd = new PrintDocument();
                pd.PrinterSettings.PrinterName = Settings.PrinterName;
                pd.PrintController = new StandardPrintController();
                pd.DefaultPageSettings.Margins = pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                pd.DefaultPageSettings.Landscape = true;
                pd.OriginAtMargins = true;

                Image label = generateLabel(maxResolution.X, maxResolution.Y);

                label.Save(Path.Combine(Settings.LabelDirectory, currentDespatch.SalesOrderNumber + "-" + currentDespatch.DespatchNumber + ".png"), ImageFormat.Png);

                pd.PrintPage += (sndr, args) =>
                {
                    args.Graphics.DrawImage(label, args.MarginBounds);
                };
                pd.Print();

                WriteLog(string.Format("Print request sent to {0}...", Settings.PrinterName), Color.Black);

                label.Dispose();

                if (this.NeedsCN22(currentDespatch.ShippingAddressCountry)) {
                    PrintDocument pd22 = new PrintDocument();
                    pd22.PrinterSettings.PrinterName = Settings.PrinterName;
                    pd22.PrintController = new StandardPrintController();
                    pd22.DefaultPageSettings.Margins = pd.PrinterSettings.DefaultPageSettings.Margins = new Margins(0, 0, 0, 0);
                    pd22.DefaultPageSettings.Landscape = false;
                    pd22.OriginAtMargins = true;

                    var cn22label = this.GenerateCN22(currentDespatch.SalesOrderNumber, currentDespatch.Items);

                    pd22.PrintPage += (sndr, args) =>
                    {
                        args.Graphics.DrawImage(cn22label, args.MarginBounds);
                    };
                    pd22.Print();

                    cn22label.Dispose();
                }

                trackingNumber = Settings.MHIPrefix + currentDespatch.SalesOrderNumber;
                Result = Settings.MHIPrefix + currentDespatch.SalesOrderNumber;
            }
            catch (Exception e)
            {
                WriteLog(string.Format("Exception on SEND:\n{0}", e.ToString()), Color.Red);
            }
        }