c# - 需要使用一个文件来填充一个数组,然后是一个列表框。我设法让它充满了一些东西,但我无法让它正确显示。建议?
问题描述
基本上,我的任务是使用一个程序,我需要将一些信息写入文件,然后在另一个程序中使用该文件来填充列表框,其中包含名称和查找功能。我目前的问题是我在正确读取文件时遇到了很多麻烦。相关信息:其他文件写入的信息包括姓名、电话号码、手机号码、寻呼机号码、语音邮件和电子邮件地址。所有内容都通过文本框输入写入文件。
我尝试的主要方法是简单地将 .txt 文件作为资产添加到项目中以使访问更容易,但它根本没有这样做。新方法是使用选择器来选择文件。
public sealed partial class MainPage : Page
{
internal struct Information
{
internal string Name;
internal string Phone;
internal string Pager;
internal string Cell;
internal string Voicemail;
internal string Email;
}
private Information[] Info = new Information[4];
public MainPage()
{
this.InitializeComponent();
Prepare_Array();
}
private async void Prepare_Array()
{
try
{
FileOpenPicker openPicker = new FileOpenPicker();
openPicker.ViewMode = PickerViewMode.List;
openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;
openPicker.FileTypeFilter.Add(".txt");
StorageFile file = await openPicker.PickSingleFileAsync();
if (file != null)
{
Info_Listbox.Items.Add(Info[1].Name);
}
}
catch { }
}
我正在尝试使用的文本文件。
Name: Joker Phone Number: 888-888-8888 Pager Number: 90009 Cellphone Number: 555-555-5555 Voicemail Number: 333-333-3333 Email Address: Joke@Joker.Net
Name: Johnny Phone Number 123-456-7890 Pager Number: 19991 Cellphone Number: 098-765-4321 Voicemail Number: 567-843-1209 Email Address: Johnny@Joker.Net
我希望列表框有一些东西(甚至整个条目都是可以接受的,我可以从那里开始工作)。实际结果是 Windows.Foundation.Collections.IVector'1
解决方案
尝试这个
一些声明(用于上下文):
struct Information
{
string Name;
string Phone;
string Pager;
string Cell;
string Voicemail;
string Email;
}
public Information[] info = new Information[4];
其他一些声明(在该结构加载方法中):
OpenFileDialog openDialog = new OpenFileDialog();
string path;
string line;
string[] attributes = new string[6];
现在是代码
openDialog.Title = "Select A File";
openDialog.Filter = "Text Files (*.txt)|*.txt" + "|" +
"All Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
for(int i = 0; i < info.Length; i++)
{
path = openDialog.FileName;
line = File.ReadLine(path);
attributes = line.Remove("Name: ").Remove("Phone Number ").Remove("Pager Number: ")
.Remove("Cellphone Number: ").Remove("Voicemail Number: ").Remove("Email Address: ").Split(' ');
info[i].Name = attributes[0];
info[i].Phone = attributes[1];
info[i].Pager = attributes[2];
info[i].Cell = attributes[3];
info[i].Voicemail = attributes[4];
info[i].Email = attributes[5];
}
foreach(Information inf in info)
{
Info_Listbox.Items.Add(inf.Name);
}
}
推荐阅读
- python - 从 csv 文件导入数据 - 为什么他们打印不同的东西?
- c++ - 视觉工作室阿拉伯语文本:常量中的换行符
- python - Django - 使用通用视图找不到 URL
- python - Python:如何用 ast 重载运算符
- html - Node.js + Cheerio Scraping:如何在联系表中发帖?
- css - 忽略所有父 CSS 的 Html 元素
- android - CollapsingToolbarLayout 将工具栏文本推到屏幕外。
- python - visual studio code - 用 python 编写的语言服务器扩展
- angular - @handsontable/angular:如何使用热列创建自动完成或可动手操作的类型
- mongodb - UpdateMany 方法总是返回零更新字段(Mongo C# 驱动程序)