人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統下載站 > 技術開發教程 > 詳細頁面

運用 .NET的IO(5)    Paul_Ni(原作)

運用 .NET的IO(5) Paul_Ni(原作)

更新時間:2022-06-06 文章作者:未知 信息來源:網絡 閱讀次數:

查找現有的文件和目錄


您還可以使用獨立存儲文件來搜索現有的目錄和文件。請記住,在存儲區中,文件名和目錄名是相對于虛文件系統的根目錄指定的。此外,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我 paulni@citiz.net(paulni@citiz.net)。

溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統下載排行

網站地圖xml | 網站地圖html
主站蜘蛛池模板: 灵武市| 巴里| 龙里县| 泾阳县| 龙口市| 历史| 潮安县| 海门市| 东乡县| 永登县| 万荣县| 孝感市| 淳安县| 抚顺县| 岳普湖县| 广汉市| 清水河县| 泽库县| 霍邱县| 当阳市| 武川县| 双流县| 隆安县| 辰溪县| 嘉义市| 合阳县| 静宁县| 克拉玛依市| 正定县| 格尔木市| 巴彦县| 碌曲县| 咸宁市| 高密市| 靖宇县| 汶川县| 阳春市| 抚顺县| 聂荣县| 杭州市| 灌阳县|