C IO 建立資料夾

2021-09-23 18:33:47 字數 2734 閱讀 5155

c# 建立資料夾

c#中對資料夾操作需要用到directory class。其中提供了建立、刪除、移動、列舉等靜態方法。該類不能被繼承。

以下**實現了建立資料夾。

if(!directory.exists(spath))

以下是msdn上directory class的sample code。

以下**首先檢查指定的資料夾是否存在,若存在則刪除之,否則建立之。接下來移動資料夾,在其中建立檔案並統計資料夾中檔案數目。

usingsystem;

usingsystem.io;

classtest

if(directory.exists(target))

// move the directory.

directory.move(path, target);

// create a file in the directory.

file.createtext(target +@"\myfile.txt");

// count the files in the target directory.

console.writeline("the number of files in is ",

target, directory.getfiles(target).length);

}

catch(exception e)

", e.tostring());

}

finally{}

}

}

以下**演示了如何計算資料夾大小。

// the following example calculates the size of a directory

// and its subdirectories, if any, and displays the total size

// in bytes.

usingsystem;

usingsystem.io;

publicclassshowdirsize

// add subdirectory sizes.

directoryinfo dis = d.getdirectories();

foreach(directoryinfo diindis)

return(size);

}

publicstaticvoidmain(stringargs)

else

and its subdirectories is bytes.", d, dsize);

}

}

}

建立資料夾

當某資料夾不存在時,建立資料夾 import os path dir file if not os.path.exists path dir os.makedirs path dir 同時建立資料夾有兩種函式,os.mkdir和os.makedirs,兩者的區別在於前者是一級一級建立檔案目錄,後者可...

linux c建立資料夾,並在資料夾中建立檔案

建立資料夾,在在檔案裡面建立檔案 乙個例項 include include include include int main char pathname 100 pathname 0 f pathname 1 l pathname 2 o pathname 3 w pathname 4 pathna...

C 建立資料夾,刪除資料夾,建立檔案,刪除檔案

protected void button1 click object sender,eventargs e 判斷檔案的存在 else string name getfiles.filename 獲取已上傳檔案的名字 string size getfiles.postedfile.contentle...