ذخیره کردن هر نوع فایلی در متغیر byte

byte[] fd;

OpenFileDialog o = new OpenFileDialog();

if (o.ShowDialog() != DialogResult.Cancel)

{

    filename = o.FileName;

    FileStream st = new FileStream(filename, FileMode.Open, FileAccess.Read);

    FileInfo fi = new FileInfo(filename);

    fd = new byte[fi.Length];

    st.Read(fd, 0, (int)fi.Length);

    st.Close();

}

تابع معکوس یک رشته

public static string ReverseString(string s)

        {

            char[] arr = s.ToCharArray();

            Array.Reverse(arr);

            return new string(arr);

        }

textbox با قابلیت autocomplet

private void  main_Load(object sender, EventArgs e)
        {
            txtname.AutoCompleteMode = AutoCompleteMode.Suggest;                            
            txtname.AutoCompleteSource = AutoCompleteSource.CustomSource;                   
            AutoCompleteStringCollection DataCollection = new AutoCompleteStringCollection();
            Items(DataCollection);                                                        
            txtname.AutoCompleteCustomSource = DataCollection;                              
        }



        public void Items(AutoCompleteStringCollection Strings)
        {                                                         
            Strings.Add("azhman");                                
            Strings.Add("azhmancode");                                 
            Strings.Add("mohsen");                                  
            Strings.Add("behrooz");                                
            Strings.Add("rahmani");                               
            Strings.Add("moradi");                                   
        }

وسط چین کردن فرم

 

private void Form1_Load(object sender, System.EventArgs e)

{

    Graphics graphics = this.CreateGraphics();

    double startingPoint = (this<.Width / 2) - (graphics.MeasureString(this.Text.Trim(),this.Font).Width / 2);

double widthOfASpace = graphics.MeasureString(" ", this.Font).Width;

    temp = " ";

    double tempWidth = 0;

    while ((tempWidth + widthOfASpace)  startingPoint)

    {

        temp += " ";

        tempWidth += widthOfASpace;

    }

    this.Text = temp + this.Text.Trim();

}

چاپ لوزی توسط حلقه های تودرتو

 

using System;

class program

{

    public static void Main()

    {

        int number, i, k, count = 1;

        Console.Write("Enter number of rows\n");

        number = int.Parse(Console.ReadLine());

        count = number - 1;

        for (k = 1; k &amp;amp;lt;= number; k++)

        {

            for (i = 1; i &amp;amp;lt;= count; i++)

                Console.Write(" ");

            count--;

            for (i = 1; i &amp;amp;lt;= 2 * k - 1; i++)

                Console.Write("*");

            Console.WriteLine();

        }

        count = 1;

        for (k = 1; k &amp;amp;lt;= number - 1; k++)

        {

            for (i = 1; i &amp;amp;lt;= count; i++)

                Console.Write(" ");

            count++;

            for (i = 1; i &amp;amp;lt;= 2 * (number - k) - 1; i++)

                Console.Write("*");

            Console.WriteLine();

        }

        Console.ReadLine();

    }

}

تولید N عدد تصادفی بدونه تکرار – با استفاده از HashSet

public static int[] RandomNumbers(int n,int min,int max)

{

    Random rnum = new Random();

    HashSet hset = new HashSet();

    while (hset.Count < n)

        hset.Add(rnum.Next(min,max));

    int[] OutPut = hset.ToArray();

    return OutPut;

}

بدست آوردن میزان مصرف cpu

public class CpuUsing    {

    private readonly PerformanceCounter _cpuUsing = new PerformanceCounter();

    public double Progress()

    {

        _cpuUsing.CategoryName = "Processor";

        _cpuUsing.CounterName = "% Processor Time";

        return _cpuUsing.NextValue();

    }

}

طریقه استفاده
باید قطعه کد زیر در رویداد کنترل تایمر نوشته شود و پروپرتی تایمر ترو شود

label1.Text = "میزان مصرف پردازنده : " + new CpuUsing().Progress()  + " % ";

 
  BLOGFA.COM