Last active
May 10, 2017 06:43
-
-
Save genbtc/9a6cd4076c260549ed7b to your computer and use it in GitHub Desktop.
Program for niel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.IO; | |
using System.Net; | |
using Ionic.Zip; | |
namespace niel | |
{ | |
class Program | |
{ | |
string ftp_username = "ftp"; | |
string ftp_password = "123459"; | |
string ftp_remote_host = @"ftp://68.225.10.253"; | |
private static readonly string UserProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | |
private static readonly string UserName = Environment.UserName; | |
private static readonly List<string> contentsDirList = new List<string>(); | |
private static string[] GetEnv() | |
{ | |
//SPECS | |
var envdata = new string[4]; | |
envdata[0] = Environment.OSVersion.ToString(); | |
envdata[1] = Environment.MachineName; | |
envdata[2] = Environment.ProcessorCount.ToString(); | |
envdata[3] = Environment.Is64BitOperatingSystem ? "64-bit" : "32-bit"; | |
return envdata; | |
} | |
private void FTP_Test(object sender, EventArgs e) | |
{ | |
UploadFile("d:\\test.txt", ftp_remote_host + @"/test.txt", ftp_username, ftp_password); | |
} | |
/// <summary> | |
/// Methods to upload file to FTP Server | |
/// </summary> | |
/// <param name="fileName">local source file name</param> | |
/// <param name="uploadPath">Upload FTP path including Host name</param> | |
/// <param name="ftpUser">FTP login username</param> | |
/// <param name="ftpPass">FTP login password</param> | |
/// | |
private static void UploadFile(string fileName, string uploadPath, string ftpUser, string ftpPass) | |
{ | |
System.IO.FileInfo fileInfo = new System.IO.FileInfo(fileName); | |
// Create FtpWebRequest object from the Uri provided | |
System.Net.FtpWebRequest ftpWebRequest = (System.Net.FtpWebRequest)System.Net.WebRequest.Create(new Uri(uploadPath)); | |
// Provide the WebPermission Credintials | |
ftpWebRequest.Credentials = new System.Net.NetworkCredential(ftpUser, ftpPass); | |
// By default KeepAlive is true, where the control connection is not closed | |
// after a command is executed. | |
ftpWebRequest.KeepAlive = false; | |
// set timeout for 20 seconds | |
ftpWebRequest.Timeout = 20000; | |
// Specify the command to be executed. | |
ftpWebRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile; | |
// Specify the data transfer type. | |
ftpWebRequest.UseBinary = true; | |
// Notify the server about the size of the uploaded file | |
ftpWebRequest.ContentLength = fileInfo.Length; | |
// The buffer size is set to 2kb | |
int buffLength = 2048; | |
byte[] buff = new byte[buffLength]; | |
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded | |
System.IO.FileStream fileStream = fileInfo.OpenRead(); | |
try | |
{ | |
// Stream to which the file to be upload is written | |
System.IO.Stream stream = ftpWebRequest.GetRequestStream(); | |
// Read from the file stream 2kb at a time | |
int contentLen = fileStream.Read(buff, 0, buffLength); | |
// Till Stream content ends | |
while (contentLen != 0) | |
{ | |
// Write Content from the file stream to the FTP Upload Stream | |
stream.Write(buff, 0, contentLen); | |
contentLen = fileStream.Read(buff, 0, buffLength); | |
} | |
// Close the file stream and the Request Stream | |
stream.Close(); | |
stream.Dispose(); | |
fileStream.Close(); | |
fileStream.Dispose(); | |
Console.WriteLine("Done"); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message + " - Upload Error"); | |
} | |
} | |
public static void FileBackup() | |
{ | |
string[] filters = { "*.jpg", "*.png", "*.gif", ".pdf" }; | |
string[] filePaths = filters.SelectMany(f => Directory.GetFiles(UserProfile, f)).ToArray(); | |
using (ZipFile zip = new ZipFile()) | |
{ | |
zip.AddFiles(filePaths,true,""); | |
zip.Save("MyZipFile.zip"); | |
} | |
} | |
static void Main(string[] args) | |
{ | |
//FileBackup(); | |
foreach (string envinfo in GetEnv()) | |
{ | |
Console.WriteLine(envinfo); | |
} | |
Console.ReadLine(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment