国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開(kāi)發(fā) > 綜合 > 正文

C#文件操作

2024-07-21 02:27:04
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

獲取文件的版本信息:

fileversioninfo myfileversioninfo1 = fileversioninfo.getversioninfo("d://test.dll");
textbox1.text="版本號(hào): " + myfileversioninfo1.fileversion; 


更改文件屬性,刪除只讀文件:

下例欲將e:/test.txt文件拷貝至d:/tmp/test.txt,但d:/tmp/test.txt已經(jīng)存在。

//file.copy(sourcefile,destinationfile,true); 用來(lái)拷貝文件
//當(dāng)destinationfile已經(jīng)存在時(shí),無(wú)法將文件file1拷貝到目標(biāo)文件,
//因此先刪除destination文件,file.delete()方法不能刪除只讀文件,
//因此,如果文件屬性為只讀(attributes屬性中會(huì)包含有"readonly"),
//先把文件屬性重置為normal,然后再刪除:
string file1="e://test.txt";
string destinationfile="d://tmp//test.txt";
if(file.exists(destinationfile))
{
 fileinfo fi=new fileinfo(destinationfile);
 if(fi.attributes.tostring().indexof("readonly")!=-1)
  fi.attributes=fileattributes.normal;
  file.delete(destinationfile);
}
file.copy(file1,destinationfile,true);

  判斷文件是否存在:file.exists(string filepath)

  判斷目錄是否存在:directory.exists("d://lastestversion")

  按行讀取文件:

int filecount=0;
// open the file just specified such that no one else can use it.
streamreader sr = new streamreader(textbox1.text.trim());
while(sr.peek() > -1)//streamreader.peek()返回下一個(gè)可用字符,但不使用它
{
 listbox1.items.add(sr.readline());
 filecount++;
}
sr.close();

  按行寫(xiě)入文件:

streamwriter sw = new streamwriter("d://result.txt");
for(int i=0;i<10;i++)
{
 sw.writeline("這是第"+i.tostring()+"行數(shù)據(jù)");
}

方法二:

 

private const int fo_delete = 0x3;
private const ushort fof_noconfirmation = 0x10;
private const ushort fof_allowundo = 0x40;

[dllimport("shell32.dll", setlasterror=true, charset=charset.unicode)]
private static extern int shfileoperation([in,out] _shfileopstruct str);

[structlayout(layoutkind.sequential, charset=charset.unicode)]
public class _shfileopstruct
{
public intptr hwnd;
public uint32 wfunc;
public string pfrom;
public string pto;
public uint16 fflags;
public int32 fanyoperationsaborted;
public intptr hnamemappings;
public string lpszprogresstitle;
}

public static int delete(string path)
{
_shfileopstruct pm = new _shfileopstruct();
pm.wfunc = fo_delete;
pm.pfrom = path + '/0';
pm.pto = null;
pm.fflags = fof_allowundo ¦ fof_noconfirmation;
return shfileoperation(pm);
}
調(diào)用:class1.delete("c://temp.txt");

注:返回值為0表示調(diào)用成功,非0表示調(diào)用失敗。
需要引用system.runtime.interopservices命名空間。


 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 灌阳县| 敖汉旗| 仁怀市| 大兴区| 若尔盖县| 嵩明县| 屯留县| 浏阳市| 永仁县| 遂昌县| 周口市| 寿光市| 东台市| 桃江县| 栾川县| 正蓝旗| 苗栗县| 冷水江市| 博白县| 阿城市| 邵东县| 准格尔旗| 绵竹市| 宜昌市| 杂多县| 秦皇岛市| 恩施市| 林州市| 莲花县| 吕梁市| 华池县| 会同县| 巴彦淖尔市| 延边| 嘉义市| 永定县| 资阳市| 金平| 弥渡县| 弥渡县| 达孜县|