C# .NET Writing To The Registry

By alice johnson
Access over 40 UI widgets with everything from interactive menus to rich charts.

In this article we can edit the registry to block a outlook configured email using c#.net.

A checklistbox is required to display all the configured email of the outlook, through which we can select the email id to be blocked:
All the email id are displayed by readsing from registry.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;
using System.Collections;

namespace ResumeParse
{
    public partial class Block : Form
    {
       
        ArrayList l1 = new ArrayList();
        ArrayList l2 = new ArrayList();
        ArrayList l3 = new ArrayList();
        //string j;
        Byte[] y;
        char[] c2;
        public static int ii = 0;
        DataSet ds = new DataSet();
        public Block()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Add All Emial ID's;

            RegistryKey reg = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows messaging Subsystem\\Profiles\\Outlook", true);

            string[] d = reg.GetSubKeyNames();
            int uu = 0;
            foreach (object obj in d)
            {

                uu++;

                l1.Add(obj);

            }


            foreach (object o1 in l1)
            {

 

                string s2 = "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Windows messaging Subsystem\\Profiles\\Outlook\\" + o1.ToString();

                RegistryKey reg2 = Registry.CurrentUser.OpenSubKey(s2, true);
                string[] d1 = reg2.GetSubKeyNames();
                if (reg2.ValueCount > 0)
                {
                    char[] c1 = new char[1000];
                    foreach (object obj in d1)
                    {
                        string s3 = s2 + "\\"+obj.ToString();

                        RegistryKey rege = Registry.CurrentUser.OpenSubKey(s3, true);
                        if (rege.ValueCount > 0)
                        {
                            y = (Byte[])rege.GetValue("email");
                            // c1.Length = y.Length;
                            if (y != null)
                            {
                                foreach (byte b in y)
                                {
                                    c1[ii] = (char)b;
                                    ii++;
                                }
                                ii = 0;
                                c2 = new char[1000];
                                int k = 0;
                                for (int i = 0; i < c1.Length; i = i + 2)
                                {
                                    c2[k] = c1[i];
                                    k++;
                                }


                                string snr = new string(c2);
                                string a = snr.Replace("\0", "");
                               // string a = c2.ToString();

                                checkedListBox1.Items.Add(a);


                            }
                        }
                    }

                }

            }


           
        
            ds.ReadXml(@"D:\ResumeSettings\Email.xml");
            if (ds.Tables[0].Rows.Count > 0)
            {
                int x = checkedListBox1.Items.Count;
                DataRow[] dr = ds.Tables[0].Select("status like '1'");
                for (int di = 0; di < dr.Length; di++)
                {

                    string email = dr[di].ItemArray.GetValue(0).ToString();
                    for (int i = 0; i < x; i++)
                    {
                        if (email.ToLower().ToString().Equals(checkedListBox1.Items[i].ToString().ToLower().TrimStart().TrimEnd()))
                        {

                            int xx = checkedListBox1.Items.IndexOf(email.ToString());
                            checkedListBox1.SetItemChecked(xx, true);

                        }

                    }

                }
            }

            ds.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CheckedListBox.CheckedItemCollection col = checkedListBox1.CheckedItems;
            string str = "";
            for (int i = 0; i < col.Count; i++)
            {
                str = str + " "+ col[i].ToString()+" ";
            }
         
            ds.ReadXml(@"D:\ResumeSettings\Email.xml");

            if (ds.Tables[0].Rows.Count > 0)
            {
                CheckedListBox.CheckedItemCollection chkitem = checkedListBox1.CheckedItems;
                int x = chkitem.Count;
                DataRow[] dr = ds.Tables[0].Select("status like '1'");
               
            
               
                for (int d = 0; d < dr.Length; d++)
                {
                       ds.Tables[0].Rows.Remove(dr[d]);
                }
            }

            ds.WriteXml(@"D:\ResumeSettings\Email.xml");
           
            ds.Clear();  
           
            ds.ReadXml(@"D:\ResumeSettings\Email.xml");
          
            for (int i = 0; i < col.Count; i++)
            {
               
                ds.Tables[0].Rows.Add(col[i].ToString(), "1");
            }
            ds.WriteXml(@"D:\ResumeSettings\Email.xml");
            MessageBox.Show("You will not receive mails from the selected Email-Id's","ResumeParser");
        }

     
    }
}

     

Popularity  (1397 Views)