C# 使用文件流和字典<;字符串,字符串>;在C语言中,带二进制格式化程序的';似乎工作不正常。
基本上,我试图将C# 使用文件流和字典<;字符串,字符串>;在C语言中,带二进制格式化程序的';似乎工作不正常。,c#,serialization,dictionary,filestream,C#,Serialization,Dictionary,Filestream,基本上,我试图将字典对象序列化到一个文件中,稍后再将其加载回来。我知道我可能做错了什么。(是的,这是一个irc bot)。问题是字典的全部内容没有保存到文件中,在我的调试窗口中,我得到了“未保存”文本,这意味着它可能没有保存。 它有时也会崩溃(离开聊天室然后返回) 关于这件事我只能说这些了。。如果我做错了什么,我想可能是因为我的嵌套语句或文件处理程序没有得到正确的处理 我有两个父函数,save和load,但是编译器不喜欢我在IRCBOT类中调用它们 以下是完整的代码: using System;
字典
对象序列化到一个文件中,稍后再将其加载回来。我知道我可能做错了什么。(是的,这是一个irc bot
)。问题是字典的全部内容没有保存到文件中,在我的调试窗口中,我得到了“未保存”文本,这意味着它可能没有保存。
它有时也会崩溃(离开聊天室然后返回)
关于这件事我只能说这些了。。如果我做错了什么,我想可能是因为我的嵌套语句或文件处理程序没有得到正确的处理
我有两个父函数,save和load,但是编译器不喜欢我在IRCBOT
类中调用它们
以下是完整的代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading;
/*
* This program establishes a connection to irc server and joins a channel. Thats it.
*
* Coded by Pasi Havia 17.11.2001 http://koti.mbnet.fi/~curupted
*
* Updated / fixed by Blake 09.10.2010
*/
class IrcBot
{
//Save the file
public bool SaveFile(string fullpath = Filename)
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = File.Create(fullpath);
formatter.Serialize(stream, C);
stream.Close();
}
catch
{
return false;
}
return true;
}
//Load a file
public bool LoadFile(string fullpath = Filename)
{
try
{
if (!File.Exists(fullpath))
return false;
BinaryFormatter formatter = new BinaryFormatter();
FileStream stream = File.OpenRead(fullpath);
C = (Dictionary<string, string>)formatter.Deserialize(stream);
stream.Close();
}
catch
{
return false;
}
return true;
}
// Irc server to connect
public static string SERVER = "irc.caffie.net";
// Irc server's port (6667 is default port)
private static int PORT = 6667;
// User information defined in RFC 2812 (Internet Relay Chat: Client Protocol) is sent to irc server
private static string USER = "USER IrcBot 0 * :IrcBot";
// Bot's nickname
private static string NICK = "Pinkbot";
// Channel to join
private static string CHANNEL = "#testingbots";
//The dictionary for messages
// private Dictionary<int, string> D = new Dictionary<int, string>();
public static Dictionary<string, string> C = new Dictionary<string, string>();
public const string fullpath = "bot_knowledge.txt";
public const string Filename = "bot_knowledge.txt";
public static StreamWriter writer;
static bool responded;
static bool shutup;
private static void Add_Response(string a, string b)
{
C.Add(a, b);
}
public static void Main(string[] args)
{
NetworkStream stream;
TcpClient irc;
string inputLine;
StreamReader reader;
try
{
//C.Add("what's up?", "nothing much");
// C.Add("meep", "mope");
// C.Add("hey", "what?");
// C.Add("what is", "it's two. ");
// C.Add("This", "that");
// C.Add("okay", "it is okay.");
// C.Add("is it?", "yeah..");
// C.Add("who are you", "I'm a bot");
// C.Add("Who are you?", "I am a bot");
// C.Add("not you", "oh I'm sorry");
// C.Add("what?", "nothing... just processing");
// C.Add("What?", "nothing just you know. :3");
// C.Add("help", "With ? ");
// C.Add("help me", "I can't I'm a bot.");
//C.Add("hm", "thinking deeply about something are we?");
// C.Add("yes", "oh no. ");
// C.Add("no", "OHHHH YESSS");
// C.Add("super mario world", "yes");
// C.Add("SMWC", "That's this place.. isn't it?");
// C.Add("smwc", "on our website!");
// C.Add("lol", "AHAHAHAHAHAHAHA!");
// C.Add("lel", "It's LOL");
// C.Add("!help", "use '!'shutup or '!'reset to change states omit '' see help2 for more");
// C.Add("!help2", "usage of '!'add is '!add' <yourphrasehere> '!'and <yoursecond phrase here> omit the ' ' ");
irc = new TcpClient(SERVER, PORT);
stream = irc.GetStream();
reader = new StreamReader(stream);
writer = new StreamWriter(stream);
writer.WriteLine("NICK " + NICK);
writer.Flush();
writer.WriteLine(USER);
writer.Flush();
while (true)
{
while ((inputLine = reader.ReadLine()) != null)
{
if (inputLine.Contains("!shutup"))
{
shutup = true;
}
Console.WriteLine("<-" + inputLine);
//=======Here data is saved or loaded manually======//
if (inputLine.Contains("!load"))
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream st = File.OpenRead(fullpath);
C = (Dictionary<string, string>)formatter.Deserialize(st);
st.Close();
}
catch
{
Console.WriteLine("Didn't load");
}
writer.WriteLine("PRIVMSG " + CHANNEL + " :Data was loaded, presumably." + "\r\n");
writer.Flush();
}
if (inputLine.Contains("!save"))
{
try
{
BinaryFormatter formatter = new BinaryFormatter();
if (File.Exists(fullpath))
{
FileStream sa = File.OpenWrite(fullpath);
formatter.Serialize(sa, C);
sa.Close();
}
else
File.Delete(fullpath);
FileStream sc = File.Create(fullpath);
formatter.Serialize(sc, C);
sc.Close();
}
catch
{
Console.WriteLine("Wasn't saved...");
}
writer.WriteLine("PRIVMSG " + CHANNEL + " :Data was saved, presumably." + "\r\n");
writer.Flush();
}
//==============================================//
if (inputLine.Contains("!add"))
{
string[] parts = inputLine.Split(new Char[] { ' ' });
int x = parts.Count();
int splitstart = 0;
string a = "";
string b = "";
for (int t = 4; t < x; t++)
{
if (parts[t] == "!and")
{
splitstart = t;
break;
}
if (t == 4)
{
a = a + parts[t];
}
else
a = a + " " + parts[t];
}
//now that i found splitstart i can do this
if (splitstart > 0)
{
for (int tt = splitstart+1; tt<x; tt++)
{
if (tt == splitstart + 1)
{
b = b + parts[tt];
}
else
b = b + " " + parts[tt];
}
Add_Response(a, b);
writer.WriteLine("PRIVMSG " + CHANNEL + " :added " +a+" ->"+b+" to my dictionary"+ "\r\n");
writer.Flush();
}
}
if (responded == true)
{
responded = false;
}
// Split the lines sent from the server by spaces. This seems the easiest way to parse them.
string[] splitInput = inputLine.Split(new Char[] { ' ' });
foreach (string it in splitInput)
{
if (it.Contains("!reset"))
{
if (responded == true)
{
writer.WriteLine("PRIVMSG " + CHANNEL + " :variable 'responded' was set to true" + "\r\n");
writer.Flush();
}
if (responded == false)
{
writer.WriteLine("PRIVMSG " + CHANNEL + " :variable 'responded' was set to false" + "\r\n");
writer.Flush();
}
responded = false;
shutup = false;
}
//if (it.Contains("hello"))
//{
// writer.WriteLine("PRIVMSG " + CHANNEL + " :hey" + "\r\n");
//writer.Flush();
// }
if (responded == false)
{
if (!shutup)
{
foreach (KeyValuePair<string, string> entry in C)
{
// Console.WriteLine("<- was split to part " + it);
if (it.StartsWith(entry.Key))
{
string output = entry.Value;
writer.WriteLine("PRIVMSG " + CHANNEL + " :" + output + "\r\n");
writer.Flush();
responded = true;
}
else
if (inputLine.Contains(entry.Key))
{
string output = entry.Value;
writer.WriteLine("PRIVMSG " + CHANNEL + " :" + output + "\r\n");
writer.Flush();
responded = true;
}
}
}
}
}
if (splitInput[0] == "PING")
{
string PongReply = splitInput[1];
//Console.WriteLine("->PONG " + PongReply);
writer.WriteLine("PONG " + PongReply);
writer.Flush();
continue;
}
switch (splitInput[1])
{
// This is the 'raw' number, put out by the server. Its the first one
// so I figured it'd be the best time to send the join command.
// I don't know if this is standard practice or not.
case "001":
string JoinString = "JOIN " + CHANNEL;
writer.WriteLine(JoinString);
writer.Flush();
break;
case "002":
break;
default:
break;
}
}
// Close all streams
writer.Close();
reader.Close();
irc.Close();
}
}
catch (Exception e)
{
// Show the exception, sleep for a while and try to establish a new connection to irc server
Console.WriteLine(e.ToString());
Thread.Sleep(5000);
string[] argv = { };
Main(argv);
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.IO;
使用System.Linq;
Net系统;
使用System.Net.Sockets;
使用System.Runtime.Serialization.Formatters.Binary;
使用系统文本;
使用系统线程;
/*
*这个程序建立了到irc服务器的连接并加入了一个通道。就是这样。
*
*由Pasi Havia 17.11.2001编码http://koti.mbnet.fi/~z~昏昏欲睡
*
*Blake于2010年10月9日更新/修复
*/
类IrcBot
{
//保存文件
public bool SaveFile(字符串fullpath=Filename)
{
尝试
{
BinaryFormatter formatter=新的BinaryFormatter();
FileStream stream=File.Create(完整路径);
序列化(流,C);
stream.Close();
}
接住
{
返回false;
}
返回true;
}
//加载文件
公共bool加载文件(字符串完整路径=文件名)
{
尝试
{
如果(!File.Exists(完整路径))
返回false;
BinaryFormatter formatter=新的BinaryFormatter();
FileStream stream=File.OpenRead(完整路径);
C=(字典)格式化程序。反序列化(流);
stream.Close();
}
接住
{
返回false;
}
返回true;
}
//要连接的Irc服务器
公共静态字符串服务器=“irc.caffie.net”;
//Irc服务器的端口(6667为默认端口)
专用静态int端口=6667;
//RFC 2812(Internet中继聊天:客户端协议)中定义的用户信息被发送到irc服务器
私有静态字符串USER=“用户IrcBot 0*:IrcBot”;
//机器人的昵称
私有静态字符串NICK=“Pinkbot”;
//加入渠道
专用静态字符串通道=“#测试机器人”;
//信息字典
//私有字典D=新字典();
公共静态字典C=新字典();
public const string fullpath=“bot\u knowledge.txt”;
public const string Filename=“bot_knowledge.txt”;
公共静态流作者;
静态布尔响应;
静态布尔闭嘴;
私有静态void Add_响应(字符串a、字符串b)
{
C.添加(a,b);
}
公共静态void Main(字符串[]args)
{
网络流;
TCP客户端irc;
字符串输入线;
流阅读器;
尝试
{
//加上(“怎么了?”,“没什么”);
//C.添加(“meep”、“mope”);
//加上(“嘿”,“什么?”);
//C.加上(“what is”,“it's two.”);
//C.添加(“该”、“该”);
//C.加上(“好的”,“没事的”);
//C.加上(“是吗?”,“是的…”);
//加上(“你是谁”,“我是机器人”);
//加上(“你是谁?”,“我是机器人”);
//加上(“不是你”,“哦,对不起”);
//C.添加(“什么?”,“没有……只是处理”);
//C.加上(“什么?”,“没什么只是你知道的。:3”);
//C.添加(“帮助”、“帮助”);
//C.加上(“帮帮我”,“我不行,我是个机器人”);
//C.加上(“嗯”,“深入思考我们是什么样的人?”);
//C.加上(“是”、“哦不是”);
//C.添加(“否”、“OHHHH是”);
//C.添加(“超级马里奥世界”,“是”);
//加上(“SMWC”,“就是这个地方……不是吗?”);
//添加(“smwc”,“在我们的网站上!”);
//C.加上(“lol”,“哈哈哈!”);
//C.添加(“lel”,“它是LOL”);
//C.添加(“!help”,“使用'!'关闭或'!'重置以更改状态,省略''',有关更多信息,请参阅帮助2”);
//C.添加(“!help2”,“使用“!”添加为“!添加”!”并省略“”);
irc=新的TCP客户端(服务器、端口);
stream=irc.GetStream();
读卡器=新的流读卡器(流);
writer=新的流writer(流);
作者:WriteLine(“尼克”+NICK);
writer.Flush();
writer.WriteLine(用户);
writer.Flush();
while(true)
{
而((inputLine=reader.ReadLine())!=null)
{
if(inputLine.Contains(“!shutup”))
{
闭嘴=正确;
}
Console.WriteLine(“+b+”到我的字典“+”\r\n”);
writer.Flush();
}
}
如果(响应==真)
{
回答=假;
}
//将服务器发送的行按空格分开。这似乎是解析它们的最简单方法。
string[]splitInput=inputLine.Split(新字符[]{''});
foreach(在splitInput中字符串)
{
如果(它包含(“!重置”))
{
如果(响应==真)
{
writer.WriteLine(“privmg”+CHANNEL+”:变量“responsed”被设置为true“+”\r\n”);
try
{
BinaryFormatter formatter = new BinaryFormatter();
if (File.Exists(fullpath))
{
FileStream sa = File.OpenWrite(fullpath);
formatter.Serialize(sa, C);
sa.Close();
}
else
File.Delete(fullpath);
FileStream sc = File.Create(fullpath);
formatter.Serialize(sc, C);
sc.Close();
}
catch
{
Console.WriteLine("Wasn't saved...");
}