Need to delete the files on the server after we pull them.

Advertisement

Philky001
Donor
Joined:
Posts:
20

Need to delete the files on the server after we pull them.

HI we are getting files from ecom site that has our customers orders. This is now working well. But in SCP manual process we then delete the files in that server, having permissions of course. The question is how to do this in the .net program.
// Setup session options
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = ConfigurationManager.AppSettings["HostName"].ToString(),
UserName = ConfigurationManager.AppSettings["UserName"].ToString(),
Password = ConfigurationManager.AppSettings["Password"].ToString(),
SshHostKeyFingerprint = ConfigurationManager.AppSettings["SshHostKeyFingerprint"].ToString()
};

using (Session session = new Session())
{
// Connect
session.Open(sessionOptions);

RemoteDirectoryInfo directory = session.ListDirectory(ConfigurationManager.AppSettings["RemotePath"].ToString());
foreach (RemoteFileInfo fileInfo in directory.Files)
{
Console.WriteLine("{0} with size {1}, permissions {2} and last modification at {3}",
fileInfo.Name, fileInfo.Length, fileInfo.FilePermissions, fileInfo.LastWriteTime);


string fileName = fileInfo.Name;
string remotePath = ConfigurationManager.AppSettings["RemotePath"].ToString() + fileName;
// string localPath = "d:\\backup\\" + fileName;
string localPath = ConfigurationManager.AppSettings["LocalPath"].ToString() + fileName;


if (session.FileExists(remotePath))
{
bool download;
if (!File.Exists(localPath))
{
Console.WriteLine("File {0} exists, local backup {1} does not", remotePath, localPath);
download = true;
}
else
{
DateTime remoteWriteTime = session.GetFileInfo(remotePath).LastWriteTime;
DateTime localWriteTime = File.GetLastWriteTime(localPath);


download = true;
}

if (download)
{
// Download the file and throw on any error
session.GetFiles(remotePath, localPath).Check();

Console.WriteLine("Download to backup done.");
}
}
else
{
Console.WriteLine("File {0} does not exist yet", remotePath);
}
}
}

return 0;
}
catch (Exception e)
{
Console.WriteLine("Error: {0}", e);
return 1;
}
}
}
}

Reply with quote

Advertisement

You can post new topics in this forum