一、利用.NET做“电子书阅读器”原来这么简单
先下载组件:Interop.SpeechLib.dll (http://files.cnblogs.com/waw/Interop.SpeechLib.rar)
阅读能力:中文,英文...
调用代码如下:
/// <summary>
/// 利用“SpeechLib组件”调用“windows系统声音”来读取“文本”内容
/// </summary>
/// <param name="str"></param>
private void SpeakText(string str)
{
SpeechVoiceSpeakFlags sVSFlagsAsync = SpeechVoiceSpeakFlags.SVSFlagsAsync;
SpVoice voice = new SpVoiceClass();
voice.Rate = 1;
voice.Speak(str, sVSFlagsAsync);
}
原理:利用windows系统内置的声音API接口完成读取
--------------------------------------------------华丽分割线(与此文无关,仅为知识点备份)-------------------------------------------
二、Hashtable格式数据与文件存储
示例代码如下:
/// <summary>
/// 读取 data.dat 中的数据
/// </summary>
private void Deserialize()
{
FileStream serializationStream = new FileStream(Application.StartupPath + @"\data.dat", FileMode.Open);
try
{
BinaryFormatter formatter = new BinaryFormatter();
Hashtable hst = (Hashtable)formatter.Deserialize(serializationStream);
foreach (DictionaryEntry entry in hst)
{
//读取数据...
}
}
catch (SerializationException exception)
{
MessageBox.Show(exception.Message);
}
finally
{
serializationStream.Close();
}
}
/// <summary>
/// 保存Hashtable格式的数据为文件
/// </summary>
private void Serialize()
{
FileStream serializationStream = new FileStream(Application.StartupPath + @"\data.dat", FileMode.Create);
BinaryFormatter formatter = new BinaryFormatter();
Hashtable hst = new Hashtable();
//设置Hashtable数据值
hst.Add("key1", "value1");
hst.Add("key2", "value2");
formatter.Serialize(serializationStream, hst);
serializationStream.Close();
}
代码中实现了将Hashtable格式的数据另存为文件,和读取Hashtable格式的数据为对象类型