共用方式為


SPChangeTokenCollection.ToString method

取得變更的語彙基元集合的序列化的字串表示。

Namespace:  Microsoft.SharePoint
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'宣告
Public Overrides Function ToString As String
'用途
Dim instance As SPChangeTokenCollection
Dim returnValue As String

returnValue = instance.ToString()
public override string ToString()

傳回值

Type: System.String
字串,包含集合的序列化的字串表示。

備註

您可以使用這個方法來序列化變更語彙基元集合之前保存到永久儲存區。若要重建集合,請將集合的序列化的字串表示傳遞至接受字串的SPChangeTokenCollection建構函式多載。

Examples

下列範例包含兩個常式從較大的應用程式。首先,將變更的語彙基元集合寫入至磁碟上的檔案的程序。第二個是函式接受檔案名稱做為引數,會開啟檔案,讀取它找到,並使用它來建立變更語彙基元集合,函式的傳回值的第一個序列化的字串變數。請注意是否找不到檔案,或不包含格式正確的字串變數,函式會傳回空集合。

Sub SaveTokens(ByRef tokens As SPChangeTokenCollection, ByVal filePath As String)
  Using fs As FileStream = File.Create(filePath)
     ' Serialize the tokens
     Dim bw As BinaryWriter = New BinaryWriter(fs)
     Dim s As String = tokens.ToString()
     bw.Write(s)

     ' flush and close
     bw.Flush()
     bw.Close()
  End Using
End Sub

Function GetTokens(ByVal filePath As String) As SPChangeTokenCollection

  Dim tokens As SPChangeTokenCollection = New SPChangeTokenCollection()

  ' If we have a persisted collection, use it
  If File.Exists(filePath) Then
     Using fs As FileStream = File.OpenRead(filePath)
        Dim br As BinaryReader = New BinaryReader(fs)
        Try
           Dim s As String = br.ReadString()
           ' Construct a change token from string
           tokens = New SPChangeTokenCollection(s)
        Catch 
           ' No serialized string, or an incorrectly formatted string.
           ' Do nothing. We'll return an empty collection.
        Finally
           br.Close()
        End Try
     End Using
  End If
  Return tokens
End Function
static void SaveTokens(SPChangeTokenCollection tokens, string filePath)
{
   using (FileStream fs = File.Create(filePath))
   {
      // Serialize the tokens
      BinaryWriter bw = new BinaryWriter(fs);
      string s = tokens.ToString();
      bw.Write(s);

      // flush and close
      bw.Flush();
      bw.Close();
   }
}

static SPChangeTokenCollection GetTokens(string filePath)
{
   SPChangeTokenCollection tokens = new SPChangeTokenCollection();

   // If we have a persisted collection, use it
   if (File.Exists(filePath))
   {
      using (FileStream fs = File.OpenRead(filePath))
      {
         BinaryReader br = new BinaryReader(fs);
         try
         {
            string s = br.ReadString();
            // Construct a change token from string
            tokens = new SPChangeTokenCollection(s);
         }
         catch 
         {
            // No serialized string, or an incorrectly formatted string.
            // Do nothing. We'll return an empty collection.
         }
         finally
         {
            br.Close();
         }
      }
   }

   return tokens;
}

請參閱

參照

SPChangeTokenCollection class

SPChangeTokenCollection members

Microsoft.SharePoint namespace

ToString()