作者:dasda | 来源:互联网 | 2023-09-15 20:00
我是想做一个日记应用可是调用SubString(0,7)时却出现这样的问题:“指定的参数已超出有效值的范围”当调用SubString(0,6)或者SubString(0,5)或更小时就不会出现这
我是想做一个日记应用可是调用SubString(0,7)时却出现这样的问题:“指定的参数已超出有效值的范围”...当调用SubString(0,6)或者SubString(0,5)或更小时就不会出现这样的问题.....如何解决啊???!!
(出问题在红色标记的地方.....其实 day hour minute ....都存在问题的....唉....求高手解答..!!)
源码如下:
这是一个按钮的点击事件:
private void AppBar_add(object sender, EventArgs e)
{
string fileName = "2012_06_12_16_14_02_iudgui_hdgauy-China.txt";
string fileContent = "fcdfcwvfaWVF";
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
if(!appStorage.FileExists(fileName))
{
using (var file=appStorage.CreateFile(fileName))
{
using (var writer = new StreamWriter(file))
{
writer.WriteLine(fileContent);
}
}
}
bindList();
}
然后:
public void bindList()
{
var appStorage = IsolatedStorageFile.GetUserStoreForApplication();
string[] fileList = appStorage.GetDirectoryNames();
foreach (string file in fileList)
{
string fileName = file;
string year = file.Substring(0, 4);
string month = file.Substring(0,7);
string day = file.Substring(8, 2);
string hour = file.Substring(11, 2);
string minute = file.Substring(14, 2);
string second = file.Substring(17,2);
DateTime dateCreated = new DateTime(2012, int.Parse(month), int.Parse(day), int.Parse(hour), int.Parse(minute), int.Parse(second));
string location = file.Substring(20);
location = location.Replace("_", ",");
location = location.Replace("-", " ");
location = location.Substring(0, location.Length - 4);
}
}
}
出问题在红色标记的地方.....其实 day hour minute ....都存在问题的....唉....求高手解答..!!
4 个解决方案
其实我是看这里的教程:
http://msdn.microsoft.com/zh-cn/windowsphone/hh418016边看边做的....
我发现问题了...不应该是:string[] fileList = appStorage.GetDirectoryNames();
应该是:string[] fileList = appStorage.GetFileNames();
我靠......
这是代码的容错的考虑问题,
你写代码的时候,你得预想到可能出现的异常情况,并为之写一些catch的代码。