FileInfo f=new FileInfo("C:\\mdu\\abcd.xml");
string uploadfile=f.FullName;
Console.WriteLine(f.Name);
Console.WriteLine("uploadfile"+uploadfile);
var client = new SftpClient(host, port, username, password);
client.Connect();
if(client.IsConnected){
Console.WriteLine("I AM CONNECTED");
}
var fileStream = new FileStream(uploadfile, FileMode.Open);
if(fileStream!=null){
Console.WriteLine("YOU ARE NOT NULL");
}
client.BufferSize = 4 * 1024;
client.UploadFile(fileStream, f.Name,null);
client.Disconnect();
client.Dispose();
Loading
Tuhin PaulPosted May 13, 2023, 3:50 AM
The error in the code is that the UploadFile() method requires a third parameter, which is the remote path of the file to be uploaded. The code should be changed to:
Muhammad Imran AnsariPosted May 11, 2023, 7:00 PM
What is the issue you facing in the code?
Mohamed Azarudeen ZPosted May 11, 2023, 12:00 PM
Hi what is it you want to know?
This is what the code is.
A new
FileInfoobject is created with the path to the file to be uploaded.The full path of the file is assigned to the
uploadfilevariable.The
SftpClientobject is created with the SFTP server details.The
Connectmethod of theSftpClientobject is called to establish a connection to the SFTP server.If the connection is successful, the message "I AM CONNECTED" is printed to the console.
A
FileStreamobject is created with the path to the file to be uploaded and theFileMode.Openparameter to open the file for reading.If the
FileStreamobject is not null, the message "YOU ARE NOT NULL" is printed to the console.The buffer size of the
SftpClientobject is set to 4 KB.The
UploadFilemethod of theSftpClientobject is called with theFileStreamobject, the name of the file, and a nullActiondelegate.The
DisconnectandDisposemethods of theSftpClientobject are called to close the connection and free up resources.