C# windows 8 RT应用程序中未访问文本文件
我试图在我的应用程序中访问一个文件,我已经创建了它,但它不在我的解决方案资源管理器的文件夹中,但我试图通过从文本文件中提取一些单词来访问它,然后在按下按钮时将其放在列表框中,但什么都没有发生。有人能帮我解决可能出现的问题吗?我不知道我是否有问题没有以正确的方式访问文件,但这是我的代码C# windows 8 RT应用程序中未访问文本文件,c#,visual-studio-2012,windows-8,windows-runtime,C#,Visual Studio 2012,Windows 8,Windows Runtime,我试图在我的应用程序中访问一个文件,我已经创建了它,但它不在我的解决方案资源管理器的文件夹中,但我试图通过从文本文件中提取一些单词来访问它,然后在按下按钮时将其放在列表框中,但什么都没有发生。有人能帮我解决可能出现的问题吗?我不知道我是否有问题没有以正确的方式访问文件,但这是我的代码 async private void txtFileBtn_Click(object sender, RoutedEventArgs e) { try
async private void txtFileBtn_Click(object sender, RoutedEventArgs e)
{
try
{
// Get folder
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
// Get the file path from hardcoded string
StorageFile sampleFile = await localFolder.GetFileAsync("ks1words.txt");
// Read the lines
var words = await FileIO.ReadLinesAsync(sampleFile);
// Create a list
List<string> wordList = new List<string>();
// Loops and add word to list
foreach (var word in words) { wordList.Add(word); } // fill a listbox with the words
// Set list as itemsource
lstBox.ItemsSource = wordList;
}
catch {
// Do nothing
}
}
async private void txtFileBtn\u单击(对象发送方,RoutedEventArgs e)
{
尝试
{
//获取文件夹
Windows.Storage.StorageFolder localFolder=Windows.Storage.ApplicationData.Current.localFolder;
//从硬编码字符串获取文件路径
StorageFile sampleFile=await localFolder.GetFileAsync(“ks1words.txt”);
//读台词
var words=await FileIO.ReadLinesAsync(sampleFile);
//创建一个列表
List wordList=新列表();
//循环并将单词添加到列表中
foreach(var word in words){wordList.Add(word);}//用单词填充列表框
//将列表设置为itemsource
lstBox.ItemsSource=单词列表;
}
捕获{
//无所事事
}
}
你说“什么都没发生”是什么意思?你有没有试过调试代码,并逐步检查它,看看是否一切都得到了执行?这个方法执行了吗?这些词被加载了吗?你有例外吗?您可以在Alt+Ctrl+E窗口中启用在第一次机会异常时中断,以查看是否抛出了任何异常,即使您将“捕获所有”块设置为“吃掉所有异常”。它在尝试获取文件后跳过“尝试捕获”尝试在下一行放置断点(F9),而不是使用F10逐行执行。这可能不适合异步调用。是“ks1words.txt”应用程序的本地文件夹中的文件?是,请将其添加为新项目,并显示在解决方案资源管理器中,我一直在调试它,收到的异常为:此处是我收到的异常系统。NullReferenceException未由用户代码处理HResult=-2147467261 Message=对象引用未设置为对象的实例。Source=workingwtxtfiles StackTrace:c:\Users\Tunde\Documents\visualstudio 2012\Projects\workingwtxtfiles\workingwtxtfiles\MainPage.xaml.cs中的workingwtxtfiles.MainPage.d_u0.MoveNext()处:第48行内部异常: