h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Upload Multiple files into FTP Server using C#.net

Upload Multiple files into FTP Server using C#.Net

public static bool MultipleUploadFiles(string[] fileName, int candidateId, int resumeId)
{
try
{
string parentDir = string.Empty;
string candir = string.Empty;
string desPath = string.Empty;
foreach (string fName in fileName)
{
FileInfo objfileInfo = new FileInfo(fName);
parentDir = ClsConfigurations.ReadSetting("Folder");
candir = @"/" + parentDir + "/" + Convert.ToString(candidateId);
desPath = candir + "-" + Convert.ToString(resumeId) + objfileInfo.Extension;
CreateDirectory(candir);
objFTPCon.ServerDirectory = candir.Trim();
objFTPCon.UploadFile(fName, desPath);
}
return true;
}
catch (Exception ex)
{
Program.WriteLog(ex.Message, ex.StackTrace);
return false;
}
}

Display file from FTP Server

Display file from FTP Server
public static void DownloadFromFTP(string sourceFile, string destinationFile,RichTextBox rtxtJResume)
{
if (IsFTPConnected())
{
string partnet_Dir = string.Empty;
partnet_Dir = ClsConfigurations.ReadSetting("Folder");
string[] id = sourceFile.Split('-');
objFTPCon.ServerDirectory = @"/" + partnet_Dir + "/" + id[0];
objFTPCon.DownloadFile(destinationFile, sourceFile);
FileInfo fi = new FileInfo(destinationFile);
string file = System.Windows.Forms.Application.StartupPath + "\\temp" + fi.Extension.ToLower();
string text = ClsFile.ExtractTextFromFile(file);
File.Delete(System.Windows.Forms.Application.StartupPath + "\\temp" + fi.Extension.ToLower());
rtxtJResume.Text = text;
}
}

Connect FTP Server using C#.net

Check FTP Connection using C#.Net
 public static bool IsFTPConnected()  
     {  
           objFTPCon = new FTPConnection();  
           objFTPCon.ServerAddress = "Server address"  
           objFTPCon.UserName = "User Name"  
           objFTPCon.Password = "Password"  
           objFTPCon.ServerDirectory = "directory name"  
           if (objFTPCon.ServerAddress != "" && objFTPCon.UserName != "" && objFTPCon.Password != "")  
           {  
             objFTPCon.Login();  
             objFTPCon.Connect();  
             return true;  
           }  
           else  
           {  
             MessageBox.Show("Please Fill FTP Server Connection", "Staffing Solution");  
             return false;  
           }  
     }  

Create Directory in FTP using C#.Net

Today , we will discuss about FTP using C#.Net.

FTP Component :EnterpriseDT.Net.Ftp
 public static FTPConnection objFTPCon = new FTPConnection();  
  private static void CreateDirectory(string dirName)  
     {  
       if (!objFTPCon.DirectoryExists(dirName))  
         objFTPCon.CreateDirectory(dirName);  
     }  

Use of a multicast delegate

A multicast delegate may be used to call more than one method.

Remove unused using in VS 2008

Open Class File
Edit>>Intellisense >> Organise Usings >> Remove unused usings

Data Table Function for LINQToSQL

public static DataTable AsDataTable<T>(IEnumerable<T> varlist)
{
DataTable dtReturn = new DataTable();
// column names
PropertyInfo[] oProps = null;
if (varlist == null) return dtReturn;
foreach (T rec in varlist)
{
// Use reflection to get property names, to create table, Only first time, others will follow if (oProps == null)
{
oProps = ((Type)rec.GetType()).GetProperties();
foreach (PropertyInfo pi in oProps)
{
Type colType = pi.PropertyType;
if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
{
colType = colType.GetGenericArguments()[0];
}
dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
}
}
DataRow dr = dtReturn.NewRow();
foreach (PropertyInfo pi in oProps)
{
dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue
(rec, null);
}
dtReturn.Rows.Add(dr);
}
return dtReturn;
}

Sort using in VS 2008


Open Class File
Edit>>Intellisense >> Organise Usings >> Sort Usings

Business Validation via Keypress Event in C#.Net

Today we will discuss about Display Form Icon in the Menu Strip of MDI Form
public static void BOValidation(KeyPressEventValidation mode, KeyPressEventArgs e, object sender)
{
switch (mode)
{
case KeyPressEventValidation.Alphabets:
if (!char.IsLetter(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32)
e.Handled = true;
break;
case KeyPressEventValidation.AlphabetValidation:
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && e.KeyChar != 32)
e.Handled = true;
break;
case KeyPressEventValidation.AlphaNumeric:
if (!char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32 && e.KeyChar != 45)
e.Handled = true;
break;
case KeyPressEventValidation.CompanyName:
if (!char.IsLetter(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 8 && e.KeyChar != 32 && e.KeyChar != 38 && e.KeyChar != 45 && e.KeyChar != 40 && e.KeyChar != 46 && e.KeyChar != 40)
e.Handled = true;
break;
case KeyPressEventValidation.DecimalValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.') e.Handled = true;
// To allow only one decimal point if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1 )
e.Handled = true;
break; case KeyPressEventValidation.FilePathvalidation:
if ((e.KeyChar >= 48 && e.KeyChar <= 57) && (e.KeyChar >= 65 && e.KeyChar <= 122) && e.KeyChar == 92)
e.Handled = true;
break;
case KeyPressEventValidation.IntegerValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
e.Handled = true;
break;
case KeyPressEventValidation.PhoneValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != 32 && e.KeyChar != 40 && e.KeyChar != 41 && e.KeyChar != 43 && e.KeyChar != 45)
e.Handled = true;
break;
case KeyPressEventValidation.ValidateSpecialCharacters:
if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar))
e.Handled = true;
break;
case KeyPressEventValidation.PercentageValidation:
if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
{
e.Handled = true;
}
// only allow one decimal point
if (e.KeyChar == '.' && (sender as TextBox).Text.IndexOf('.') > -1)
{
e.Handled = true;
}
if (!char.IsControl(e.KeyChar))
{
TextBox textBox = (TextBox)sender;
if (textBox.Text.IndexOf('.') > -1 && textBox.Text.Substring(textBox.Text.IndexOf('.')).Length >= 3)
{
e.Handled = true;
}
}
break;
}
}

Clear Rich Text Box Data in C#.net

public static void ClearRichTextBox(Control parent)
{
foreach (Control child in parent.Controls)
{
RichTextBox rtxtBox = child as RichTextBox;
if (rtxtBox == null)
ClearRichTextBox(child);
else
rtxtBox.Text = string.Empty;
}
}