错误消息C#:格式异常 [英] Error messages C#: format exception

查看:111
本文介绍了错误消息C#:格式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,

抱歉发布错误信息,但我真的遇到了麻烦。我有一个ac#winforms项目收到此错误消息:



Hey guys,
sorry to post an error message, but I am really having trouble here. I have a c# winforms project that gets this error message:

************** Exception Text **************
    System.FormatException: Input string was not in a correct format.
       at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
       at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)
       at SlashA006.Form1.IdCheck() in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA006 f\SlashA006\SlashA006\Form1.cs:line 180
       at SlashA006.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\John\Desktop\John\SCAR\OperationMonkeyRaider-v2\visual studio\real\SlashA006 f\SlashA006\SlashA006\Form1.cs:line 64
       at System.Windows.Forms.Form.OnLoad(EventArgs e)
       at System.Windows.Forms.Form.OnCreateControl()
       at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
       at System.Windows.Forms.Control.CreateControl()
       at System.Windows.Forms.Control.WmShowWindow(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Form.WmShowWindow(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)





所以我做了一些研究,我想这个错误意味着一些变量类型可以混合在一起,但我不知道该做什么我在网上找不到任何相关的东西。我的第64-180行的代码是:



第64行:



So I did some research, I guess this error means some variable types could be mixed around, but I have no idea what to do and I can't find anything relavent online. My code for lines 64-180 is:

line 64:

IdCheck();



第180行:


line 180:

public void IdCheck()
       {
           string currentDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)+ @"\SlashA006";
           string path = currentDirectory + @"\saveData.txt";
           if (!File.Exists(path))
           {
              var saveDataFile = File.Create(path);
               saveDataFile.Close();
               int id = IDsetup();
               TextWriter tw = new StreamWriter(path);
               tw.WriteLine(id.ToString());
               tw.Close();
               computerID = id;
               this.Visible = true;
               System.Windows.Forms.MessageBox.Show("ID is: " + id);
               func_wait_3("500");
               this.Visible = false;
           }
           if (File.Exists(path))
           {
               bool success = int.TryParse(File.ReadAllText(path), out int idRead);
               //  printLine(idRead.ToString()); //LINE 180 ABOVE THIS ONE
               computerID = idRead;
           }
       }



再一次,抱歉有一个新问题,但我真的很感激任何帮助。谢谢!



我尝试了什么:



如上所述,我试着在网上寻找这个问题的例子,但无济于事。我将第180行从解析更改为tryparse,但这也不起作用。


Once again, sorry for a newb question, but I'd really appreciate any help. Thanks!

What I have tried:

As I said above, I tried looking online for examples of this problem but to no avail. I changed line 180 from parse to tryparse, but that didn't work either.

推荐答案

基本上,这意味着您尝试将任何文本转换为数字类型或日期类型,不是可以转换为该类型的字符串。



如果输入AXG012然后键入以将其转换为整数,你会得到你正在谈论的错误。



在你的例子中,看起来你读取整个文件并尝试将其转换为整数。回车符和换行符不会转换为整数,所以我不会对这个错误感到惊讶。



你做的很奇怪。您通常不会在TryParse的参数中执行任何类型的文件操作。
Basically, it means that whatever text you're trying to convert to a number type, or date type, is not a string of characters that can be converted to that type.

If you enter "AXG012" and then type to convert that to an integer, you'll get the error you're talking about.

In your example, it would appear that your reading an ENTIRE FILE and trying to convert that to an integer. Carriage Return and Line Feed characters don't convert to integers, so I'm not surprised you're getting this error.

What you're doing is very odd. You normally don't do any kind of file operation in the parameters to TryParse.


这篇关于错误消息C#:格式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆