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

Set File Readonly in C#.Net

Call this Code for Set File ReadOnly Mode using C#.Net:
public void SetReadOnly(string fileName)
{
FileInfo fi = new FileInfo(fileName);
fi.IsReadOnly = true;
}

Create INI File in C#.Net

Today , we will discuss about Create,Read and Write INI File in C#.Net


Create a Class file for INI File Operation

Class file name : INIOperation.cs
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Security.AccessControl;
using System.Text;
 #region Declarations
        private string path = string.Empty;
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
        [DllImport("kernel32")]
        private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
        int count = 0;
        private static string _SDevice = string.Empty;
string GetInputPath  =string.Empty;
string GetOutputPath =string.Empty;
        #endregion


     #region Constructor
        /// <summary>
        /// INI Operation
        /// </summary>
        /// <param name="INIPath">Pass INI Path</param>
        public INIOperation(string INIPath)
        {
            if (!File.Exists(INIPath))
            {
                path = INIPath;
            }
            else
            {
                File.SetAttributes(INIPath, FileAttributes.Normal);
                File.Delete(INIPath);
                path = INIPath;
            }
        }
        /// <summary>
        /// INI OPperation
        /// </summary>
        /// <param name="INIPath">Pass INI Path</param>
        /// <param name="mode">Pass Mode [Read]</param>
        public INIOperation(string INIPath, string mode)
        {
            if (mode == "Read")
            {
                path = INIPath;
            }
        }
        #endregion Constructor

        #region Functions/Methods    
        /// <summary>
        /// Write Data to the INI File
        /// </summary>
        /// <PARAM name="Section"></PARAM>
        /// Section name
        /// <PARAM name="Key"></PARAM>
        /// Key Name
        /// <PARAM name="Value"></PARAM>
        /// Value Name
        public void IniWriteValue(string Section, string Key, string Value)
        {
            WritePrivateProfileString(Section, Key, Value, this.path);
        }
        /// <summary>
        /// Read Data Value From the Ini File
        /// </summary>
        /// <PARAM name="Section"></PARAM>
        /// <PARAM name="Key"></PARAM>
        /// <PARAM name="Path"></PARAM>
        /// <returns></returns>
        public string IniReadValue(string Section, string Key)
        {
            StringBuilder temp = new StringBuilder(255);
            int i = GetPrivateProfileString(Section, Key, "", temp, 255, this.path);
            return temp.ToString();
        }



   /// <summary>
        /// Default INI File
        /// </summary>
        public void DefaultINI()
        {
            IniWriteValue("InputPath", "Input", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\EasyconWS\PCL");
            IniWriteValue("OutputPath", "Output", Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\EasyconWS\Data");
       
        }
        /// <summary>
        /// Read Ini File
        /// </summary>
        public void ReadINI()
        {
            GetInputPath = IniReadValue("InputPath", "Input");
            GetOutputPath = IniReadValue("OutputPath", "Output");        
        }

Custom Control vs User Control


S.NoCustom ControlUser Control
1A loosely coupled control w.r.t code and UI.A tightly coupled control w.r.t code and UI.
2Derives from Control.Derives from UserControl.
3Defines UI in a ResourceDictionary.Defines UI as normal XAML.
4UI is skinable.Child controls are skinable.
5Has dynamic layout.Has static layout.
6UI can be changed in different projects.UI is fixed and can't have different looks in different project.
7Has full toolbox support.Can't be added to the toolbox.
8Defines a single control.Defines a set of controls.
9More flexible.Not very flexible like a Custom Control.

Display Cell Value into ListBox in C#.Net

Add  DataGridview,Button  and ListBox in your form.
Create  one method for getting Particular Cell data for listbox:

void DisplayCellDataintoListBox(int cellId)
{ foreach (DataGridViewRow row in dataGridView1.Rows)
{ listBox1.Items.Add(Convert.ToString(row.Cells[cellId].Value));
}
}

Call this method in the Button Click event:
DisplayCellDataintoListBox(<Pass Datagridview CellId>);

Create Text File and Values in C#.net

First Add Class file for CreateTextFile.
  Class file name: CreateTextFile.cs
  #region Declaration
CreateTextFile obj = new CreateTextFile(<Text File PATH>);
obj.AppendText(<Message/Text>);

C# provides us with the IS operator

–returns true if an object isan instance of a type

AS operator attempts to cast an object to a specified type

–returning the instance cast to the type
–null if not possible
–does not raise an exception

COM object that has been separated from its underlying RCW cannot be used

COM object that has been separated from its underlying RCW cannot be used. Error occured when i closed the application in C#.net
How to fix it?


 I am using event watcher in my Application . So I didn't stop when i close the Application ,So I Stop eventWatcher. solved issue

Update auto_increment in SQL Server

Update auto_increment in SQL Server

Create PROC SP_UpdateEmployee @Id int = null out, @employeename varchar(50), @Active bit AS INSERT INTO Employeemaster(employeename,Active)values(@employeename,@Active) Set @Id = Scope_Identity()

Get Count of Unread mail from Outlook Express in C#.Net

Get Count of Unread mail from Outlook Express in C#.Net

 using Microsoft.Office.Interop.Outlook;  
   public static int CountOfUnreadMails()  
     {  
       Application OutlookApp = new Application();  
       NameSpace myMail = OutlookApp.GetNamespace("MAPI");  
       int unread = myMail.GetDefaultFolder(OlDefaultFolders.olFolderInbox).UnReadItemCount;  
      return  unread;  
      }