C# SFTP: Exception thrown: 'System.NotSupportedException' in mscorlib.dll

  • Thread starter Thread starter samiarja
  • Start date Start date
S

samiarja

Guest
I recently built a C# windows form application to transfer data from a remote server to a local server. The application read from a json file the information below:

1. username

2. password

3. machine name

4. source folder

5. destination folder

6. file extension

and then use this information to connect to the SFTP server.

The remote server is structured like below:

|-Home\
|-root
|-bin
|-conf
|-data
|-completed
|-current

I have tried to transfer data by reading the json file and deserializing it, and then connect to the SFTP and then finally starts downloading them. I have used to code below to do that.


private void button2_Click(object sender, EventArgs e)
{

filePath = @"D:\DataProfiler_Nautitech\JSON\app-db.json";
string text = File.ReadAllText(filePath);
var currentList = JsonConvert.DeserializeObject<List<Datalogger>>(text);


//first data
string host = currentList[1].IPaddress;
string username = currentList[1].username;
string password = currentList[1].password;
string remoteDirectory = currentList[1].sourcefolder;
string localDirectory = currentList[1].destfolder;
string filextension = currentList[1].filextension;

using (SftpClient sftp = new SftpClient(host, username, password))
{
try
{
sftp.Connect();
var files = sftp.ListDirectory(remoteDirectory);

foreach (var file in files)
{
try
{

string remoteFileName = file.Name;
Console.WriteLine(remoteFileName);
string path = remoteDirectory + "/" + remoteFileName;
if ((file.Name.EndsWith(filextension)) || (file.Name.EndsWith(filextension.ToUpper())) || (file.Name.EndsWith(filextension.ToLower())))
{
using (Stream file1 = File.OpenWrite(Path.Combine(localDirectory, remoteFileName)))
{
//Console.WriteLine(file1);
sftp.DownloadFile(path, file1);

}
}
}
catch (Exception er1)
{
//MessageBox.Show("An exception has been caught " + er1.ToString());
}

}
}
catch (Exception entry)
{
MessageBox.Show(entry.Message);
}
//finally
//{
// sftp2.Disconnect();
//}
}
}

I was able to download every data from each folder, the code works perfectely and smoothly on each folder but it doesn't work on "completed" folder, and it through an error in the console like below:

DADLoggerDB_2018-04-29T08:00:00.db
Exception thrown: 'System.NotSupportedException' in mscorlib.dll
DADLoggerDB_2018-04-23T14:00:00.db
Exception thrown: 'System.NotSupportedException' in mscorlib.dll
DADLoggerDB_2018-05-19T10:00:00.db
Exception thrown: 'System.NotSupportedException' in mscorlib.dll
DADLoggerDB_2019-07-20T07:00:00.db
Exception thrown: 'System.NotSupportedException' in mscorlib.dll


I have tried to create a new folder inside the "data" directory, name it "completed1" and put everything from "completed" in it. That way it worked and i was able to transfer to data to my local server. However, when I tried to transfer data from "completed" it through that error.

I have tried to get full permission to access those folders, but unfortunately the error still exists. Until now I didn't find an explanation or solution to this error.

Continue reading...
 
Back
Top