Use the WebRequest class found in the System.Net namespace.
//create the request object
WebRequest req = WebRequest.Create(@'http://www.syncfusion.com');
//get the response and use the response
WebResponse resp = req.GetResponse();
Stream stream = resp.GetResponseStream();
StreamReader sr = new StreamReader(stream);
string s = sr.ReadToEnd();
textBox1.Text = s;
This is usually an issue when a newer version introduces newer names, then the older version will not serialize property due to the absence of certain names. For example, this code will fail, sometimes:
protectedMyClassConstructor(SerializationInfo info, StreamingContext context)
{
...
// This might fail if MyProp was added in a newer version and you are serializing an older version.this.MyProp = info.GetBoolean('MyProp');
}
To avoid such conflicts, you could insert version nos. into the serialized info. and during deserialization check for a name only when a particular version is being deserialized. Or you could instead parse through the available info in the SerializationInfo list as follows:
[C#]
protectedMyClassConstructor(SerializationInfo info, StreamingContext context)
{
foreach(SerializationEntry entry in info)
{
switch(entry.Name)
{
case'MyProp':
// This will make sure that older versions without the MyProp name will also deserialize without any problemsthis.MyProp = (bool)entry.Value;
break;
...
}
}
}
[VB.Net]
Protected MyClassConstructor(ByVal info As SerializationInfo, ByVal context As StreamingContext) As Protected
Dim entry As SerializationEntry
For Each entry In info
SelectCase entry.Name
Case'MyProp'
’ This will make sure that older versionswithout the MyProp name will also deserialize withoutany problems
Me.MyProp = (Boolean)entry.Value
ExitForEndSelectNextEndFunction
This is possible in this case: Say your object graph contains an object A, which has a reference to the object B. Then while deserializing A, the reference B might not be initialized yet. This is because while deserializing, references are deserialized one at a time and when A is deserialized, B might not have been deserialized, yet. You should follow the workaround as follows:
[C#]
protectedMyCustomConstrucotr(SerializationInfo info, StreamingContext context)
{
this.cachedRefToB = (B)info.GetValue('B', typeof(B));
// At this point cachedRefToB might not be initialized.
}
// But when this method gets called, after complete serialization, the cachedRefToB will be initializedvoid IDeserializationCallback.OnDeserialization(object sender)
{
// At this point cachedRefToB will be initialized.
}
[VB.Net]
protected MyCustomConstrucotr(ByVal info As SerializationInfo, ByVal context As StreamingContext) As Protected
Me.cachedRefToB = CType(info.GetValue('B', Type.GetType(B)), B)
’ At this point cachedRefToB might not be initialized.
End Function
’ But whenthis method gets called, after complete serialization, the cachedRefToB will be initialized
’ Your classshouldimplementIDeserializationCallback
Sub OnDeserialization(ByVal sender As Object) as IDeserializationCallback.OnDeserialization
’ At this point cachedRefToB will be initialized.
End Sub
Use FileSystemWatcher to watch for changes in a specified directory. You can watch for changes in files and subdirectories of the specified directory. The component can watch files on a local computer, a network drive, or a remote computer.
Yes. This may not be the most efficient way to read from a ini file but it is a pretty good solution that is easy to maintain.
You can use a XML file to store your settings. Use a DataSet to read from it. Consider a simple sample file, config.ini. It has three parameters base_path, update_path and output_file. These will map to columns in the settings datatable.
You can of course use XmlReader, XmlDocument etc to create and maintain more complex ini files. But this is a quick way to maintain a simple property set.
To store an object in the registry, the object should be serializable (either has a Serializable attribute attached to it or derives from ISerializable; same holds to all contained objects).
ArrayList names; // Source object; Can contain any object that is serializable
... // Fill up this arraylist
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream1 = new MemoryStream();
formatter.Serialize(stream1, names);
RegistryKey regKey;
... // Open the key where you want to store it, with write permissions
regKey.SetValue('ValueName', stream1.ToArray());
To Read from registry:
ArrayList names; // Destination object
RegKey regKey;
... // Open the corresponding key
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream1 = new MemoryStream();
byte[] barray1 = null;
barray1 = (byte[])regKey.GetValue('ValueName');
if(barray1 != null)
{
stream1.Write(barray1, 0, barray1.Length);
MemoryStream stream1 = new MemoryStream();
byte[] barray1 = null;
barray1 = (byte[])regKey.GetValue('ValueName');
if(barray1 != null)
{
stream1.Write(barray1, 0, barray1.Length);
stream1.Position = 0;
names = formatter.Deserialize(stream1) as ArrayList;
}
It doesn’t get any simpler than this. In production code always make sure that you handle exceptions.
using System.IO;
....
// filePath has the complete path to the file
StreamWriter writer = new StreamWriter(filePath);
writer.Write(fileNewText);
writer.Close();
The following code reads a text file into a string object. It doesn’t get any simpler than this. In production code always make sure that you handle exceptions.
using System.IO;
....
// filePath should contain the complete path to a file.
StreamReader stream = new StreamReader(filePath);
fileText = stream.ReadToEnd();
stream.Close();