تاپیک جامع نکات در سی شارپ
انتقال آیتمهای یک آرایه از اعداد به یک لیست باکس
کد:
int[] numbers = { 12, 23, 34, 45, 56, 67 };
Object[] oNumbers = new Object[numbers.Length];
numbers.CopyTo(oNumbers, 0);
listBox1.Items.AddRange(oNumbers);
مخفی کردن فایل
کد:
file.Attributes = FileAttributes.Hidden;
برای این که فایل مخفی شده ظاهر شود و از حالت Hidden خارج شود از کد زیر استفاده می کنیم:
کد:
file.Attributes = FileAttributes.Hidden;
گرفتن کد اسکی و یونیکد کاراکتر ها
کد:
String ucode = String.Format("{0:x4}", (int)('a'));
// ucode = 0061
تبدیل به اسکی
String acode = (((int)('a')).ToString());
//acode=97
بدست آوردن لیست تمامی درایوها و نوع آنها
برای این کار روش های زیادی وجود داره که اینم یه روشی هست
کد:
foreach (DriveInfo drv in DriveInfo.GetDrives())
listBox1.Items.Add(drv.Name + ":" + drv.DriveType);
ایجاد رمزهای تصادفی توسط فضای نام System.Security.Cryptography
کد:
public static string CreateRandomEncryptedPassword(int PasswordLen)
{
String allowedChars =
"abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXY Z0123456789";
Byte[] randomBytes = new Byte[PasswordLen];
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
rng.GetBytes(randomBytes);
char[] chars = new char[PasswordLen];
int allowedCharCount = allowedChars.Length;
for (int i = 0; i < PasswordLen; i++)
{
chars[i] = allowedChars[(int)randomBytes[i] % allowedCharCount];
}
return new string(chars);
}
یک Event برای تعدادی کنترل و انجام کارهای مشابه روی آنها
کد:
private void textBox1_Enter(object sender, EventArgs e)
{
TextBox txtbx = sender as TextBox;
txtbx.text.selectall();
}
بدست آوردن لیست تمامی فرمهای باز در یک برنامه
مثال : تغییر رنگ پشت زمینه ی تمامی فرمهای باز در یک برنامه
کد:
foreach (Form frm in Application.OpenForms)
frm.BackColor = Color.Fuchsia;
نمایش تصاویر در Windows picture and fax viewer به روشی ساده تر
این روش خیلی ساده تر هست ، امیدوارم مفید باشه
کد:
System.Diagnostics.Process.Start(@"C:\Windows\system32\MSPaint.exe ", filename);
ساختن یک Connection با استفاده از ConnectionString
کد:
SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrin gs["myConnection"].ConnectionString);
بدست آوردن خروجی دستورهای خط فرمان
کد:
using System.Diagnostics;
کد:
private static string CaptureCommandPromptOutput(string command, string argument)
{
ProcessStartInfo info = new ProcessStartInfo(command, argument);
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.CreateNoWindow = true;
Process p = new Process();
p.StartInfo = info;
p.Start();
return p.StandardOutput.ReadToEnd();
}
شمارش تعداد کلمات یک جمله
کد:
private void btnCount_Click(object sender, EventArgs e)
{
int count = txtSentences.Text.Replace('\t',' ').Trim().Split(new char[] { ' ' },StringSplitOptions.RemoveEmptyEntries).Length;
lblCount.Text = string.Format("تعداد {0} کلمه در جمله وارد شده وجود دارد .", count.ToString());
}
عوض کردن نشانگر ماوس (cursor) روی یک کنترل
کد:
button1.Cursor = Cursors.Hand;
لود کردن یک کرسر سفارشی از فایل :
کد:
button1.Cursor = new Cursor(@"C:\WINDOWS\Cursors\hibeam.cur");
بستن یک پروسه در حال اجرا به روشی ساده تر
کد:
System.Diagnostics.Process.Start("TaskKill", "/f /im Notepad.exe /t");
ایجاد صدا با کلیک بر روی دکمه
کد:
using System.Media;
و در رویداد Button این کد رو باید بنویسید و بعد برنامه رو قبل از اجرا ، build کنید و بعد اجرا کنید
کد:
SoundPlayer player = new SoundPlayer();
string path = "C:\\windows\\media\\ir_end.wav";
player.SoundLocation = path;
player.Play();
خاموش کردن pc
این کد رو در هرجایی از فرمتون میتونید بنویسید ولی پیشنهاد میدم در رویداد یک Button بنویسید و در نهایت فرمتون رو از حالت close خارج کنید و برنامه رو اجرا کنید ، در غیر این صورت برای بستنش از تسک منیجر اقدام کنید
کد:
Process p = newProcess();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
StreamWriter sw = p.StandardInput;
sw.WriteLine("shutdown -s");
sw.Close();
p.Close();
فراخوانی و شروع یک ترد :
کد:
myThread.Start();
متوقف کردن ترد ایجاد شده :
کد:
myThread.Abort();
اتصال فایلهای Wave
برای تبدیل یک رشته به کلاس مربوطه از تابع GetType استفاده میکنیم
کد:
Type.GetType("Customer");
جمع در داخل کلاس دیگه
کد:
Type.GetType("Customer+Category");