FormatException
string dir = @"E:\";
string monitorID = DropDownListMonitor.SelectedValue; //monitorID = "001";
string pathMonitor = dir + monitorID; //此目录下有很多命名类似“201001271110.xls”的文件
string[] fileNames = Directory.GetFiles(pathMonitor);
string filePath = getFilePath(); // filePath = "E:\001\201004271110.xls"
for (int i = 0; i < fileNames.Length; i++)
{
int filePathNum = Convert.ToInt32(filePath.Substring(17, 2));
int fileNamesNum = Convert.ToInt32(fileNames[i].Substring(17,2));
int fileNamesNum1 = Convert.ToInt32(fileNames[i+1].Substring(17,2));
}
运行的时候,报一个System.FormatException错误
输入字符串的格式不正确。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.FormatException: 输入字符串的格式不正确。
源错误:
行 73: int filePathNum = Convert.ToInt32(filePath.Substring(17, 2));
行 74: int fileNamesNum = Convert.ToInt32(fileNames[i].Substring(17,2));
行 75: int fileNamesNum1 = Convert.ToInt32(fileNames[i+1].Substring(17,2));行 76:
行 77: if (filePath == fileNames[i])
求解.
--------------------编程问答-------------------- 有没有人帮我看看的,我还没搞明白! --------------------编程问答-------------------- 别沉啊 --------------------编程问答-------------------- --------------------编程问答-------------------- .....
你看下 fileNames[i+1].Substring(17,2)) 结果是什么先 --------------------编程问答-------------------- 行 75: int fileNamesNum1 = Convert.ToInt32(fileNames[i+1].Substring(17,2));行 76:
把断点设在这里,看看什么错误! --------------------编程问答--------------------
不怎么会设断点查看。。。,我把那行设为断点了,怎么查看fileNames[i+1].Substring(17,2))的结果啊? --------------------编程问答-------------------- 设置断点,然后F5运行程序,F11单步执行程序,运行到那行鼠标放到那行就可以看运行结果 --------------------编程问答--------------------
你确定 fileNames 的长度超过了19么?如果fileNames长度少于19的话,这里就肯定会出现异常。 --------------------编程问答--------------------
明白了,谢谢! --------------------编程问答--------------------
鼠标将焦点设在那一行:76行,然后按F9,F5进行调试,到了那一步的时候查看fileNames[i+1].Substring(17,2)的值 --------------------编程问答-------------------- 在Substring前一定要判断一下长度
1、看下str.Substring()越界
2、看一下你的取值是否能转化成Int 应该是不行,FormatException转化异常时触发 --------------------编程问答--------------------
fileNames[i]都是这样的文件:"E:\001\201004271110.xls",文件名不同而已;
for (int i = 0; i < fileNames.Length - 1; i++)
{
int filePathNum = Convert.ToInt32(filePath.Substring(17, 2));
int fileNamesNum = Convert.ToInt32(fileNames[i].Substring(17,2));
int fileNamesNum1 = Convert.ToInt32(fileNames[i+1].Substring(17,2));
}
str.Substring()不会越界吧,而且肯定是能转换为Int的,具体的代码是这样的:
protected void btnOK_Click(object sender, EventArgs e)
{
string dir = @"E:\";
string monitorID = DropDownListMonitor.SelectedValue;
string pathMonitor = dir + monitorID;
string[] fileNames = Directory.GetFiles(pathMonitor);
string filePath = getFilePath();
for (int i = 0; i < fileNames.Length - 1; i++)
{
if (filePath == fileNames[i])
{
lblFilePath.Text = "您查找的文件为:" + filePath.Substring(7, 16);
break;
}
else if ((filePath != fileNames[i]) && (filePath.Substring(0, 17) == fileNames[i].Substring(0, 17)))
{
int filePathNum = Convert.ToInt32(filePath.Substring(17, 2));
int fileNamesNum = Convert.ToInt32(fileNames[i].Substring(17, 2));
int fileNamesNum1 = Convert.ToInt32(fileNames[i + 1].Substring(17, 2));
if (Math.Abs(filePathNum - fileNamesNum) < Math.Abs(filePathNum - fileNamesNum1))
{
lblFilePath.Text = "您查找的文件不存在,这个是最接近的文件:" + fileNames[i].Substring(7, 16);
break;
}
}
else
{
lblBack.Text = "没有符合条件的文件,请继续查找。";
}
}
}
我把它放在下面,就不会报错。
补充:.NET技术 , C#