获取序列化的字符串表示形式更改标记集合。
命名空间: Microsoft.SharePoint
程序集: Microsoft.SharePoint(位于 Microsoft.SharePoint.dll 中)
语法
声明
Public Overrides Function ToString As String
用法
Dim instance As SPChangeTokenCollection
Dim returnValue As String
returnValue = instance.ToString()
public override string ToString()
返回值
类型:System.String
一个字符串,包含该集合的序列化的字符串表示形式。
备注
您可以使用此方法以序列化更改标记集合之前将它保持到永久存储区。要重建集合,请向SPChangeTokenCollection构造函数接受字符串重载传递集合的序列化的字符串表示形式。
示例
下面的示例包含两个例程来自较大的应用程序。第一种是将更改标记集合写入到磁盘上的文件的过程。第二个是一个函数,接受文件名作为一个参数,将打开该文件,读取它找到并使用此信息来创建一个令牌更改集合,该函数的返回值的第一个序列化的字符串变量。注意是否文件找不到或不包含格式正确的字符串变量,该函数将返回一个空集合。
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;
}