C# How read a string to listView? [closed] - c#

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
How to load string from a txt file to the listView?
I have a txt file with three lines of characters.
I read the first line of a txt file into the first row in listView and etc.?

Well if your line contains everything you need and doesn't need to be split after you can use ListBox instead of ListView
foreach(string line in File.ReadAllLines(pathToYourFile))
ListBox.Items.Add(line);
Or if you really need ListView you can use
foreach(string line in File.ReadAllLines(pathToYourFile))
listView.Items.Add(new ListViewItem(line));

Use iostreamreader .. then use readline function .. then fill the listview

Try something like this:
string[] lines = System.IO.File.ReadAllLines(#"yourtextfile");
foreach (string line in lines)
{
listView1.Items.Add(line);
}

Here is a Linq example for you.
using System.Linq;
...
System.IO.File.ReadAllLines(pathToFile)
.ToList()
.ForEach(line => listView.Items.Add(new ListViewItem(line)));

First,
using System.IO; <-- to read the file
Then, if you can use a listbox, addrange works well without the loop:
listBox1.Items.Clear();
string[] s_array = File.ReadAllLines( -- your file path -- );
listBox1.Items.AddRange(s_array);
If you're using a list view, then the loop suggested by Blablablaster above works well

Related

Sorting pictures into folders by date taken [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 9 years ago.
I want to make software that groups pictures into folders by date actually taken. The pictures will sort into folders with names the year taken like:
Folder: 2000
Inside the folder: Some pictures taken in the 2000.
How can I do that?
To get the date the picture was actually taken, you want to look at the Exif data.
This data is automatically read into the PropertyItems array when you use Image.FromFile(). You can then use another reference (like this one) to get the right codes for date info. You could also use this library to simplify reading the codes.
Not all images will have Exif data, so you may want to incorporate David's answer as a fallback.
Once you have the relevant date info, you can use Directory.Create(year) and File.Move(oldPath, newPath) to organize the files.
List<string> imageFiles= ... // Here you get the image path
Dictionary<int, List<string>> groupedPaths= ... //output dict
foreach(string str in imageFiles)
{
FileInfo fi=new FileInfo(str);
int year = fi.CreationTime.Year;
if(!groupedPath.ContainsKey(year))
{
var list=new List<string>();
list.Add(year, string);
groupedPaths.Add(year, list);
}
else
{
groupedPaths[year].Add(year, str);
}
//Now you can process with foreach or use LINQ to group your images
foreach(KeyValuePair<int, string> pair in groupedPaths)
{
...
}

The fastest way to check number in txt file [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
H iguys. I have a loop which parse users id's and adds tham to txt file.
What's the best way than to check if this txt has this id skip it ( while next parsing ).
The size of txt rises from 5-..... mb
I tried to add ids to List, but when the size of file if bigger than 5mb the app begins hanging
Use a HashSet<int> or HashSet<string>, collect the ids in it, then at the end write the result to the text file.
PS: Note that HashSet is O(1) while List is O(n)
You should probably load all of the IDs in the text file into some collection and check if that collection contains the IDs.
I honestly don't think that there's a much more efficient way of doing it than that.
A rule of thumb I believe is to trade time with space. If you want to make copying faster and avoid looking into the file again and again then you may maintain an array or linked list or hash table which also have the id stored in it
var userIsAlreadyThere = File.ReadLines(path).Contains(userid);

compile and execute lines of code from txt in C# [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have on text s this lines:
myData = myData.Replace(".jpg", ">JPG<");
myData = myData.Replace(".gif", ">GIF<");
myData = myData.Replace(".png", ">PNG<");
myData = myData.Replace(".tif", ">TIF<");
and on my C# program i wont one by one, on a cicle for:
for (int l=0; w<listWithLines.Count;l++)
{
// MY LINE
// listWithLines[l]
}
If your intention is just to do string replacement (as in the example lines), and you are able to modify the text list, the best approach would be to provide just the replacement tokens in the list:
.jpg,>JPG<
.gif,>GIF<
.png,>PNG<
.tif,>TIF<
Then your C# code can be modified like this:
for (int l=0; w<listWithLines.Count;l++)
{
string[] strTokens = listWithLines[l].Split(',');
// MY LINE
myData = myData.Replace(strTokens[0], strTokens[1]);
}
I dont think you can do that, you can compile a block of code from an external source using CodeProviders etc but I dont think you can just drop it into a predefined scope like you seem to want to (the scope being within your for loop), unless you can load it as a block and pass it in to a method (which would do the loop) as an Action.
I'm not aware of an easy way to compile and run lines of code from a text file this way. But if you were to provide methods for myData objects to be serialized and deserialzed using XML you could read in lines from an external file to do something similar to this.
You could maybe do that but it's lot of work with things like Reflection.Emit. Pretty sure you'd have to an entire class as well
You could use IronPython or one of the other DLR implementations to do it, but would be a good bit of work as well
Turn it into an xml
<Replaces>
<Replace from=".jpg" to=">JPG<" />
<Replace from=".gif" to=">GIF<" />
</Replaces>
Then do something like
XmlDocument doc = new XmlDocument();
doc.Load("Replaces.xml")
foreach(XmlNode replaceNode in doc.DocumentElement.SelectNodes("Replaces/Replace"))
{
myData = myData.Replace(replaceNode.Attributes["from"].Value, replaceNode.Attributes["to"].Value);
}

Comparing specific columns of two csv files [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I have two csv files with like 18 columns each...I would like with c# to compare the first column of the first file with the first column of the second file and the third column of the first with the third column of the second and when a difference is found,I want to be saved to another file so the third file should have as an output two columns.So somehow I need to tell which is the first column and which is the third column and then compare.
Any suggestions how can i achieve this?
The basics are reading each file line by line and then splitting each string. With a CSV file you typically have a comma as seperator, but this could be a tab or similar char as well. So use the one you have. You then get something similar to
string line;
System.IO.StreamReader file = new System.IO.StreamReader("c:\\test.csv");
while ((line = file.ReadLine()) != null)
{
var arr = line.Split(new char[] { ',' });
// do your comparison
}
though you nee to open 2 files. You can then compare the array from file 1 with the array from file 2 (index into array == column, starting at 0). Smilarly, you can use String.Join to create your output again.
browse 2 excel sheets to a 2 Datatable. Comapre 2 tables and if there is difference put it in the new table.
Finally export the table to Excel.

Search in a text file from one symbol to another [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
I am coding program, and stacked. Please can give me a code which search text in file from one specific symbol to another using C# visual Windows Forms , not console application. Like this text in textfile c:\id.txt
The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.
search string from { to } , and result with { and }, without . at the end. And send found text in a message box.Code to search text in a file an send whole line in message box. But i need part of line.
Regex can be useful:
MessageBox.Show(
Regex.Match(inputString, "\{(?<path>[^}]*)\}").Groups["path"].Value);
explain:
{ '{'
[^}]* any character except: '}'
(0 or more times, matching the most amount possible)
} '}'
Try by using regular expressions:
var line = " The entry was successfully copied to {ea4c4653-cc65-11e1-a2fc-001e101f4e71}.";
var foo = Regex.Match(line, #"to\s*\{([^}]+)\}");
if(foo.Success) {
MessageBox.Show(foo.Groups[1].Value); //ea4c4653-cc65-11e1-a2fc-001e101f4e71
} else {
//not found value
}

Categories