[英]C# Write from text file to listView
I created windows application in C# that writes from text file to listView. 我用C#创建了Windows应用程序,该应用程序从文本文件写入listView。 I first executed commands in cmd to list system and user apps from the phone and I put cmd output to text file.
我首先在cmd中执行命令以列出手机中的系统和用户应用程序,然后将cmd输出放入文本文件。 Now I want to add content of text file to listView: in first column apps from /system/app should be listed, in second apps from /system/priv-app and in third apps from /data/app!
现在,我想将文本文件的内容添加到listView中:应该列出第一列中的/ system / app中的应用程序,第二列中的/ system / priv-app中的应用程序以及第三遍/ data / app的应用程序! When I press the button to execute function I get error:System.ArgumentException: Cannot add or insert the item '' in more than one place.
当我按下按钮执行功能时,出现错误:System.ArgumentException:无法在多个地方添加或插入项目“。 You must first remove it from its current location or clone it.
您必须首先将其从其当前位置删除或克隆它。
Process process;
ProcessStartInfo startInfo;
private void Form1_Load(object sender, EventArgs e)
{
process = new Process();
startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd.exe";
startInfo.ErrorDialog = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "/C adb devices&adb shell pm list packages -f";
}
private void Redirect_ADB_Output()
{
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
File.WriteAllText("ADB.txt", result);
}
}
}
private void WriteToListView()
{
ListViewItem lvItem = new ListViewItem();
var lines = File.ReadLines("ADB.txt");
foreach (var line in lines)
{
ListViewItem lvItem = new ListViewItem();
if (line != string.Empty)
{
var newLine = line.Remove(0, 8);
if (line.Contains("package:/system/app"))
{
ApkList.Items.Add(newLine);
}
if (line.Contains("package:/system/priv-app"))
{
lvItem.SubItems.Add(newLine);
}
if (line.Contains("package:/data/app"))
{
lvItem.SubItems.Add(newLine);
}
}
ApkList.Items.Add(lvItem);
}
File.WriteAllText("ADB.txt", string.Empty);
}
private void btn_PullApkFromPhone_Click(object sender, EventArgs e)
{
startInfo.Arguments = "/C adb devices&adb shell pm list packages -f";
Redirect_ADB_Output();
WriteToListView();
}
I think it may be because you are modifying an element while looping over it. 我认为这可能是因为您在循环访问元素时正在对其进行修改。
Try creating a new object and adding that to the collections (also, as another user pointed out, try moving the ListView into the foreach loop): 尝试创建一个新对象并将其添加到集合中(也正如另一个用户指出的那样,尝试将ListView移到foreach循环中):
foreach (var line in lines)
{
ListViewItem lvItem = new ListViewItem();
var newLine = line.Remove(0, 8);
if (line.StartsWith("package:/system/app"))
{
ApkList.Items.Add(newLine);
}
if (line.StartsWith("package:/system/priv-app"))
{
lvItem.SubItems.Add(newLine);
}
if (line.StartsWith("package:/data/app"))
{
lvItem.SubItems.Add(newLine);
}
ApkList.Items.Add(lvItem);
}
I would suggest first adding "columns", this helps. 我建议先添加“列”,这会有所帮助。
ListViewItem lvItem = new ListViewItem();
ApkList.View = View.Details;
ApkList.Columns.Add("Col1");
ApkList.Columns.Add("Col2");
ApkList.Columns.Add("Col3");
THEN You need to do this for the foreach. 然后,您需要在foreach上执行此操作。
foreach (var line in lines)
{
if (line != string.Empty)
{
var newLine = line.Remove(0, 8);
if (line.Contains("package:/system/app"))
{
lvItem.Text = newLine;
}
if (line.Contains("package:/system/priv-app"))
{
lvItem.SubItems.Add(newLine);
}
if (line.Contains("package:/data/app"))
{
lvItem.SubItems.Add(newLine);
}
}
}
ApkList.Items.Add(lvItem);
You'll then need a final loop around the above, for, foreach, while to populate your entire list. 然后,您需要围绕上面的最后一个循环,foreach,同时填充整个列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.