Various Scripts and Application Code Segments for .NET, VB, C#, C++, C, Java, JavaScript, HTML, Python, Perl, AutoIT, Batch, ASP Classic, Objective-C, Swift, Unreal Engine 4, Unity3D & others. Also contains numerous IT tidbits, procedures, and tricks including Technology Hacks on various platforms.
Sunday, December 26, 2010
Simple FTP List Example in C#
I've integrated FTP Listing capabilities into my MobileExec Program (screenshot above). C# has robust FTP capabilities using FTPWebRequest Classes, check out the simple code below. This uses System.Net and System.Sockets. First declare the class, then the usage is below that for the method.
public class FTPApp
{
public string[] GetFileList(string UID, string PWD, string Server)
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + Server + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(UID, PWD);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
return result.ToString().Split('\n');
}
catch (Exception ex)
{
//System.Windows.Forms.MessageBox.Show(ex.Message);
downloadFiles = null;
return downloadFiles;
}
}
}
//Usage Method Below
public void GetFTPList(string Server,
string UID, string PWD)
{
Label1.Text = "FTP Listing for <b>" + Server + " </b><br />";
Label1.Text += "--------------------------------------------------- <br />";
FTPApp FTPA = new FTPApp();
string[] FTPFileList;
FTPFileList = FTPA.GetFileList(UID, PWD, Server);
foreach (string x in FTPFileList)
{
Label1.Text += "<a href='" + "ftp://" +
UID + ":" + PWD + "@" +
Server + "/" + x + "'><img src='images/dl.png'></a>" + x + "<br />";
}
}
Subscribe to:
Post Comments (Atom)
Generating "Always On Top" NSWindow in macOS across all detected displays
Also: Using UIKit & Cocoa Frameworks using Objective-C In m acOS or OS X , written in either Objective-C or Swift Langues, you m...
-
In Unreal Engine 4 you will eventually need Linear Interpolation between two values to do something like ping pong between two float val...
-
Recently Possess () has been deprecated from UE4 , and when writing classes based on AAIController you have to use the function OnPossess ...
-
Often we intermingle C++ and Blueprints, and need for the two to communicate. With Behavior Trees, using ENUMs is an everyday occurrence an...
No comments:
Post a Comment