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;
}


}

Wednesday, August 18, 2010

Filter DataTable with Select method of DataTable

Private Function GetRowsByBlockIdFilter(ByVal dt As DataTable, ByVal blockId As String) As DataTable

Dim table As DataTable = dt.Copy()
dt.Rows.Clear()

Dim expression As String
expression = "blockId='" + blockId.Trim() + "' "
Dim foundRows() As DataRow

' Use the Select method to find all rows matching the filter.
foundRows = table.Select(expression)

Dim i As Integer
' Print column 0 of each returned row.
For i = 0 To foundRows.GetUpperBound(0)
dt.ImportRow(foundRows(i))

Next i

Return dt
End Function

Tuesday, August 17, 2010

Remove Cache in ASP.net

For Each de As DictionaryEntry In HttpContext.Current.Cache

HttpContext.Current.Cache.Remove(DirectCast(de.Key, String))

Next

Saturday, May 8, 2010

How to get the ContentType of the File

private string GetContentType(string fileName)

{

string contentType = "application/octetstream";

string ext = System.IO.Path.GetExtension(fileName).ToLower();

Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);

if (registryKey != null && registryKey.GetValue("Content Type") != null)

contentType = registryKey.GetValue("Content Type").ToString();

return contentType;

}

Tuesday, March 23, 2010

Recursive_Files_Search_In_CSharp

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Trans829115.BLL
{
///
/// RecursiveFilesSearch from FolderPath/DrivePath
///

///


public class RecursiveFilesSearch
{

static private List FilesFound = new List(); // DirectSearch(Global)

///
///
///

///
public RecursiveFilesSearch()
{

}

private string PaddAsDirectoryPath(string path)
{
if(string.IsNullOrEmpty(path)) return string.Empty;


if(path.Substring((path.Length-2))!="\\")
return path+"\\";

return string.Empty;

}


public enum EnumSourceFilesType
{
CS_Files,
VB_Files
}
///
/// Get All the files in the respective Folder/Drive
///

///
///
///
public List GetAllFileNames(string SourceDir, bool blnIsFileValidator)
{
if(!Directory.Exists(SourceDir))
{
throw new Exception(string.Format("{0} - is not exists - Please define valid Source Path", SourceDir ));
return null;
}

SourceDir = PaddAsDirectoryPath(SourceDir);

List files = new List();

DirectoryInfo di = new DirectoryInfo(SourceDir);

DirectoryInfo[] dirs = di.GetDirectories();

foreach (DirectoryInfo diNext in dirs)
{
files.AddRange(DirSearch(diNext.FullName, "*.*", blnIsFileValidator));
}

return files;

}

///
/// Specific search on SearchType
///

///
///
///

public List GetSelectedFileNames(string SourceDir, EnumSourceFilesType enumSourceType, bool blnIsFileValidator)
{
if(!Directory.Exists(SourceDir))
{
throw new Exception(string.Format("{0} - is not exists - Please define valid Source Path", SourceDir ));
return null;
}

string searchPattern = "*.*"; // Default

switch(enumSourceType)
{
case EnumSourceFilesType.CS_Files:
searchPattern = "*.cs";
break;
case EnumSourceFilesType.VB_Files:
searchPattern = "*.vb";
break;
}

SourceDir = PaddAsDirectoryPath(SourceDir);

List files = new List();

DirectoryInfo di = new DirectoryInfo(SourceDir);

DirectoryInfo[] dirs = di.GetDirectories();

foreach (DirectoryInfo diNext in dirs)
{
files.AddRange(DirSearch(diNext.FullName, searchPattern, blnIsFileValidator));
}

return files;

}



private List DirSearch(string sDir, string SearchPattern)
{

try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, SearchPattern))
{
FilesFound.Add(f);
}
DirSearch(d,SearchPattern);
}

return FilesFound;
}
catch (System.Exception excpt)
{
throw new Exception(excpt.Message);
}
}

///
/// Option to apply FileValidator method
///

///
///
///
///
private List DirSearch(string sDir, string SearchPattern, bool blnIsFileValidator)
{

try
{
foreach (string d in Directory.GetDirectories(sDir))
{
foreach (string f in Directory.GetFiles(d, SearchPattern))
{
if(blnIsFileValidator)
{
if(ValidateFile(f))
FilesFound.Add(f);
}
else
FilesFound.Add(f);
}
DirSearch(d, SearchPattern, blnIsFileValidator);
}

return FilesFound;
}
catch (System.Exception excpt)
{
return FilesFound; // Intermediate results to pass on
throw new Exception(excpt.Message);
}
}
///
/// check Is File is Valid for Copying
///

///
///
private bool ValidateFile(string file)
{
file = Path.GetFileName(file);
if(string.IsNullOrEmpty(file)) return false;

if(file.Equals("Program.cs")) return false;

if(file.Equals("Program.vb")) return false;

if(file.Contains("Designer.cs")) return false;

if(file.Contains("Designer.vb")) return false;

if(file.Equals("AssemblyInfo.cs")) return false;

if(file.Equals("AssemblyInfo.vb")) return false;

return true;
}
}
}
 
Locations of visitors to this page