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


}

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
 
Locations of visitors to this page