Program.cs
--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using Microsoft.Build.BuildEngine;
namespace ExpeditorsUpload
{
class Program
{
private static Boolean Flag = true;
static void Main(string[] args)
{
string DCLfolder = ConfigurationManager.AppSettings["Expeditorsfolder"];
FileInfo[] DCLfiles = new DirectoryInfo(DCLfolder).GetFiles();
foreach (var file in DCLfiles)
{
string fl = file.Name;
if (file.Extension.ToLower() == ".xml")
{
UploadFile(file.FullName);
if (Flag)
{
CreateLogFiles.MessageLog("File " + file.Name + " uploaded to Expeditors");
SendEmail(file.Name);
if (Flag)
{
CreateLogFiles.MessageLog("Email sent for uploaded file");
}
}
}
}
}
//To Check the File in FTP
public static bool CheckIfFtpFileExists(string uri)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
string ftpUserName = ConfigurationManager.AppSettings["FTPuserName"];
string ftpUserPassword = ConfigurationManager.AppSettings["FTPpassword"];
request.Credentials = new NetworkCredential(ftpUserName, ftpUserPassword);
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
// THE FILE EXISTS
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (FtpStatusCode.ActionNotTakenFileUnavailable == response.StatusCode)
{
// THE FILE DOES NOT EXIST
return false;
}
}
return true;
}
//For Uploading Files To FTP
private static void UploadFile(String _Path)
{
FileInfo fileInf = new FileInfo(_Path);
string uri = ConfigurationManager.AppSettings["FTP"] + fileInf.Name;
Boolean ftpAnnonymous = Convert.ToBoolean(ConfigurationManager.AppSettings["FTPanonymous?"]);
string ftpUserName = ConfigurationManager.AppSettings["FTPuserName"];
string ftpUserPassword = ConfigurationManager.AppSettings["FTPpassword"];
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
if (!ftpAnnonymous)
{
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpUserPassword);
}
reqFTP.KeepAlive = false;
FtpWebResponse response1 = null;
try
{
if (CheckIfFtpFileExists(uri))
{
// File Exists
}
else
{
//File Does Not Exists
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
string filename = string.Empty;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
if (response != null)
{
fs.Close();
FileInfo f = new FileInfo(_Path);
string filefullname = f.Name;
string fileextension = f.Extension;
filename = filefullname.Substring(0, filefullname.Length - fileextension.Length);
if (File.Exists(f.DirectoryName + "\\" + filename + ".upl"))
{
File.Delete(f.DirectoryName + "\\" + filename + ".upl");
}
f.MoveTo(Path.ChangeExtension(_Path, ".upl"));
}
}
catch (WebException webex)
{
Flag = false;
CreateLogFiles.MessageLog(webex.Message);
}
catch (Exception exe)
{
Flag = false;
CreateLogFiles.MessageLog(exe.Message);
}
finally
{
fs.Close();
}
}
}
catch (Exception ex)
{
CreateLogFiles.MessageLog(ex.Message);
}
}
//For Sending Email
private static void SendEmail(String Fname)
{
MailMessage mail = new MailMessage();
mail.Subject = "File Uploaded";
mail.From = new MailAddress("thoutamurali@gmail.com");
mail.To.Add("mthouta@adaequare.com");
mail.Body = "Hello! your file has been Uploaded to FTP...";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("thoutamurali@gmail.com", "9390868000");
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
CreateLogFiles.MessageLog(ex.Message);
Flag = false;
}
}
}
}
Web.config:
---------------
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Expeditorsfolder" value="C:\Users\abc\Desktop\xml_documents"/>
<add key="ProcessedPath" value="C:\Users\abc\Desktop\Processed Documents"/>
<add key="FTP" value="ftp://192.168.1.7/Ramu/"/>
<add key="FTPuserName" value="abc"/>
<add key="FTPpassword" value="123"/>
<add key="FTPanonymous?" value="false"/>
<add key="FromEmail" value="abc@gmail.com"/>
<add key="ToEmail" value="abc@gmail.com"/>
<add key="Host" value="smtp.gmail.com"/>
<add key="Port" value="587"/>
<add key="Password" value="1234"/>
<!--<add key ="1" value="1forTria"/>-->
<!--<add key="Logpath" value ="C:\Users\abc\Documents\Test\log\"/>-->
<add key="Logpath" value="D:\Test\log\"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
--------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Net;
using System.Net.Mail;
using Microsoft.Build.BuildEngine;
namespace ExpeditorsUpload
{
class Program
{
private static Boolean Flag = true;
static void Main(string[] args)
{
string DCLfolder = ConfigurationManager.AppSettings["Expeditorsfolder"];
FileInfo[] DCLfiles = new DirectoryInfo(DCLfolder).GetFiles();
foreach (var file in DCLfiles)
{
string fl = file.Name;
if (file.Extension.ToLower() == ".xml")
{
UploadFile(file.FullName);
if (Flag)
{
CreateLogFiles.MessageLog("File " + file.Name + " uploaded to Expeditors");
SendEmail(file.Name);
if (Flag)
{
CreateLogFiles.MessageLog("Email sent for uploaded file");
}
}
}
}
}
//To Check the File in FTP
public static bool CheckIfFtpFileExists(string uri)
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
string ftpUserName = ConfigurationManager.AppSettings["FTPuserName"];
string ftpUserPassword = ConfigurationManager.AppSettings["FTPpassword"];
request.Credentials = new NetworkCredential(ftpUserName, ftpUserPassword);
request.Method = WebRequestMethods.Ftp.GetFileSize;
try
{
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
// THE FILE EXISTS
}
catch (WebException ex)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (FtpStatusCode.ActionNotTakenFileUnavailable == response.StatusCode)
{
// THE FILE DOES NOT EXIST
return false;
}
}
return true;
}
//For Uploading Files To FTP
private static void UploadFile(String _Path)
{
FileInfo fileInf = new FileInfo(_Path);
string uri = ConfigurationManager.AppSettings["FTP"] + fileInf.Name;
Boolean ftpAnnonymous = Convert.ToBoolean(ConfigurationManager.AppSettings["FTPanonymous?"]);
string ftpUserName = ConfigurationManager.AppSettings["FTPuserName"];
string ftpUserPassword = ConfigurationManager.AppSettings["FTPpassword"];
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(uri));
if (!ftpAnnonymous)
{
reqFTP.Credentials = new NetworkCredential(ftpUserName, ftpUserPassword);
}
reqFTP.KeepAlive = false;
FtpWebResponse response1 = null;
try
{
if (CheckIfFtpFileExists(uri))
{
// File Exists
}
else
{
//File Does Not Exists
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
reqFTP.UseBinary = true;
reqFTP.ContentLength = fileInf.Length;
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
string filename = string.Empty;
FileStream fs = fileInf.OpenRead();
try
{
Stream strm = reqFTP.GetRequestStream();
contentLen = fs.Read(buff, 0, buffLength);
while (contentLen != 0)
{
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
strm.Close();
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
if (response != null)
{
fs.Close();
FileInfo f = new FileInfo(_Path);
string filefullname = f.Name;
string fileextension = f.Extension;
filename = filefullname.Substring(0, filefullname.Length - fileextension.Length);
if (File.Exists(f.DirectoryName + "\\" + filename + ".upl"))
{
File.Delete(f.DirectoryName + "\\" + filename + ".upl");
}
f.MoveTo(Path.ChangeExtension(_Path, ".upl"));
}
}
catch (WebException webex)
{
Flag = false;
CreateLogFiles.MessageLog(webex.Message);
}
catch (Exception exe)
{
Flag = false;
CreateLogFiles.MessageLog(exe.Message);
}
finally
{
fs.Close();
}
}
}
catch (Exception ex)
{
CreateLogFiles.MessageLog(ex.Message);
}
}
//For Sending Email
private static void SendEmail(String Fname)
{
MailMessage mail = new MailMessage();
mail.Subject = "File Uploaded";
mail.From = new MailAddress("thoutamurali@gmail.com");
mail.To.Add("mthouta@adaequare.com");
mail.Body = "Hello! your file has been Uploaded to FTP...";
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
smtp.EnableSsl = true;
NetworkCredential netCre = new NetworkCredential("thoutamurali@gmail.com", "9390868000");
smtp.Credentials = netCre;
try
{
smtp.Send(mail);
}
catch (Exception ex)
{
CreateLogFiles.MessageLog(ex.Message);
Flag = false;
}
}
}
}
Web.config:
---------------
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="Expeditorsfolder" value="C:\Users\abc\Desktop\xml_documents"/>
<add key="ProcessedPath" value="C:\Users\abc\Desktop\Processed Documents"/>
<add key="FTP" value="ftp://192.168.1.7/Ramu/"/>
<add key="FTPuserName" value="abc"/>
<add key="FTPpassword" value="123"/>
<add key="FTPanonymous?" value="false"/>
<add key="FromEmail" value="abc@gmail.com"/>
<add key="ToEmail" value="abc@gmail.com"/>
<add key="Host" value="smtp.gmail.com"/>
<add key="Port" value="587"/>
<add key="Password" value="1234"/>
<!--<add key ="1" value="1forTria"/>-->
<!--<add key="Logpath" value ="C:\Users\abc\Documents\Test\log\"/>-->
<add key="Logpath" value="D:\Test\log\"/>
</appSettings>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>
No comments:
Post a Comment