Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#Void不';除了日志和返回true之外,您不做任何其他事情吗?_C#_Steambot - Fatal编程技术网

C#Void不';除了日志和返回true之外,您不做任何其他事情吗?

C#Void不';除了日志和返回true之外,您不做任何其他事情吗?,c#,steambot,C#,Steambot,我对SteamBot代码有问题。所以,如果有人添加“机器人”作为好友,那么它将在日志中显示添加者。它做到了这一点,在那之后我添加了更多的代码,但那部分它就是做不到。它只会记录有人添加的内容,看起来它会立即返回true。问题是,为什么它不做代码的其他部分 public override bool OnFriendAdd() { Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) +

我对SteamBot代码有问题。所以,如果有人添加“机器人”作为好友,那么它将在日志中显示添加者。它做到了这一点,在那之后我添加了更多的代码,但那部分它就是做不到。它只会记录有人添加的内容,看起来它会立即返回true。问题是,为什么它不做代码的其他部分

public override bool OnFriendAdd()
        {
            Bot.Log.Success(Bot.SteamFriends.GetFriendPersonaName(OtherSID) + " (" + OtherSID.ToString() + ") added me!");
            HttpWebRequest check =
            check.Method = "GET";
            HttpWebResponse checkResp;
            try
            {
                checkResp = check.GetResponse() as HttpWebResponse;
                StreamReader createRead = new StreamReader(checkResp.GetResponseStream());
                string resultCA = createRead.ReadToEnd();
                if (resultCA.Contains("success"))
                {
                    Regex SomeNameHere3 = new Regex("\"\" : \"([A-Za-z0-9]+)");
                    string swag3 = SomeNameHere3.Match(resultCA).Value;
                    swag3 = swag3.Replace("\"\" : \"", "");
                    currentSID = currentSID.ConvertToUInt64();
                    Log.Success("User: " + Bot.SteamFriends.GetFriendPersonaName(currentSID) + "  : " + swag3);
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                    Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                }
            }
            catch (WebException ex)
            {
                checkResp = ex.Response as HttpWebResponse;
                StreamReader createRead = new StreamReader(checkResp.GetResponseStream());
                string resultCA = createRead.ReadToEnd();
                if (resultCA.Contains("fail") && resultCA.Contains("Label does not exist"))
                {
                    HttpWebRequest create =
                    create.Method = "GET";
                    HttpWebResponse createResp = (HttpWebResponse)create.GetResponse();
                    StreamReader createSR = new StreamReader(createResp.GetResponseStream());
                    string createSRRTE = createSR.ReadToEnd();
                    if (createSRRTE.Contains("success"))
                    {
                        Regex SomeNameHere3 = new Regex("\"\" : \"([A-Za-z0-9]+)");
                        string get = SomeNameHere3.Match(createSRRTE).Value;
                        get = get.Replace("\"\" : \"", "");
                        Log.Success("User: " + Bot.SteamFriends.GetFriendPersonaName(currentSID) + "  : " + get);
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                        Bot.SteamFriends.SendChatMessage(currentSID, type, ");
                       Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                       Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                       Bot.SteamFriends.SendChatMessage(currentSID, type, "");
                   }
               }
           }
           return true;
       }

最好使用using语句,它将为您处理处置

using (StreamReader createRead = new StreamReader(createResp.GetResponseStream()))
{
   ///
}

要使
读取或写入,必须告知它正在读取或写入的非托管数据已完成,或者它不必继续读取或写入

除了关闭流之外,还需要处理它。处置流将自动关闭它,因此仅处置也足够:

var stream = new FileStream();
// stuff;
stream.Dispose();
正如另一个答案所建议的,您还可以选择在
IDisposable
对象(例如
)上使用
using(){}
块。这将确保在
使用
块结束时,正在“使用”的对象将被处置:

using(var stream = new FileStream()){
    //stuff;
}

您的streamwriter等待它关闭。您需要显式地编写:createSR.Close();createSR.dispose();谢谢,它为我解决了这个问题。在那里写解决方案不是这个论坛的工作方式。你们应该让@Glubus写一个答案,然后选择正确的答案。@Glubus你们能写和答案一样的东西吗?所以我可以接受,我已经创建了一个答案。为流显式调用
Close
Dispose
是一样的。“正确”(异常安全)的做法是使用
using
块(或try finally),因此在该MSDN页面上会显示advice。此方法调用Dispose,指定true以释放所有资源。您不必专门调用Close方法。相反,请确保每个流对象都已正确处理。你说得对。