Friday, September 17, 2010

Copy complete folder with respective folder hierarchy to another location

public class FileSystem{
// Copy directory structure recursively

public static void copyDirectory(string Src,string Dst){
String[] Files;

if(Dst[Dst.Length-1]!=Path.DirectorySeparatorChar)
Dst+=Path.DirectorySeparatorChar;
if(!Directory.Exists(Dst)) Directory.CreateDirectory(Dst);
Files=Directory.GetFileSystemEntries(Src);
foreach(string Element in Files){
// Sub directories

if(Directory.Exists(Element))
copyDirectory(Element,Dst+Path.GetFileName(Element));
// Files in directory

else
File.Copy(Element,Dst+Path.GetFileName(Element),true);
}
}

}

Thursday, September 2, 2010

How to write XML File from the Object Collection

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

public partial class WriteProductXMLFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
WriteProducts("9955");
}
}

public System.Collections.IEnumerable CreateProducts()
{
List products = new List();


products.Add(Product.Create("1", "AAAA"));
products.Add(Product.Create("2", "BBBB"));
products.Add(Product.Create("3", "JJJJJ"));
products.Add(Product.Create("4", "AAAKKKA"));
products.Add(Product.Create("5", "YYYYY"));
products.Add(Product.Create("6", "PPPPP"));
products.Add(Product.Create("7", "MMMMM"));
products.Add(Product.Create("8", "TTTTT"));


return products;

}


protected void WriteProducts(string CustomerID)
{

System.Collections.IEnumerable products = CreateProducts();

string FileName = "Products_" + CustomerID+".xml";

string FilePath = Server.MapPath(FileName);

if (System.IO.File.Exists(FilePath))
{
System.IO.File.Delete(FilePath);
}

XmlTextWriter XmlWriter = new XmlTextWriter(FilePath, null);

XmlWriter.WriteStartDocument();
XmlWriter.WriteComment("Product XML.");
XmlWriter.WriteStartElement("Products of Customer: ID");

foreach (Product prod in products)
{

XmlWriter.WriteStartElement("Product");
XmlWriter.WriteElementString("ProductID", prod.ProductID);
XmlWriter.WriteElementString("ProductImage", prod.ProductImage);
XmlWriter.WriteEndElement();
}


XmlWriter.WriteEndDocument();
XmlWriter.Flush();
XmlWriter.Close();

}


# region Internal Class


public class Product
{
public static Product Create(string productID,
string productImage)
{
Product product = new Product();

product.ProductID = productID;
product.ProductImage = productImage;

return product;
}

private string _productID;

public string ProductID
{
get { return _productID; }
set { _productID = value; }
}

private string _Image;
public string ProductImage
{
get { return _Image; }
set { _Image = value; }
}

}

#endregion



}

Wednesday, August 25, 2010

Extract SubString From Delimitered Chars

public string ExtractSubStringFromDelimiterChar(string strValue, char Char1, char Char2)
{
if (string.IsNullOrEmpty(strValue)) {
return strValue;

} else {
Int16 iStart = strValue.IndexOf(Char1);
Int16 iEnd = strValue.LastIndexOf(Char2);

if ((iStart >= 0 & iEnd > iStart)) {
return strValue.Substring(iStart + 1, (iEnd) - (iStart + 1));
} else {
return strValue;


}
}

}


VB.Net

Public Function ExtractSubStringFromDelimiterChar(ByVal strValue As String, ByVal Char1 As Char, ByVal Char2 As Char) As String
If String.IsNullOrEmpty(strValue) Then
Return strValue
Else

Dim iStart As Int16 = strValue.IndexOf(Char1)
Dim iEnd As Int16 = strValue.LastIndexOf(Char2)

If (iStart >= 0 And iEnd > iStart) Then
Return strValue.Substring(iStart + 1, (iEnd) - (iStart + 1))
Else
Return strValue


End If
End If

End Function

Extract SubString From the Delimitered String

public string ExtractSubStringFromDelimiterString(string strValue, string s1, string s2)
{
if (string.IsNullOrEmpty(strValue)) {
return strValue;

} else {
Int16 iStart = strValue.IndexOf(s1);
Int16 iEnd = strValue.LastIndexOf(s2);

if ((iStart >= 0 & iEnd > iStart)) {
return strValue.Substring(iStart + 1, (iEnd) - (iStart + 1));
} else {
return strValue;


}
}

}


VB.Net

Public Function ExtractSubStringFromDelimiterString(ByVal strValue As String, ByVal s1 As String, ByVal s2 As String) As String
If String.IsNullOrEmpty(strValue) Then
Return strValue
Else

Dim iStart As Int16 = strValue.IndexOf(s1)
Dim iEnd As Int16 = strValue.LastIndexOf(s2)

If (iStart >= 0 And iEnd > iStart) Then
Return strValue.Substring(iStart + 1, (iEnd) - (iStart + 1))
Else
Return strValue


End If
End If

End Function

Tuesday, August 24, 2010

Get File Attributes of File residing on FTP Server

private static FTPFileMetaData GetMetaDataFTPFile(string ftpServerIP, string ftpUserID, string ftpPassword, string strFolderName, string strFileName)
{
FTPFileMetaData metaData = new FTPFileMetaData();
FtpWebRequest reqFTP;


long fileSize = 0;

if (!ftpServerIP.Contains("ftp://"))
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
ftpServerIP + "/" + strFolderName + "/" + strFileName));
}
else
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP +
"/" + strFolderName + "/" + strFileName));
}
reqFTP.Method = WebRequestMethods.Ftp.GetDateTimestamp;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID,
ftpPassword);
FtpWebResponse response;
try
{
response = (FtpWebResponse)reqFTP.GetResponse();

DateTime lastModified = response.LastModified;

metaData.FileName = strFileName;
metaData.ModifiedTime = lastModified;
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;
metaData.FileLength = fileSize;
metaData.FileLength = GetFTPFileSize(ftpServerIP, ftpUserID, ftpPassword, strFolderName, strFileName);

return metaData;
}
catch(Exception ex)
{
BAL.WriteLog("Error in FTPModule for MetaData " + ex.ToString() + "-" + DateTime.Now);
return metaData;
}
}

Get the File List of Files on FTP Server

public static FTPFileMetaData GetFTPFileList(string ftpServerIP, string ftpUserID, string ftpPassword, string strFolderName, string FileName)
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
FtpWebRequest reqFTP;
FTPFileMetaData metaDataOfReFile = new FTPFileMetaData();
try
{
if (!ftpServerIP.Contains("ftp://"))
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" +
ftpServerIP + "/" + strFolderName + "/" + FileName));
}
else
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(ftpServerIP +
"/" + strFolderName + "/" + FileName ));
}
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
WebResponse response = reqFTP.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
//MessageBox.Show(reader.ReadToEnd());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
result.Remove(result.ToString().LastIndexOf('\n'), 1);
reader.Close();
response.Close();
//MessageBox.Show(response.StatusDescription);
downloadFiles = result.ToString().Split('\n');


foreach (string s in downloadFiles)
{
metaDataOfReFile = GetMetaDataFTPFile(ftpServerIP, ftpUserID, ftpPassword, strFolderName, s);


#if (Diagnostics)

BAL.WriteTrackLog("Fetching meta data of FTP File in Re-DownloadMode - '" + s + "'" + " " + DateTime.Now);

#endif
}

return metaDataOfReFile;
}
catch (Exception ex)
{
BAL.WriteLog("Error in FTPModule for FTPList for ReDownload" + ex.ToString() + "-" + DateTime.Now);
return metaDataOfReFile;
}
}

Get the File Size of File residing on FTP Server

private static long GetFTPFileSize(string ftpServerIP, string ftpUserID, string ftpPassword, string strFolderName, string strFileName)
{
FtpWebRequest reqFTP;
long fileSize = 0;
try
{
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + strFolderName + "/" + strFileName));
reqFTP.Method = WebRequestMethods.Ftp.GetFileSize;
reqFTP.UseBinary = true;
reqFTP.KeepAlive = false;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream ftpStream = response.GetResponseStream();
fileSize = response.ContentLength;

ftpStream.Close();
response.Close();
}
catch (Exception ex)
{
BAL.WriteLog("Error in FTPModule for FileSize " + ex.ToString() + "-" + DateTime.Now);
}
return fileSize;
}


}
 
Locations of visitors to this page