C 常用操作

2022-04-14 19:11:14 字數 3448 閱讀 1199

1. streamwriter -檔案寫入類

streamwriter s = new streamwriter(address + "

/menu.ini

", true

);s.writeline(openfiledialog1.filename);

s.flush();

s.close();

2. streamreader -檔案讀取類

streamreader sr = new streamreader(address + "

/menu.ini");

while (sr.peek()>=0

)sr.close();

3. image -影象類

image p = image.fromfile("

/背景.jpg");

form f = new form(); //

建立mid視窗

f.mdiparent = this; //

設定父視窗

f.backgroundimage = p; //

設定mdi視窗的背景圖

f.show(); //

顯示mdi視窗

4. bitmap -點陣圖類

//建立位圖, bitmap類繼承於image類

bitmap bit;

bit = new bitmap("

heart.bmp");

bit.maketransparent(color.white);

//設定透明色

protected

override

void

onpaint(painteventargs e)

5. this.opacity -控制項的不透明度

//控制控制項透明程度,很有用。

6. c#中匯入dll檔案中的api

[system.runtime.interopservices.dllimportattribute(

"user32.dll")]

public

static

extern

bool flashwindow(intptr handle, bool

binvert); 7

. 隱藏標題欄

this.controlbox = false;

8. 視窗始終處於最上面

this.topmost =ture;

9. screen -桌面類

screen.primaryscreen.workingarea.height

//桌面的高

screen.primaryscreen.workingarea.width //

桌面的寬

screen.primaryscreen.bitsperpixel //

桌面的位深

10. 基本繪圖

graphics graphics;

pen mypen = new pen(color.blue, 2);

//畫線

graphics = this

.creategraphics();

graphics.drawline(mypen,

30, 60, 150, 60);

//畫矩形

graphics = this

.creategraphics();

graphics.drawrectangle(mypen,

30, 80, 120, 50);

//畫橢圓

graphics = this

.creategraphics();

rectangle myrectangle = new rectangle(160, 70, 100, 60

);graphics.drawellipse(mypen, myrectangle); 11

. 獲得滑鼠在視窗中的座標

cursor.clip = new rectangle(this.location, this

.size);

label1.text = "

當前滑鼠的位置為:

" +cursor.position; 12

. 判斷鍵盤

protected

override

bool processcmdkey(ref

message msg, keys keydata)

messagebox.show(strinfo, "資訊

", messageboxbuttons.ok, messageboxicon.information);

}return

base.processcmdkey(ref

msg, keydata);}

13. 控制遠端計算機

//首先新增對 system.management的引用

private

void closecomputer(string strname,string strpwd,string ip,string

doinfo)

; mobj.invokemethod(doinfo, str);

}messagebox.show(

"操作成功");

}catch

(exception ey)}

//重啟遠端計算機

closecomputer(this.textbox2.text, this.textbox3.text, this.textbox1.text, "

reboot");

//關閉遠端計算機

closecomputer(this.textbox2.text, this.textbox3.text, this.textbox1.text, "

shutdown");

14. ping的使用

ping pinginfo = new

ping();

pingoptions pingopt = new

pingoptions();

pingopt.dontfragment = true

;string myinfo = "

hyworkhyworkhyworkhyworkhyworkhywork";

byte bufferinfo =encoding.ascii.getbytes(myinfo);

int timeout = 120

;pingreply reply = pinginfo.send(this

.textbox1.text, timeout, bufferinfo, pingopt);

if (reply.status ==ipstatus.success)

else 15

. 檢查檔案是否存在

public

int checkfileexit(string

objfilepath)

C 常用函式操作

2.string include尾部新增 push back 元素個數 size 是否為空 empty 在第i個元素前面插入k insert a.begin i,k 刪除尾部元素 pop back 刪除區間 eraser a.begin i,a.begin j 刪除區間 i,j 1 的元素 刪除元素...

C 常用IO操作

建立資料夾 如果資料夾路徑不存在則建立資料夾 if directory.exists path directory.createdirectory path 遞迴建立資料夾 public void createdir string fullpath view code 刪除整個資料夾 directo...

C 常用的檔案操作

c 常用的檔案操作 c 寫入 讀出文字檔案 public void page load object src,eventargs e 開啟文字檔案 streamreader sr mytext.txt stringbuilder output new stringbuilder string rl ...