SPChangeTokenCollection.ToString - Méthode
Obtient la représentation sous forme de chaîne sérialisée de la collection de jetons de modification.
Espace de noms : Microsoft.SharePoint
Assembly : Microsoft.SharePoint (dans Microsoft.SharePoint.dll)
Syntaxe
'Déclaration
Public Overrides Function ToString As String
'Utilisation
Dim instance As SPChangeTokenCollection
Dim returnValue As String
returnValue = instance.ToString()
public override string ToString()
Valeur renvoyée
Type : System.String
Chaîne qui contient la représentation sous forme de chaîne sérialisée de la collection.
Remarques
Vous pouvez utiliser cette méthode pour sérialiser une collection de jetons de modification avant du rendre persistantes dans un stockage permanent. Pour reconstruire la collection, passez la représentation sous forme de chaîne sérialisée de la collection à la surcharge du constructeur SPChangeTokenCollection qui accepte une chaîne.
Exemples
L'exemple suivant se compose de deux routines à partir d'une application plus importante. La première est une procédure qui écrit une collection de jetons de modification dans un fichier sur le disque. La seconde est une fonction qui accepte un nom de fichier en tant qu'argument, ouvre le fichier, lit la première variable de chaîne sérialisée qu'elle détecte et utilise ce dernier pour créer une collection de jetons de modification, valeur de retour de la fonction. Notez que si le fichier est introuvable ou ne contient-elle pas une variable de chaîne mise en forme correctement, la fonction retourne une collection vide.
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;
}
Voir aussi
Référence
SPChangeTokenCollection classe
SPChangeTokenCollection - Membres