ListView adding items with subitems - c#

I'm trying to read a file line by line, which works perfectly but I want to seperate the results I get into subitems in the listview.
I am also searching for all .jar files in the folder so I can use those as the name (first column). The second column needs to have the "version", the third column the "author" and the fourth column the "description".
Here's one of the text files I receive from within the jar files:
name: AFK
main: com.github.alesvojta.AFK.AFK
version: 2.0.5
author: Ales Vojta / schneckk
description: Provides AFK messages
website: http://dev.bukkit.org/server-mods/afk/
commands:
afk:
description: Provides AFK message when player types /afk.
usage: /<command>
this is the code I have right now:
private List<string> GetInstalledPlugins()
{
List<string> list = new List<string>();
lvInstalledPlugins.Items.Clear();
if (!Directory.Exists(Environment.CurrentDirectory + "\\plugins"))
{
Directory.CreateDirectory(Environment.CurrentDirectory + "\\plugins");
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory + "\\plugins");
FileInfo[] fileInfo = di.GetFiles("*.jar");
foreach (var info in fileInfo)
{
//lvInstalledPlugins.Items.Add(info.Name);
list.Add(info.Name);
}
}
else
{
DirectoryInfo di = new DirectoryInfo(Environment.CurrentDirectory + "\\plugins");
FileInfo[] fileInfo = di.GetFiles("*.jar");
foreach (var info in fileInfo)
{
//lvInstalledPlugins.Items.Add(info.Name);
list.Add(info.Name);
}
}
return list;
}
private void test(IEnumerable<string> list)
{
List<ListViewItem> PluginList = new List<ListViewItem>();
var items = new string[4];
try
{
foreach (var ListItem in list)
{
Console.WriteLine(ListItem);
var name = Environment.CurrentDirectory + "\\plugins\\" + ListItem;
var zip = new ZipInputStream(File.OpenRead(name));
var filestream = new FileStream(name, FileMode.Open, FileAccess.Read);
var zipfile = new ZipFile(filestream);
ZipEntry item;
while ((item = zip.GetNextEntry()) != null)
{
if (item.Name == "plugin.yml")
{
using (var s = new StreamReader(zipfile.GetInputStream(item)))
{
string line;
while ((line = s.ReadLine()) != null)
{
if (line.Contains("name"))
{
items[0] = line;
}
if (line.Contains("version"))
{
items[1] = line;
}
if (line.Contains("author"))
{
items[2] = line;
}
if (line.Contains("description"))
{
items[3] = line;
}
try
{
var lvitem = new ListViewItem(items);
lvitem.Name = items[0];
lvitem.Text = items[0];
lvitem.SubItems.Add(items[1]);
lvitem.SubItems.Add(items[2]);
lvitem.SubItems.Add(items[3]);
PluginList.Add(lvitem);
}
catch (Exception)
{
}
}
lvInstalledPlugins.Items.AddRange(PluginList.ToArray());
}
}
}
}
This doesn't seem to work :/, any ideas? I've been working on this for the whole day and can't seem to get it to work :(.

Not exactly sure of your question, but going by the title, the answer to the question below may provide some assistance.
C# listView, how do I add items to columns 2, 3 and 4 etc?

Related

Lucene 4.8 facets usage

I have difficulties understanding this example on how to use facets :
https://lucenenet.apache.org/docs/4.8.0-beta00008/api/Lucene.Net.Demo/Lucene.Net.Demo.Facet.SimpleFacetsExample.html
My goal is to create an index in which each document field have a facet, so that at search time i can choose which facets use to navigate data.
What i am confused about is setup of facets in index creation, to
summarize my question : is index with facets compatibile with
ReferenceManager?
Need DirectoryTaxonomyWriter to be actually written and persisted
on disk or it will embedded into the index itself and is just
temporary? I mean given the code
indexWriter.AddDocument(config.Build(taxoWriter, doc)); of the
example i expect it's temporary and will be embedded into the index (but then the example also show you need the Taxonomy to drill down facet). So can the Taxonomy be tangled in some way with the index so that the are handled althogeter with ReferenceManager?
If is not may i just use the same folder i use for storing index?
Here is a more detailed list of point that confuse me :
In my scenario i am indexing the document asyncrhonously (background process) and then fetching the indext ASAP throught ReferenceManager in ASP.NET application. I hope this way to fetch the index is compatibile with DirectoryTaxonomyWriter needed by facets.
Then i modified the code i write introducing the taxonomy writer as indicated in the example, but i am a bit confused, seems like i can't store DirectoryTaxonomyWriter into the same folder of index because the folder is locked, need i to persist it or it will be embedded into the index (so a RAMDirectory is enougth)? if i need to persist it in a different direcotry, can i safely persist it into subdirectory?
Here the code i am actually using :
private static void BuildIndex (IndexEntry entry)
{
string targetFolder = ConfigurationManager.AppSettings["IndexFolder"] ?? string.Empty;
//** LOG
if (System.IO.Directory.Exists(targetFolder) == false)
{
string message = #"Index folder not found";
_fileLogger.Error(message);
_consoleLogger.Error(message);
return;
}
var metadata = JsonConvert.DeserializeObject<IndexMetadata>(File.ReadAllText(entry.MetdataPath) ?? "{}");
string[] header = new string[0];
List<dynamic> csvRecords = new List<dynamic>();
using (var reader = new StreamReader(entry.DataPath))
{
CsvConfiguration csvConfiguration = new CsvConfiguration(CultureInfo.InvariantCulture);
csvConfiguration.AllowComments = false;
csvConfiguration.CountBytes = false;
csvConfiguration.Delimiter = ",";
csvConfiguration.DetectColumnCountChanges = false;
csvConfiguration.Encoding = Encoding.UTF8;
csvConfiguration.HasHeaderRecord = true;
csvConfiguration.IgnoreBlankLines = true;
csvConfiguration.HeaderValidated = null;
csvConfiguration.MissingFieldFound = null;
csvConfiguration.TrimOptions = CsvHelper.Configuration.TrimOptions.None;
csvConfiguration.BadDataFound = null;
using (var csvReader = new CsvReader(reader, csvConfiguration))
{
csvReader.Read();
csvReader.ReadHeader();
csvReader.Read();
header = csvReader.HeaderRecord;
csvRecords = csvReader.GetRecords<dynamic>().ToList();
}
}
string targetDirectory = Path.Combine(targetFolder, "Index__" + metadata.Boundle + "__" + DateTime.Now.ToString("yyyyMMdd_HHmmss") + "__" + Path.GetRandomFileName().Substring(0, 6));
System.IO.Directory.CreateDirectory(targetDirectory);
//** LOG
{
string message = #"..creating index : {0}";
_fileLogger.Information(message, targetDirectory);
_consoleLogger.Information(message, targetDirectory);
}
using (var dir = FSDirectory.Open(targetDirectory))
{
using (DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(dir))
{
Analyzer analyzer = metadata.GetAnalyzer();
var indexConfig = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer);
using (IndexWriter writer = new IndexWriter(dir, indexConfig))
{
long entryNumber = csvRecords.Count();
long index = 0;
long lastPercentage = 0;
foreach (dynamic csvEntry in csvRecords)
{
Document doc = new Document();
IDictionary<string, object> dynamicCsvEntry = (IDictionary<string, object>)csvEntry;
var indexedMetadataFiled = metadata.IdexedFields;
foreach (string headField in header)
{
if (indexedMetadataFiled.ContainsKey(headField) == false || (indexedMetadataFiled[headField].NeedToBeIndexed == false && indexedMetadataFiled[headField].NeedToBeStored == false))
continue;
var field = new Field(headField,
((string)dynamicCsvEntry[headField] ?? string.Empty).ToLower(),
indexedMetadataFiled[headField].NeedToBeStored ? Field.Store.YES : Field.Store.NO,
indexedMetadataFiled[headField].NeedToBeIndexed ? Field.Index.ANALYZED : Field.Index.NO
);
doc.Add(field);
var facetField = new FacetField(headField, (string)dynamicCsvEntry[headField]);
doc.Add(facetField);
}
long percentage = (long)(((decimal)index / (decimal)entryNumber) * 100m);
if (percentage > lastPercentage && percentage % 10 == 0)
{
_consoleLogger.Information($"..indexing {percentage}%..");
lastPercentage = percentage;
}
writer.AddDocument(doc);
index++;
}
writer.Commit();
}
}
}
//** LOG
{
string message = #"Index Created : {0}";
_fileLogger.Information(message, targetDirectory);
_consoleLogger.Information(message, targetDirectory);
}
}

How to read and separate segments of a txt file?

I have a txt file, that has headers and then 3 columns of values (i.e)
Description=null
area = 100
1,2,3
1,2,4
2,1,5 ...
... 1,2,1//(these are the values that I need in one list)
Then another segment
Description=null
area = 10
1,2,3
1,2,4
2,1,5 ...
... 1,2,1//(these are the values that I need in one list).
In fact I just need one list per "Table" of values, the values always are in 3 columns but, there are n segments, any idea?
Thanks!
List<double> VMM40xyz = new List<double>();
foreach (var item in VMM40blocklines)
{
if (item.Contains(','))
{
VMM40xyz.AddRange(item.Split(',').Select(double.Parse).ToList());
}
}
I tried this, but it just work with the values in just one big list.
It looks like you want your data to end up in a format like this:
public class SetOfData //Feel free to name these parts better.
{
public string Description = "";
public string Area = "";
public List<double> Data = new List<double>();
}
...stored somewhere in...
List<SetOfData> finalData = new List<SetOfData>();
So, here's how I'd read that in:
public static List<SetOfData> ReadCustomFile(string Filename)
{
if (!File.Exists(Filename))
{
throw new FileNotFoundException($"{Filename} does not exist.");
}
List<SetOfData> returnData = new List<SetOfData>();
SetOfData currentDataSet = null;
using (FileStream fs = new FileStream(Filename, FileMode.Open))
{
using (StreamReader reader = new StreamReader(fs))
{
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
//This will start a new object on every 'Description' line.
if (line.Contains("Description="))
{
//Save off the old data set if there is one.
if (currentDataSet != null)
returnData.Add(currentDataSet);
currentDataSet = new SetOfData();
//Now, to make sure there is something after "Description=" and to set the Description if there is.
//Your example data used "null" here, which this will take literally to be a string containing the letters "null". You can check the contents of parts[1] inside the if block to change this.
string[] parts = line.Split('=');
if (parts.Length > 1)
currentDataSet.Description = parts[1].Trim();
}
else if (line.Contains("area = "))
{
//Just in case your file didn't start with a "Description" line for some reason.
if (currentDataSet == null)
currentDataSet = new SetOfData();
//And then we do some string splitting like we did for Description.
string[] parts = line.Split('=');
if (parts.Length > 1)
currentDataSet.Area = parts[1].Trim();
}
else
{
//Just in case your file didn't start with a "Description" line for some reason.
if (currentDataSet == null)
currentDataSet = new SetOfData();
string[] parts = line.Split(',');
foreach (string part in parts)
{
if (double.TryParse(part, out double number))
{
currentDataSet.Data.Add(number);
}
}
}
}
//Make sure to add the last set.
returnData.Add(currentDataSet);
}
}
return returnData;
}

Efficiently comparing two lists

so, i have this block of code on my app that scans a directory for files, makes a list with them and compares that list with a list of files from a database (if the path to that directory exists on the db) and adds the difference between them to one of two other lists.Here it is :
if (id > 0)
{
var dbDrawingList = mdl_drawing.GetDrawingsByBaseId(id);
var counter = 0;
if (dbDrawingList.Count() < serverDrawingList.Count())
{
counter = serverDrawingList.Count();
}
else
{
counter = dbDrawingList.Count();
}
for (int i = 0; i <= counter; i++)
{
if (i < serverDrawingList.Count())
{
if (dbDrawingList.Select(f => f.partNumber).Contains(serverDrawingList[i].partNumber) == false)
{
onServerAndNotDb.Add(serverDrawingList[i]);
}
}
if (i < dbDrawingList.Count())
{
if (serverDrawingList.Select(f => f.partNumber).Contains(dbDrawingList[i].partNumber) == false)
{
onDbAndNotServer.Add(dbDrawingList[i]);
}
}
}
serverDrawingList = null;
dbDrawingList = null;
}
Does anyone have a better way of doing this?(there can be more than one file with the same name, so the Except method doesn't work)
I have one method that does exactly that, this is the way I implemented it and it's working so far:
private void deleteRegistersFromFilesThatWasRemoved(string path)
{
// add local files to list
List<string> allFiles = new List<string>();
string[] dirs = Directory.GetDirectories(path, "*",
SearchOption.TopDirectoryOnly);
foreach (var dir in dirs)
{
string[] files = Directory.GetFiles(dir, "*", SearchOption.TopDirectoryOnly);
foreach (var file in files)
{
if(file != path)
{
allFiles.Add(file);
}
}
}
// list for file records in the database
List<string> record = new List<string>();
string queryfiles = //your query
SqlCommand cmd = new SqlCommand(queryfiles, connection);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataReader read = cmd.ExecuteReader();
while (read.Read())
{
// I have path and file name separated that's why here's a string sum
string r = read[2].ToString() + read[1].ToString();
record.Add(r);
}
cmd.Connection.Close();
for (int i = 0; i < record.Count; i++)
{
if (!allFiles.Contains(record[i]))
{
// do something if the record on the database is not in the local
//files list
}
}
}
}

Check if item is found in text file

How can I check if a text file contains an item from a listbox. To stop saving duplicates. I'm not sure what I'd add to this. This is called on a button click event. For example, if a duplicate was found, I could show MessageBox.Show ("duplicate error");
using (StreamWriter writer = new StreamWriter("test.txt", true))
{
foreach (object item in listBox2.Items)
{
writer.WriteLine(item.ToString());
}
}
Before writing to "test.txt", enumerate its contents:
var fileLines = File.ReadAllLines("test.txt");
List<string> fileItems = new List<string>(fileLines);
Then before you write each item, check to see if the list contains it:
using (StreamWriter writer = new StreamWriter("test.txt", true))
{
foreach (object item in listBox2.Items)
{
if (fileItems.Contains(item))
// Do something, break, etc.
else
writer.WriteLine(item.ToString());
}
}
Edit:
Per suggestions, you can use a HashSet instead of a List for performance, as it can only contain unique values.
Another improvement may be to check if any duplicates exist before writing anything to the file. I've done that in the example below in a LINQ statement:
var fileLines = File.ReadAllLines("test.txt");
HashSet<string> fileItems = new HashSet<string>(fileLines);
using (StreamWriter writer = new StreamWriter("test.txt", true))
{
bool duplicateFound = fileItems.Any(fi => listBox1.Items.Cast<string>().Any(i => i == fi));
if (duplicateFound)
MessageBox.Show("Duplicate items found.");
else
foreach (object item in listBox1.Items)
writer.WriteLine(item.ToString());
}
Edit 2:
As #Servy suggested, the listbox could contain duplicates, which should also be taken into consideration. Additionally, my HashSet implementation was sub-par. So in this third example, I am first checking if the listbox contains duplicates, then if any of the listbox items are already in the file. The usage of HashSet is more performant as well because I am not iterating it.
var fileLines = File.ReadAllLines("test.txt");
HashSet<string> fileItems = new HashSet<string>(fileLines);
List<string> duplicateListboxItems = listBox1.Items.Cast<string>().GroupBy(l => l).Where(g => g.Count() > 1).Select(g => g.Key).ToList();
if (duplicateListboxItems.Count > 0)
{
MessageBox.Show("The listbox contains duplicate entries.");
return;
}
bool duplicateFound = false;
List<string> outputItems = new List<string>();
foreach (string item in listBox1.Items)
{
if (fileItems.Contains(item))
{
MessageBox.Show(String.Format("The file has a duplicate: {0}", item));
duplicateFound = true;
break;
}
outputItems.Add(item);
}
if (duplicateFound)
return;
using (StreamWriter writer = new StreamWriter("test.txt", true))
{
foreach (string s in outputItems)
writer.WriteLine(s);
}
string filePath = "test.txt";
var existingLines = new HashSet<string>(File.ReadAllLines(filePath));
var linesToWrite = new List<string>();
foreach (string item in listBox2.Items)
{
if (existingLines.Add(item))
{
linesToWrite.Add(item);
}
else
{
//this is a duplicate!!!
}
}
File.AppendAllLines(filePath, linesToWrite);

Windows Phone 7 Gif to Png

I am building an Rss reader app and having problems with gifs.
Does anyone know how to build the ImageConverter class so it would convert gif to png?
Converting gifs in code will also work for me.
My app works in a way that it takes everything from the feed, puts it in a list() first and then it populates the listbox, (in a way the user chooses to). gifs leave blank images :(
so basically converter would have to work with a link, not a direct data stream.
I will add some code on how my data is updated:
ObservableCollection<FeedItem> slika0; //where the feeditems from the selected category go
public ObservableCollection<FeedItem> Slika0
{
get { return slika0; }
set
{
slika0 = value;
OnPropertyChanged("Slika0");
}
}
int broj_elemenata = 0;
bool nema_elemenata = false;
private void UpdateFeedList(string feedXML)
{
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
List<string> kategorije = new List<string>();
foreach (SyndicationItem sitem in feed.Items)
{
foreach (SyndicationCategory sc in feed.Categories)
{
if (!kategorije.Contains(sc.Name))
{
kategorije.Add(sc.Name);
}
}
}
FeedItem FeedItem = new FeedItem();
Slika0 = new ObservableCollection<FeedItem>();
//SyndicationCategory tražena_kat = new SyndicationCategory("Trendy");
foreach (SyndicationItem item in feed.Items)
{
foreach (SyndicationCategory sc in item.Categories)
{
FeedItem.Content_List.Add(sc.Name);
foreach (SyndicationElementExtension ext in item.ElementExtensions)
{
XElement ele = ext.GetObject<XElement>();
if (ele.Name.LocalName == "encoded" && ele.Name.Namespace.ToString().Contains("content"))
{
FeedItem.Content_List.Add(item.Title.Text);
FeedItem.Content_List.Add(ele.Value);//takes the content of the feed
}
}
foreach (SyndicationLink link in item.Links)
{
FeedItem.Content_List.Add(link.Uri.AbsoluteUri); //takes the links for browsing and the image
}
}
}
IsolatedStorageSettings.ApplicationSettings["ContentList"] = FeedItem.Content_List;
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
int i;
int x = 0;
Slika0.Clear();
foreach (var item in FeedItem.Content_List)
{
i = FeedItem.Content_List.IndexOf(item, x); //x = index of category in the list
if (item == "Trendy")
{
FeedItem FF = new FeedItem();
FF.Post_text = FeedItem.Content_List[i + 1];//title of article
FF.Content_link = FeedItem.Content_List[i + 2];//content
FF.Post_link = FeedItem.Content_List[i + 3];//the location of link for browsing
FF.Post_slika = FeedItem.Content_List[i + 4]; //location of image link
if (FF.Post_slika == "") //if there is no link for picture code is executed
{
FF.Post_slika = "Slike/zimo_veliki_tile.png";
}
Slika0.Add(FF);
this.lsSlika_P.ItemsSource = Slika0; //take
x = i + 5;
broj_elemenata++;
}
}
if (lsSlika_P.Items.Count <= 3)
{
scroll_panorama.VerticalScrollBarVisibility = ScrollBarVisibility.Disabled;
}
if (lsSlika_P.Items.Count == 0)
{
nema_elemenata = true;
}
else nema_elemenata = false;
});
pan_progres.Visibility = Visibility.Collapsed;//progress bar
}

Categories

Resources