要進行“網絡硬盤”功能設計,首先要熟悉.net中處理文件和文件夾的操作。file類和directory類是其中最主要的兩個類。了解它們將對后面功能的實現提供很大的便利。
system.io.file類和system.io.fileinfo類
在設計和實現“網絡硬盤”的過程中,將大量地使用和文件系統操作相關的內容。故本節先對和文件系統相關的兩個.net類進行簡要介紹。
system.io.file類和system.io.fileinfo類主要提供有關文件的各種操作,在使用時需要引用system.io命名空間。下面通過程序實例來介紹其主要屬性和方法。
(1) 文件打開方法:file.open
該方法的聲明如下:
public static filestream open(string path,filemode mode)
下面的代碼打開存放在c:/tempuploads目錄下名稱為newfile.txt文件,并在該文件中寫入hello。
private void openfile()
{
filestream.textfile=file.open(@"c:/tempuploads/newfile.txt",filemode.append);
byte [] info = {(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};
textfile.write(info,0,info.length);
textfile.close();
}
(2) 文件創建方法:file.create
該方法的聲明如下:
public static filestream create(string path;)
下面的代碼演示如何在c:/tempuploads下創建名為newfile.txt的文件。
由于file.create方法默認向所有用戶授予對新文件的完全讀/寫訪問權限,所以文件是用讀/寫訪問權限打開的,必須關閉后才能由其他應用程序打開。為此,所以需要使用filestream類的close方法將所創建的文件關閉。
private void makefile()
{
filestream newtext=file.create(@"c:/tempuploads/newfile.txt");
newtext.close();
}
(3) 文件刪除方法:file.delete
該方法聲明如下:
public static void delete(string path);
下面的代碼演示如何刪除c:/tempuploads目錄下的newfile.txt文件。
private void deletefile()
{
file.delete(@"c:/tempuploads/newfile.txt");
}
(4) 文件復制方法:file.copy
該方法聲明如下:
public static void copy(string sourcefilename,string destfilename,bool overwrite);
下面的代碼將c:/tempuploads/newfile.txt復制到c:/tempuploads/backup.txt。
由于cope方法的overwrite參數設為true,所以如果backup.txt文件已存在的話,將會被復制過去的文件所覆蓋。
private void copyfile()
{
file.copy(@"c:/tempuploads/newfile.txt",@"c:/tempuploads/backup.txt",true);
}
(5) 文件移動方法:file.move
該方法聲明如下:
public static void move(string sourcefilename,string destfilename);
下面的代碼可以將c:/tempuploads下的backup.txt文件移動到c盤根目錄下。
注意:
只能在同一個邏輯盤下進行文件轉移。如果試圖將c盤下的文件轉移到d盤,將發生錯誤。
private void movefile()
{
file.move(@"c:/tempuploads/backup.txt",@"c:/backup.txt");
}
(6) 設置文件屬性方法:file.setattributes
該方法聲明如下:
public static void setattributes(string path,fileattributes fileattributes);
下面的代碼可以設置文件c:/tempuploads/newfile.txt的屬性為只讀、隱藏。
private void setfile()
{
file.setattributes(@"c:/tempuploads/newfile.txt",
fileattributes.readonly|fileattributes.hidden);
}
文件除了常用的只讀和隱藏屬性外,還有archive(文件存檔狀態),system(系統文件),temporary(臨時文件)等。關于文件屬性的詳細情況請參看msdn中fileattributes的描述。
(7) 判斷文件是否存在的方法:file.exist
該方法聲明如下:
public static bool exists(string path);
下面的代碼判斷是否存在c:/tempuploads/newfile.txt文件。若存在,先復制該文件,然后其刪除,最后將復制的文件移動;若不存在,則先創建該文件,然后打開該文件并進行寫入操作,最后將文件屬性設為只讀、隱藏。
if(file.exists(@"c:/tempuploads/newfile.txt")) //判斷文件是否存在
{
copyfile(); //復制文件
deletefile(); //刪除文件
movefile(); //移動文件
}
else
{
makefile(); //生成文件
openfile(); //打開文件
setfile(); //設置文件屬性
}
此外,file類對于text文本提供了更多的支持。
· appendtext:將文本追加到現有文件
· createtext:為寫入文本創建或打開新文件
· opentext:打開現有文本文件以進行讀取
但上述方法主要對utf-8的編碼文本進行操作,從而顯得不夠靈活。在這里推薦讀者使用下面的代碼對txt文件進行操作。
· 對txt文件進行“讀”操作,示例代碼如下:
streamreader txtreader = new streamreader(@"c:/tempuploads/newfile.txt",system.text.encoding.default);
string filecontent;
filecontent = txtreader.readend();
txtreader.close();
· 對txt文件進行“寫”操作,示例代碼如下:
streamwriter = new streamwrite(@"c:/tempuploads/newfile.txt",system.text.encoding.default);
string filecontent;
txtwriter.write(filecontent);
txtwriter.close();
system.io.directory類和system.directoryinfo類
主要提供關于目錄的各種操作,使用時需要引用system.io命名空間。下面通過程序實例來介紹其主要屬性和方法。
(1) 目錄創建方法:directory.createdirectory
該方法聲明如下:
public static directoryinfo createdirectory(string path);
下面的代碼演示在c:/tempuploads文件夾下創建名為newdirectory的目錄。
private void makedirectory()
{
directory.createdirectory(@"c:/tempuploads/newdirectoty");
}
(2) 目錄屬性設置方法:directoryinfo.atttributes
下面的代碼設置c:/tempuploads/newdirectory目錄為只讀、隱藏。與文件屬性相同,目錄屬性也是使用fileattributes來進行設置的。
private void setdirectory()
{
directoryinfo newdirinfo = new directoryinfo(@"c:/tempuploads/newdirectoty");
newdirinfo.atttributes = fileattributes.readonly|fileattributes.hidden;
}
(3) 目錄刪除方法:directory.delete
該方法聲明如下:
public static void delete(string path,bool recursive);
下面的代碼可以將c:/tempuploads/backup目錄刪除。delete方法的第二個參數為bool類型,它可以決定是否刪除非空目錄。如果該參數值為true,將刪除整個目錄,即使該目錄下有文件或子目錄;若為false,則僅當目錄為空時才可刪除。
private void deletedirectory()
{
directory.delete(@"c:/tempuploads/backup",true);
}
(4) 目錄移動方法:directory.move
該方法聲明如下:
public static void move(string sourcedirname,string destdirname);
下面的代碼將目錄c:/tempuploads/newdirectory移動到c:/tempuploads/backup。
private void movedirectory()
{
file.move(@"c:/tempuploads/newdirectory",@"c:/tempuploads/backup");
}
(5) 獲取當前目錄下的所有子目錄方法:directory.getdirectories
該方法聲明如下:
public static string[] getdirectories(string path;);
下面的代碼讀出c:/tempuploads/目錄下的所有子目錄,并將其存儲到字符串數組中。
private void getdirectory()
{
string [] directorys;
directorys = directory. getdirectories (@"c:/tempuploads");
}
(6) 獲取當前目錄下的所有文件方法:directory.getfiles
該方法聲明如下:
public static string[] getfiles(string path;);
下面的代碼讀出c:/tempuploads/目錄下的所有文件,并將其存儲到字符串數組中。
private void getfile()
{
string [] files;
files = directory. getfiles (@"c:/tempuploads",);
}
(7) 判斷目錄是否存在方法:directory.exist
該方法聲明如下:
public static bool exists(
string path;
);
下面的代碼判斷是否存在c:/tempuploads/newdirectory目錄。若存在,先獲取該目錄下的子目錄和文件,然后其移動,最后將移動后的目錄刪除。若不存在,則先創建該目錄,然后將目錄屬性設為只讀、隱藏。
if(file.exists(@"c:/tempuploads/newdirectory")) //判斷目錄是否存在
{
getdirectory(); //獲取子目錄
getfile(); //獲取文件
movedirectory(); //移動目錄
deletedirectory(); //刪除目錄
}
else
{
makedirectory(); //生成目錄
setdirectory(); //設置目錄屬性
}
注意:
路徑有3種方式,當前目錄下的相對路徑、當前工作盤的相對路徑、絕對路徑。以c:/tmp/book為例(假定當前工作目錄為c:/tmp)。“book”,“/tmp/book”,“c:/tmp/book”都表示c:/tmp/book。
另外,在c#中 “/”是特殊字符,要表示它的話需要使用“//”。由于這種寫法不方便,c#語言提供了@對其簡化。只要在字符串前加上@即可直接使用“/”。所以上面的路徑在c#中應該表示為“book”,@“/tmp/book”,@“c:/tmp/book”。
新聞熱點
疑難解答
圖片精選