1. مهمان گرامی، جهت ارسال پست، دانلود و سایر امکانات ویژه کاربران عضو، ثبت نام کنید.
    بستن اطلاعیه

كدهاي به درد بخور سي شارپ

شروع موضوع توسط minaaa ‏24/10/11 در انجمن C #C++

  1. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    ارسال عكس به ديتا بيس
    كد:
    HTML:
    try
    **
    MemoryStream ms = new  MemoryStream();
    pictureBox1.Image.Save(ms,  pictureBox1.Image.RawFormat);
    byte[] arrImage =  ms.GetBuffer();
    ms.Close();
    
    SqlConnection con = new  SqlConnection("server=(local);trusted_connection=y  es;database=DbName;");
    string strSQL = "INSERT INTO TabeName  (filename,pic,id) VALUES ( @filename, @pic,@id)";
    SqlCommand cmd = new  SqlCommand(strSQL, con);
    
    cmd.Parameters.Add(new SqlParameter("@filename",  SqlDbType.Char, 50)).Value = textBox2.Text;//filename
    cmd.Parameters.Add(new  SqlParameter("@pic", SqlDbType.Binary)).Value =  arrImage;//picture
    cmd.Parameters.Add(new SqlParameter("@id", SqlDbType.Char,  50)).Value =  textBox3.Text;//id
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
    
    MessageBox.Show("Insert  completed seccessfully.");;
    **
    catch (Exception  ex)
    **
    MessageBox.Show(ex.Message);
    **
     
  2. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    به دست آوردن آدرسهای تایپ شده در اینترنت اکسپلورر ابتدا NameSpace هاي زير را وارد مي كنيم
    كد:
    HTML:
    using System.Collections.Generic;
    using  Microsoft.Win32;
    سپس تابع زير را استفاده مي كنيم
    كد:
    HTML:
    public List<string>  PopulateUrlList()
    **
    //list the main registry key to open
    String regKey  = "Software\\Microsoft\\Internet Explorer\\TypedURLs";
    RegistryKey subKey =  Registry.CurrentUser.OpenSubKey(regKey);
    String url;
    //create a list to  hold our URL's
    List<string> urlList = new List<string>();
    int  counter = 1;
    while (true)
    **
    //grab the current value, in the registry  the key name
    //is url1, url2, url3, etc, while the value is the
    //actual  url
    String sValName = "url" + counter.ToString();
    url =  (String)subKey.GetValue(sValName);
    if ((object)url ==  null)
    break;
    urlList.Add(url);
    counter++;
    **
    return  urlList;
    **
     
  3. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    قراردادن متن بر روی عکس ( Watermark) ابتدا با دستور زیر عک عکس را لود می کنیم
    HTML:
    string  fileloc = oFileDlg.FileName;
    //load image to picturebox
    pictureBox1.Image  = Image.FromFile(fileloc);
    كد:

    سپس کد زیر را در یک رویداد قرار می دهیم
    كد:
    HTML:
    if  (pictureBox1.Image != null)
    **
    // Create image.
    Image tmp =  pictureBox1.Image;
    // Create graphics object for alteration.
    Graphics g =  Graphics.FromImage(tmp);
    
    // Create string to draw.
    String wmString =  "Mdi Sample";
    // Create font and brush.
    Font wmFont = new Font("Trebuchet  MS", 10);
    SolidBrush wmBrush = new SolidBrush(Color.White);
    // Create  point for upper-left corner of drawing.
    PointF wmPoint = new PointF(10.0F,  10.0F);
    // Draw string to image.
    g.DrawString(wmString, wmFont, wmBrush,  wmPoint);
    //Load the new image to picturebox 
    pictureBox1.Image = tmp;
    // Release  graphics object.
    g.Dispose();
    **
     
  4. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    جستجو یک رشته در یک فایل متنی
    كد:
    HTML:
     bool searchtextfile(string fileadd, string regMatch) **  bool find = false; StreamReader testTxt = new StreamReader(fileadd); string  allRead = testTxt.ReadToEnd(); testTxt.Close(); if (Regex.IsMatch(allRead,  regMatch)) ** find = true; } else ** find = false; } }[/HTML[/LEFT]
    [/FONT][/COLOR][/COLOR]
     
  5. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    خالی کردن کش اینترنت اکسپلورر(emety cach folder)
    كد:
    HTML:
     private void EmptyCacheFolder(DirectoryInfo  folder)
    **
    //loop through all the files in the folder provided
    foreach  (FileInfo file in folder.GetFiles())
    **
    file.Delete();
    }
    foreach  (DirectoryInfo subfolder in folder.GetDirectories())
    **
    //recursively  delete all files and folders in each sub  directory
    EmptyCacheFolder(subfolder);
    }
    }
    public bool  ClearCache()
    **
    //variable to hold our status
    bool  isEmpty;
    try
    **
    //call EmptyCacheFolder passing the default internet  cache
    //folder
    EmptyCacheFolder(new  DirectoryInfo(Environment.GetFolderPath(Environmen  t.SpecialFolder.InternetCache)));
    isEmpty =  true;
    }
    catch
    **
    isEmpty = false;
    }
    return  isEmpty;
    }
     
  6. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    // array of integers to hold values
    private int[] a = new int[100];
    private int[] b = new int[100];

    // number of elements in array
    private int x;

    // Merge Sort Algorithm
    public void sortArray()
    **
    m_sort( 0, x-1 );
    }

    public void m_sort( int left, int right )
    **
    int mid;

    if( right > left )
    **
    mid = ( right + left ) / 2;
    m_sort( left, mid );
    m_sort( mid+1, right );

    merge( left, mid+1, right );
    }
    }

    public void merge( int left, int mid, int right )
    **
    int i, left_end, num_elements, tmp_pos;

    left_end = mid - 1;
    tmp_pos = left;
    num_elements = right - left + 1;

    while( (left <= left_end) && (mid <= right) )
    **
    if( a[left] <= a[mid] )
    **
    b[tmp_pos] = a[left];
    tmp_pos = tmp_pos + 1;
    left = left +1;
    }
    else
    **
    b[tmp_pos] = a[mid];
    tmp_pos = tmp_pos + 1;
    mid = mid + 1;
    }
    }

    while( left <= left_end )
    **
    b[tmp_pos] = a[left];
    left = left + 1;
    tmp_pos = tmp_pos + 1;
    }

    while( mid <= right )
    **
    b[tmp_pos] = a[mid];
    mid = mid + 1;
    tmp_pos = tmp_pos + 1;
    }

    for( i = 0; i < num_elements; i++ )
    **
    a[right] = b[right];
    right = right - 1;
    }
    }
     
  7. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    شمارش تعداد تکرار یک کلمه در رشته
    كد:
    HTML:
     int GetFreq(string word)
    **
    string st =  null;
    st = 'StringThatContainWord';
    int start = 0, count = 0, curr =  0;
    int last = st.LastIndexOf(word);
    do
    **
    curr = st.IndexOf(word,  start);
    if (curr != -1)
    **
    count++;
    start = curr +  1;
    }
    }
    while (last != curr);
    return count;
    }
     
  8. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    جدا کردن یک رشته سه قسمتی این تابع یک رشته که از سه قسمت تشکیل شده و بین هر دو قسمت کاراکتر "/" قرار دارد را از هم جدا کرده و در سه متغیر قرار می دهد.
    كد:
    HTML:
     void GetSubString(string String, ref  string S1, ref string S2, ref string S3)
    **
    int first =  String.IndexOf('/');
    S3 = record.Substring(0, first));
    int last =  String.LastIndexOf('/');
    if (last > first)
    **
    S2 =  String.Substring(first + 1, last - first - 1);
    S3 = String.Substring(last +  1);
    }
    else
    **
    S2 = record.Substring(last +  1);
    }
    }
     
  9. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    دریافت یک آرگمان ورودی در حالت Command Get command line arguments
    با استفاده از این کد شما می توانید در هنگام اجرای برنامه با آن مقدار اولیه بدهید
    كد:
    HTML:
     public static void Main(string[]  args)
    **
    if (args.Length >  0)
    **
    System.Console.WriteLine(args[0]);
    try
    **
    // long number =  Int64.Parse(args[0]);
    // long number = Convert.ToInt64(s);
    long number =  long.Parse(args[0]);
    System.Console.WriteLine("Number = " +  number);
    }
    catch (System.FormatException  e)
    **
    System.Console.WriteLine("FormatException" +  e);
    }
    return;
    }
    System.Console.WriteLine("Please enter a  number");
    }
     
  10. کاربر پیشرفته

    تاریخ عضویت:
    ‏9/12/10
    ارسال ها:
    19,795
    تشکر شده:
    6,456
    امتیاز دستاورد:
    113
    پاسخ : كدهاي به درد بخور سي شارپ

    به دست آوردن اندازه تصویر Retrieve the current screen resolution
    برای به دست آوردن اندازه تصویر مانیتور اصلی
    كد:
    HTML:
    MessageBox.Show("Monitor Size:" +  SystemInformation.PrimaryMonitorSize);