日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#开发-基础知识及有用技巧(一)

發(fā)布時(shí)間:2023/12/9 C# 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#开发-基础知识及有用技巧(一) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1、時(shí)間長度的計(jì)算 TimeSpan類。
???例如:TimeSpan span = dateTime1 - dateTime2? ?方便啊
2、從類(Class)返回一個(gè)System.Type類型,用typeof關(guān)鍵字
3、從一個(gè)對象實(shí)例(Object)返回一個(gè)System.Type類型,用GetType方法
4、判斷是否處于設(shè)計(jì)狀態(tài):DesignMode屬性
5、根據(jù)GUID創(chuàng)建對象實(shí)例
System.Guid?pGuid?=?new?Guid(guid);
System.Type?ObjectCustorm?
=?Type.GetTypeFromCLSID(pGuid);
Object?obj?
=?Activator.CreateInstance(ObjectCustorm); 6、GDI+不支持xor繪制模式的近似解決方法:
ControlPaint.DrawReversibleFrame、ControlPaint.DrawReversibleLine方法
7、獲取Enum類型中的所有枚舉值:
  Enum.GetNames方法
 將字符串轉(zhuǎn)換成枚舉值
  Enum.Parse方法
8、Label放在圖片上時(shí),使Label透明
picLogo.Controls.Add(lblStatus);
lblStatus.BackColor?
=?Color.Transparent; 9、調(diào)用幫助文件
打開幫助文件
Help.ShowHelp(this,@"c:/windows/help/mspaint.chm");

打開幫助文件,并跳轉(zhuǎn)到指定的主題
Help.ShowHelp(this,@"c:/windows/help/mspaint.chm","paint_lines.htm");

打開幫助文件,并轉(zhuǎn)到“索引”選項(xiàng)卡
Help.ShowHelpIndex(this,@"c:/windows/help/mspaint.chm","paint_lines.htm");

在屏幕上顯示一條浮動(dòng)的幫助信息
Help.ShowPopup(this,"這里是幫助信息",new Point(100,100));

10、通過AppDomain在應(yīng)用程序之間傳遞數(shù)據(jù)
例如,兩個(gè)系統(tǒng)可能會(huì)共用登錄信息,登錄一個(gè)系統(tǒng)后,再啟動(dòng)另一個(gè)系統(tǒng)時(shí),不需要重新登錄。
先定義一個(gè)在應(yīng)用程序之間傳遞的數(shù)據(jù)的類,該類必須從MarshalByRefObject繼承:

/**////?<summary>
///?用于在不同的appdomain之間傳遞參數(shù)
///?</summary>

public?class?AppDomainInfo:MarshalByRefObject
{
????
public?int?UserID;
}

然后可以這樣打開新的應(yīng)用程序

????????AppDomainSetup?setup?=?new?AppDomainSetup();
????????????????????setup.ApplicationName?
=?"測試程序";????????????????????
????????????????????AppDomain?appDomain?
=?AppDomain.CreateDomain("TestDomain",?null,?setup);

????????????????????AppDomainInfo?domainInfo?
=?new??AppDomainInfo();
????????????????????domainInfo.UserID?
=?Winsharp.BaseClass.AppConfigInfo.UserID;
????????????????????appDomain.SetData(
"domainInfo",domainInfo);

????????????????????
object?obj?=?appDomain.CreateInstanceFromAndUnwrap(str,"TestDomain.Test");
????????????????????(obj?
as?Form).Show();

11、換行字符串,相當(dāng)于"\r\n",Environment.NewLine
?
?????API中有GetTickCount函數(shù),C#中為Environment.TickCount
12、取得安裝操作系統(tǒng)輸入的用戶姓名和公司名稱:

? Microsoft.Win32.RegistryKey???cmicRegKey=Microsoft.Win32.Registry.CurrentUser.OpenSubKey("Software");???
????????????????? ?cmicRegKey
=cmicRegKey.OpenSubKey("Microsoft");???
????????????????cmicRegKey
=cmicRegKey.OpenSubKey("MS?Setup?(ACME)");???
????????????????cmicRegKey
=cmicRegKey.OpenSubKey("User?Info");???
????????????????
object?cmicCompany?=?cmicRegKey.GetValue("DefCompany");?????
????????????????
object?cmicUser?=?cmicRegKey.GetValue("DefName");???

13、C# WinForm 捕獲最小化事件(來自Limon Tea的隨筆http://limon7.cnblogs.com/archive/2006/07/23/457865.html)
?雖然Form類沒有提供Minimize的事件,但還是可以通過重載Deactive來實(shí)現(xiàn)
當(dāng)Form失去焦點(diǎn)后,測試WindowState取得Form狀態(tài),若為Minimized既是最小化事件。
本例為最小化后隱藏窗口:

private?void?Form1_Deactivate(object?sender,?EventArgs?e)
????????
{
????????????
if?(this.WindowState?==?FormWindowState.Minimized)
????????????????
this.Visible?=?false;
????????}

還有種方法更加直接,重載WndProc:

const?int?WM_SYSCOMMAND?=?0x112;
const?int?SC_CLOSE?=?0xF060;
const?int?SC_MINIMIZE?=?0xF020;
const?int?SC_MAXIMIZE?=?0xF030;
protected?override?void?WndProc(ref?Message?m)
{
????
if?(m.Msg?==?WM_SYSCOMMAND)
????
{
????????
if?(m.WParam.ToInt32()?==?SC_MINIMIZE)
????????
{
????????????
this.Visible?=?false;
????????????
return;
????????}

????}

????
base.WndProc(ref?m);
}

14、FromBase64String的問題
?? 在使用Convert.ToBase64String()對字符串進(jìn)行Base64編碼時(shí),注意的幾點(diǎn):
???? 例:string s = "Hello";
???????? byte[] bytes = Convert.FromBase64String(s);
? 以上代碼在運(yùn)行時(shí)會(huì)拋出FormatException異常.提示為:Base-64字符數(shù)組的無效長度

原因:當(dāng)Convert.FromBase64String方法的參數(shù)s的長度小于 4 或不是 4 的偶數(shù)倍時(shí),將會(huì)拋出FormatException。
??
???? 例:
???????? Convert.FromBase64String("Hell");????? // Normal.
???????? Convert.FromBase64String("Hell ");???? // Normal.(忽略空格)
???????? Convert.FromBase64String("Hello!");??? // throw FormatException.
???????? Convert.FromBase64String("Hello Net"); // Normal.(忽略空格)

15、避免程序重復(fù)運(yùn)行。(即只允許運(yùn)行一個(gè)實(shí)例)

System.Threading.Mutex?appSingleton?=?new?System.Threading.Mutex(false,?"MyProgInstance_PPP");?
????
if(appSingleton.WaitOne(0,?false))?
????{
??????Application.Run(
new?FormMain(););
????}
???
else
???{
??????MessageBox.Show(
"程序已經(jīng)運(yùn)行");
???}

16、VB中的chr和asc函數(shù)在C#中沒有,C#中只要用Convert類中的函數(shù)進(jìn)行轉(zhuǎn)換就可以了,如:
???? int n = Convert.ToInt32('a');
???? char c = Convert.ToChar(99);
???? 另外,空的char值的獲得:Convert.ToChar(0) 例如,在設(shè)置了一個(gè)textBox的PasswordChar屬性后,要清除它,就只能這樣了:textBox1.PasswordChar = Convert.ToChar(0) ;

17、C#的String.Format舉例(http://blog.sina.com.cn/u/4a99b1ba010005ax)


string str1 = String.Format("{0:N1}",56789);????????????? ?//result: 56,789.0
?string str2 = String.Format("{0:N2}",56789);?????????????? //result: 56,789.00
?string str3 = String.Format("{0:N3}",56789);????????????? ?//result: 56,789.000
?string str8 = String.Format("{0:F1}",56789);?????????????? //result: 56789.0
?string str9 = String.Format("{0:F2}",56789);?????????????? //result: 56789.00
?string str11 =(56789 / 100.0).ToString("#.##");????????? ?//result: 567.89
?string str12 =(56789 / 100).ToString("#.##");????????????? //result: 567

字符說明示例輸出

C 或 c

貨幣

Console.Write("{0:C}", 2.5);

Console.Write("{0:C}", -2.5);

$2.50

($2.50)

D 或 d

十進(jìn)制數(shù)

Console.Write("{0:D5}", 25);

00025

E 或 e

科學(xué)型

Console.Write("{0:E}", 250000);

2.500000E+005

F 或 f

固定點(diǎn)

Console.Write("{0:F2}", 25);

Console.Write("{0:F0}", 25);

25.00

25

G 或 g

常規(guī)

Console.Write("{0:G}", 2.5);

2.5

N 或 n

數(shù)字

Console.Write("{0:N}", 2500000);

2,500,000.00

X 或 x

十六進(jìn)制

Console.Write("{0:X}", 250);

Console.Write("{0:X}", 0xffff);

FA

FFFF


C#的String.Format舉例

string str1 = String.Format("{0:N1}",56789);????????????? ?//result: 56,789.0
?string str2 = String.Format("{0:N2}",56789);?????????????? //result: 56,789.00
?string str3 = String.Format("{0:N3}",56789);????????????? ?//result: 56,789.000
?string str8 = String.Format("{0:F1}",56789);?????????????? //result: 56789.0
?string str9 = String.Format("{0:F2}",56789);?????????????? //result: 56789.00
?string str11 =(56789 / 100.0).ToString("#.##");????????? ?//result: 567.89
?string str12 =(56789 / 100).ToString("#.##");????????????? //result: 567

字符說明示例輸出

C 或 c

貨幣

Console.Write("{0:C}", 2.5);

Console.Write("{0:C}", -2.5);

$2.50

($2.50)

D 或 d

十進(jìn)制數(shù)

Console.Write("{0:D5}", 25);

00025

E 或 e

科學(xué)型

Console.Write("{0:E}", 250000);

2.500000E+005

F 或 f

固定點(diǎn)

Console.Write("{0:F2}", 25);

Console.Write("{0:F0}", 25);

25.00

25

G 或 g

常規(guī)

Console.Write("{0:G}", 2.5);

2.5

N 或 n

數(shù)字

Console.Write("{0:N}", 2500000);

2,500,000.00

X 或 x

十六進(jìn)制

Console.Write("{0:X}", 250);

Console.Write("{0:X}", 0xffff);

FA

FFFF


18、bytes[]和int的相互轉(zhuǎn)換 ??int???s???=???100;???
??
byte[]???shi???=???System.BitConverter.GetBytes(s);?????????
??
int???sh???=???System.BitConverter.ToInt32(shi,0);

19、BitArray和int的相互轉(zhuǎn)換

int[]???a???=???new???int[1];???
??a[
0]???=???100;???
??
int[]???b???=???new???int[1];???
????
??System.Collections.BitArray???shit???
=???new???BitArray(a);???
??shit.CopyTo(b,
0);

20、隨機(jī)數(shù):
1) System.Random
2) xdev所提到的System.Security.Cryptography.RNGCryptoServiceProvider
3) 直接調(diào)用Platform SDK中的CryptGenRandom()函數(shù)。
其中,和1)相比,2)和3)可以被作為真正的隨機(jī)數(shù)來使用。

21、最簡單的實(shí)現(xiàn)雙緩沖繪圖:
this.SetStyle(ControlStyles.DoubleBuffer |
????? ControlStyles.UserPaint |
????? ControlStyles.AllPaintingInWmPaint,
????? true);
?? this.UpdateStyles();

還有一種方法,是先建一個(gè)bmp,在該bmp上繪制完畢后,最后一次性復(fù)制到目標(biāo)畫布上。但在實(shí)際使用中,我發(fā)現(xiàn)該方法有時(shí)反倒閃爍得厲害,不知是為什么,郁悶~

22、取得局域網(wǎng)內(nèi)本機(jī)IP地址
String name = Dns.GetHostName();
IPHostEntry ips = Dns.GetHostByName(name);
return ips.AddressList[0].ToString();

23、取得局域網(wǎng)廣播地址
IPv4廣播地址的定義是網(wǎng)絡(luò)號(hào)碼與網(wǎng)絡(luò)掩碼邏輯“非”之間的邏輯“或”。
??? 例如一個(gè)地址為:197.8.43.211,其子網(wǎng)掩碼為255.255.255.240則其網(wǎng)絡(luò)號(hào)碼為兩者的二進(jìn)制邏輯"與",即197.8.43.208 網(wǎng)絡(luò)掩碼的邏輯"非"為0.0.0.15,與網(wǎng)絡(luò)號(hào)碼197.8.43.208二進(jìn)制邏輯"或"的結(jié)果為197.8.43.223,即該子網(wǎng)的廣播地址.

24、Asp.net直接下載文件

FileStream?fileStream=new?FileStream(pFileName,FileMode.Open)?
long?fileSize?=?fileStream.Length;?
Context.Response.ContentType
="application/octet-stream";?
Context.Response.AddHeader(
"Content-Disposition","attachment;?filename=\""?+?fileName?+?"\";");?
Context.Response.AddHeader(
"Content-Length",fileSize.ToString());?
byte[]?fileBuffer=new?byte[fileSize];?
fileStream.Read(fileBuffer,?
0,?(int)fileSize);?
Context.Response.BinaryWrite(fileBuffer);?
Context.Response.End();??

25、十進(jìn)制數(shù)轉(zhuǎn)成十六進(jìn)制字符串
int num =123;
string str = Convert.ToString(num,16);
十六進(jìn)制字符串轉(zhuǎn)十進(jìn)制數(shù)
string str = "7b";
int num = Convert.ToInt32(str,16);

其它進(jìn)制也是一樣,只要把16改進(jìn)2或8或10就可以了.

26、在網(wǎng)頁上實(shí)現(xiàn)html編輯的最簡單代碼(來自微軟網(wǎng)站)

<html>
<head>
<title>DHTML?編輯控件代理示例</title>
<script?type="text/javascript">
function?FillEditor()?{
????
var?doc?=?editor.document;
????doc.designMode?
=?"on";
????doc.write(
"<body><p><i>可視化</i>?<u>安靜</u></p></body>");
????doc.close();
}
</script>
</head>
<body?onload="FillEditor()">
??
<iframe?id="editor"?scrolling="yes"?height="100"?width="300">
??
</iframe>
??
<p>
??
<input?type=button?
?????
value="Submit"?
?????onclick
=
???????"alert(editor.document.getElementsByTagName('HTML')[0].outerHTML)"
>
</body>
</html>

此頁面顯示了包括格式化 HTML 的可編輯區(qū)域。您可在此區(qū)域中輸入內(nèi)容,也可使用熱鍵,如 CTRL+I 切換斜體,CTRL+U 切換下劃線。當(dāng)按下 Submit 按鈕時(shí),出現(xiàn)一個(gè)消息框,顯示 <iframe> 元素的 HTML 內(nèi)容。要等效地實(shí)現(xiàn) DHTML 編輯控件的屬性、方法和事件,必須要使用腳本。

出于安全考慮,只有來自相同域的內(nèi)容才可跨過 <iframe> 界限進(jìn)行訪問。要確保框架的內(nèi)容可被訪問,必須使用 src 屬性,從同一域的 URL 中對其進(jìn)行初始化,正如下列示例所示:

<iframe id="editor" scrolling="yes" height="100" width="300" src="templates/blank.htm"> </iframe>

有關(guān)在 Internet Explorer 中使用 designMode 屬性進(jìn)行編輯的詳細(xì)信息,請參閱 Microsoft 網(wǎng)站上的 Introduction to MSHTML Editing(英文)頁面。

27、強(qiáng)制類型轉(zhuǎn)換與as類型轉(zhuǎn)換的區(qū)別
??????當(dāng)類型轉(zhuǎn)換非法時(shí),強(qiáng)制類型轉(zhuǎn)換將拋出一個(gè)System.InvalidCastException異常,而as不會(huì)拋出異常,它返回一個(gè)null值。

28、FromBase64String(string s)函數(shù),對s有如下要求,否則會(huì)報(bào)錯(cuò)“Base-64字符中的無效字符”:
???????? s ? 由基 ? 64 ? 數(shù)字、空白字符和尾隨填充字符組成。從零開始以升序排列的以 ? 64 ? 為基的數(shù)字為大寫字符“A”到“Z”、小寫字符“a”到“z”、數(shù)字“0”到“9”以及符號(hào)“+”和“/”。 ? 空白字符為 ? Tab、空格、回車和換行。s ? 中可以出現(xiàn)任意數(shù)目的空白字符,因?yàn)樗锌瞻鬃址紝⒈缓雎浴? 無值字符“=”用于尾部的空白。s ? 的末尾可以包含零個(gè)、一個(gè)或兩個(gè)填充字符。

29、這一條不是C#的,是數(shù)據(jù)庫的,有用,收藏一下~
幾個(gè)刪除重復(fù)記錄的sql語句
(1)用rowid方法據(jù)據(jù)oracle帶的rowid屬性,進(jìn)行判斷,是否存在重復(fù),語句如下:
查數(shù)據(jù):
??? select * from table1 a where rowid !=(select? max(rowid)?
??? from table1 b where a.name1=b.name1 and a.name2=b.name2......)
刪數(shù)據(jù):
?? delete? from table1 a where rowid !=(select? max(rowid)?
??? from table1 b where a.name1=b.name1 and a.name2=b.name2......)
(2)group by方法
查數(shù)據(jù):
  select count(num), max(name) from student --列出重復(fù)的記錄數(shù),并列出他的name屬性
  group by num
  having count(num) >1 --按num分組后找出表中num列重復(fù),即出現(xiàn)次數(shù)大于一次
刪數(shù)據(jù):
  delete from student
  group by num
  having count(num) >1
  這樣的話就把所有重復(fù)的都刪除了。
(3)用distinct方法 -對于小的表比較有用
create table table_new as? select distinct *? from table1 minux
truncate table table1;
insert into table1 select * from table_new;


隨時(shí)補(bǔ)充吧

總結(jié)

以上是生活随笔為你收集整理的c#开发-基础知识及有用技巧(一)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。