2009年12月20日 星期日

IsolatedStorage

命名空間:System.IO.IsolatedStorage
組件:mscorlib.dll
IsolatedStorage類別:所有隔離儲存區的基底類別(抽象類別)

IsolatedStorageFile類別:表示含有檔案和目錄隔離儲存區

IsolatedStorageScope enumeration:列舉IsolatedStorage所支援的隔離儲存區範圍層次

IsolatedStorageFileStream類別:公開隔離區(Isolated Storage)中的檔案
  • Application 範圍限定於應用程式的隔離儲存區。
  • Assembly 範圍限定於組件 (Assembly) 識別 (Identity) 的隔離儲存區。
  • Domain 範圍限定於應用程式定義域識別的隔離儲存區。
  • Machine 範圍限定於電腦的隔離儲存區。
  • None 沒有隔離儲存區的使用。
  • Roaming 隔離儲存區可以放置在可能漫遊的檔案系統某一處 (如果漫遊使用者資料在基礎作業系統中啟用)。
  • User 由使用者識別限定範圍的隔離儲存區。
C#
Example 1
IsolatedStorageFile isoFile =
IsolatedStorageFile.GetStore(IsolatedStorageScope.User |
IsolatedStorageScope.Assembly |
IsolatedStorageScope.Domain,
null,
null);

IsolatedStorageFileStream isoStream =
new IsolatedStorageFileStream(this.userName,
FileMode.Open,
FileAccess.Read,
FileShare.Read);

Example 2

IsolatedStorageFile isoFile;
private void Form1_Load(object sender, EventArgs e)
{
isoFile = IsolatedStorageFile.GetMachineStoreForDomain();
}

private void button1_Click(object sender, EventArgs e)
{
String fileName = textBox1.Text;
try{
using (FileStream fs= new IsolatedStorageFileStream(fileName,
FileMode.Open, FileAccess.Read, isoFile))
{
StreamReader sr = new
StreamReader(fs,Encoding.GetEncoding("big5"));
textBox2.Text=sr.ReadToEnd();
sr.Close();
}
}catch(Exception ex){
MessageBox.Show(ex.Message);
}
}


MarshalByRefobject

MarshalByRefobject類別
  啟用在支援Remoting Application中跨越Application domain boundaries中的存取。
命名空間:System
組件:mscorlib.dll
有兩種型態的遠端物件
  • Marshal-by-value objects:藉由「值」,這些物件被複製和傳遞,傳出應用程式定義域
  • Marshal-by-reference objects:藉由使用「Proxy」這些物件被存取外部應用程式定義域。Client需要「Proxy」來使用這些物件,去存取這些遠端的物件。

當跨越應用程式定義域界限來使用型別時,型別必須繼承自 MarshalByRefObject,並且不可以複製物件的狀態,因為物件的成員無法在建立成員的所在應用程式定義域外部使用。

using System;
using System.Runtime.Remoting;
using System.Security.Permissions;
public class SetObjectUriForMarshalTest {
class TestClass : MarshalByRefObject {
}
[SecurityPermission(SecurityAction.LinkDemand)]
public static void Main() {
TestClass obj = new TestClass();
RemotingServices.SetObjectUriForMarshal(obj, "testUri");
RemotingServices.Marshal(obj);
Console.WriteLine(RemotingServices.GetObjectUri(obj));
}
}