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

首頁 > 編程 > .NET > 正文

使用 .NET的IO(5) Paul_Ni

2024-07-10 13:08:10
字體:
來源:轉載
供稿:網友


收集最實用的網頁特效代碼!

查找現有的文件和目錄


您還可以使用獨立存儲文件來搜索現有的目錄和文件。請記住,在存儲區中,文件名和目錄名是相對于虛文件系統的根目錄指定的。此外,windows 文件系統中的文件和目錄名不區分大小寫。
要搜索某個目錄,請使用 isolatedstoragefile 的 getdirectorynames 實例方法。getdirectorynames 采用表示搜索模式的字符串。支持使用單字符 (?) 和多字符 (*) 通配符。這些通配符不能出現在名稱的路徑部分。也就是說,directory1/*ect* 是有效的搜索字符串,而 *ect*/directory2 不是有效的搜索字符串。
要搜索某個文件,請使用 isolatedstoragefile 的 getfilenames 實例方法。對應用于 getdirectorynames 的搜索字符串中通配符的相同限制也適用于 getfilenames。
getdirectorynamesgetfilenames 都不是遞歸的,即 isolatedstoragefile 不提供用于列出存儲區中所有目錄或文件的方法。但是,下面的代碼中部分是遞歸方法的示例。另外還要注意,getdirectorynamesgetfilenames 只返回找到的項的目錄名或文件名。例如,如果找到目錄 rootdir/subdir/subsubdir 的匹配項,結果數組中將返回 subsubdir。

findingexistingfilesanddirectories 示例


下面的代碼示例闡釋如何在獨立存儲區創建文件和目錄。首先,檢索一個按用戶、域和程序集隔離的存儲區并放入 isostore 變量。createdirectory 方法用于設置幾個不同的目錄,isolatedstoragefilestream 方法在這些目錄中創建一些文件。然后,代碼依次通過 getalldirectories 方法的結果。該方法使用 getdirectorynames 來查找當前目錄中的所有目錄名。這些名稱存儲在數組中,然后 getalldirectories 調用其本身,傳入它所找到的每個目錄。結果是在數組中返回的所有目錄名。然后,代碼調用 getallfiles 方法。該方法調用 getalldirectories 以查找所有目錄的名稱,然后它檢查每個目錄以查找使用 getfilenames 方法的文件。結果返回到數組中用于顯示。
 [c#]
using system;
using system.io;
using system.io.isolatedstorage;
using system.collections;
 
public class findingexistingfilesanddirectories{
 
   // retrieves an array of all directories in the store, and 
   // displays the results.
 
   public static void main(){
 
      // this part of the code sets up a few directories and files in the
      // store.
      isolatedstoragefile isostore = isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null);
      isostore.createdirectory("topleveldirectory");
      isostore.createdirectory("topleveldirectory/secondlevel");
      isostore.createdirectory("anothertopleveldirectory/insidedirectory");
      new isolatedstoragefilestream("intheroot.txt", filemode.create, isostore);
      new isolatedstoragefilestream("anothertopleveldirectory/insidedirectory/hereiam.txt", filemode.create, isostore);
      // end of setup.
 
      console.writeline('/r');
      console.writeline("here is a list of all directories in this isolated store:");
 
      foreach(string directory in getalldirectories("*", isostore)){
         console.writeline(directory);
      }
      console.writeline('/r');
 
      // retrieve all the files in the directory by calling the getfiles 
      // method.
 
      console.writeline("here is a list of all the files in this isolated store:");
      foreach(string file in getallfiles("*", isostore)){
         console.writeline(file);
      }  
 
   }// end of main.
  
   // method to retrieve all directories, recursively, within a store.
 
   public static string[] getalldirectories(string pattern, isolatedstoragefile storefile){
 
      // get the root of the search string.
 
      string root = path.getdirectoryname(pattern);
 
      if (root != "") root += "/";
 
      // retrieve directories.
 
      string[] directories;
 
      directories = storefile.getdirectorynames(pattern);
 
      arraylist directorylist = new arraylist(directories);
 
      // retrieve subdirectories of matches.
 
      for (int i = 0, max = directories.length; i < max; i++){
         string directory = directorylist[i] + "/";
         string[] more = getalldirectories (root + directory + "*", storefile);
 
         // for each subdirectory found, add in the base path.
 
         for (int j = 0; j < more.length; j++)
            more[j] = directory + more[j];
 
         // insert the subdirectories into the list and 
         // update the counter and upper bound.
 
         directorylist.insertrange(i+1, more);
         i += more.length;
         max += more.length;
      }
 
      return (string[])directorylist.toarray(type.gettype("system.string"));
   }
 
   public static string[] getallfiles(string pattern, isolatedstoragefile storefile){
 
      // get the root and file portions of the search string.
 
      string filestring = path.getfilename(pattern);
 
      string[] files;
      files = storefile.getfilenames(pattern);
 
      arraylist filelist = new arraylist(files);
 
      // loop through the subdirectories, collect matches, 
      // and make separators consistent.
 
      foreach(string directory in getalldirectories( "*", storefile))
         foreach(string file in storefile.getfilenames(directory + "/" + filestring))
            filelist.add((directory + "/" + file));
 
      return (string[])filelist.toarray(type.gettype("system.string"));
         
   }// end of getfiles.
 
}

讀取和寫入文件


使用 isolatedstoragefilestream 類,有多種方法可以打開存儲區中的文件。一旦獲得了 isolatedstoragefilestream 之后,可使用它來獲取 streamreader 或 streamwriter。使用 streamreaderstreamwriter,您可以像對任何其他文件一樣讀取和寫入存儲區中的文件。

readingandwritingtofiles 示例


下面的代碼示例獲得獨立存儲區,創建一個名為 teststore.txt 的文件并將“hello isolated storage”寫入文件。然后,代碼讀取該文件并將結果輸出到控制臺。
 [c#]
using system;
using system.io;
using system.io.isolatedstorage;
 
public class readingandwritingtofiles{
 
   public static int main(){
 
      // get an isolated store for this assembly and put it into an
      // isolatedstorefile object.
 
      isolatedstoragefile isostore =  isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.assembly, null, null);
 
      // this code checks to see if the file already exists.
 
      string[] filenames = isostore.getfilenames("teststore.txt");
      foreach (string file in filenames){
         if(file == "teststore.txt"){
 
            console.writeline("the file already exists!");
            console.writeline("type /"storeadm /remove/" at the command line to delete all isolated storage for this user.");
 
            // exit the program.
 
            return 0;
         }
      }
 
      writetofile(isostore);
 
      console.writeline("the file /"teststore.txt/" contains:");
      // call the readfromfile and write the returned string to the
      //console.
 
      console.writeline(readfromfile(isostore));
 
      // exit the program.
 
      return 0;
 
   }// end of main.
 
 
   // this method writes "hello isolated storage" to the file.
 
   private static void writetofile(isolatedstoragefile isostore){
 
      // declare a new streamwriter.
 
      streamwriter writer = null;
 
      // assign the writer to the store and the file teststore.
 
      writer = new streamwriter(new isolatedstoragefilestream("teststore.txt", filemode.createnew,isostore));
 
      // have the writer write "hello isolated storage" to the store.
 
      writer.writeline("hello isolated storage");
 
      writer.close();
      
      console.writeline("you have written to the file.");
 
   }// end of writetofile.
 
 
   // this method reads the first line in the "teststore.txt" file.
 
   public static string readfromfile(isolatedstoragefile isostore){
 
      // this code opens the teststore.txt file and reads the string.
 
      streamreader reader = new streamreader(new isolatedstoragefilestream("teststore.txt", filemode.open,isostore));
 
      // read a line from the file and add it to sb.
 
      string sb = reader.readline();
 
      // close the reader.
 
      reader.close();
 
      // return the string.
 
      return sb.tostring();
 
   }// end of readfromfile.
}

刪除文件和目錄


您可以刪除獨立存儲文件中的目錄和文件。請記住,在存儲區中,文件名和目錄名是與操作系統相關的(在 microsoft windows 系統中通常不區分大小寫),并且是根據虛文件系統的根目錄具體而定的。
isolatedstorefile 類提供了兩種刪除目錄和文件的實例方法:deletedirectory 和 deletefile。如果嘗試刪除并不存在的文件和目錄,則會引發 isolatedstoragefileexception。如果名稱中包含有通配符,則 deletedirectory 會引發 isolatedstoragefileexception,而 deletefile 將引發 argumentexception。
如果目錄中包含任何文件或子目錄,deletedirectory 將會失敗。在 deletingfilesanddirectories 示例的一部分中定義了一個方法,該方法刪除目錄中的所有內容,然后刪除目錄本身。同樣,您可以自己定義一個接受通配符的 deletefiles 方法,該方法可以這樣來實現:使用 getfilenames 方法獲取所有匹配文件的列表,然后依次刪除每個文件。

deletingfilesanddirectories 示例


下面的代碼示例先創建若干個目錄和文件,然后將它們刪除。
 [c#]
using system;
using system.io.isolatedstorage;
using system.io;
 
public class deletingfilesdirectories{
 
   public static void main(){
 
      // get a new isolated store for this user domain and assembly.
      // put the store into an isolatedstoragefile object.
 
      isolatedstoragefile isostore =  isolatedstoragefile.getstore(isolatedstoragescope.user | isolatedstoragescope.domain | isolatedstoragescope.assembly, null, null);
    
      console.writeline("creating directories:");
 
      // this code creates several different directories.
 
      isostore.createdirectory("topleveldirectory");
      console.writeline("topleveldirectory");
      isostore.createdirectory("topleveldirectory/secondlevel");
      console.writeline("topleveldirectory/secondlevel");
 
      // this code creates two new directories, one inside the other.
 
      isostore.createdirectory("anothertopleveldirectory/insidedirectory");
      console.writeline("anothertopleveldirectory/insidedirectory");
      console.writeline();
 
      // this code creates a few files and places them in the directories.
 
      console.writeline("creating files:");
 
      // this file is placed in the root.
 
      isolatedstoragefilestream isostream1 = new isolatedstoragefilestream("intheroot.txt", filemode.create, isostore);
      console.writeline("intheroot.txt");
  
      isostream1.close();
 
      // this file is placed in the insidedirectory.
 
      isolatedstoragefilestream isostream2 = new isolatedstoragefilestream("anothertopleveldirectory/insidedirectory/hereiam.txt", filemode.create, isostore);
      console.writeline("anothertopleveldirectory/insidedirectory/hereiam.txt");
      console.writeline();
 
      isostream2.close();
 
      console.writeline("deleting file:");
 
      // this code deletes the hereiam.txt file.
      isostore.deletefile("anothertopleveldirectory/insidedirectory/hereiam.txt");
      console.writeline("anothertopleveldirectory/insidedirectory/hereiam.txt"); 
      console.writeline();
 
      console.writeline("deleting directory:");
 
      // this code deletes the insidedirectory.
 
      isostore.deletedirectory("anothertopleveldirectory/insidedirectory/");
      console.writeline("anothertopleveldirectory/insidedirectory/");
      console.writeline();
 
   }// end of main.
 
}

總結


    上面是vs.net中.net中io的基本概念、示例代碼以及訪問文件系統的基礎方法和流程,大家可以多多實踐。有任何建議請mail我 [email protected]([email protected])。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 天全县| 永修县| 任丘市| 利辛县| 扬州市| 阿城市| 武定县| 金昌市| 博客| 镇江市| 武功县| 玉田县| 榆树市| 无棣县| 蒙阴县| 鹿泉市| 桐梓县| 葵青区| 靖远县| 宣化县| 乌海市| 安新县| 焦作市| 镇宁| 泗洪县| 子长县| 台湾省| 新建县| 台东市| 丹寨县| 鄂尔多斯市| 盐池县| 定襄县| 元氏县| 临安市| 类乌齐县| 政和县| 宣汉县| 绥阳县| 柘荣县| 台南市|