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.
Related
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
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)
{
...
}
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 have a txt file that is Tab Delimited, like this:
Well Name Detector Task Quantity
1 try1 Cam1 UNKN 0
2 try2 Cam1 UNKN 0
3 try3 Cam3 STND 1000
4 try4 Cam5 UNKN 0
5 try5 Cam6 UNKN 0
6 try6 Cam6 UNKN 0
....
92 try92 Cam4 STND 100
I need to output a CSV file from this TXT file.
In the outputted CSV file I need to change the delimiter from Tab to semi-colon and I only need the first two columns.
I put the text file in an array with:
string [] source....
also to skip some header lines I don't need,
But now I'm stuck at how to go on.
I know about StreamReader and StreamWriter but I don't know how to use them to accomplish my task.
Any help would be kindly appreciated.
Thanks!
Hope this gives you the idea, so try to understand each part.
string[] lines = System.IO.File.ReadAllLines(YOUR INPUT FILE);
StringBuilder builder = new StringBuilder();
foreach (string line in lines)
{
var temp = line.Split('\t');
builder.AppendLine(string.Join(";", temp[0], temp[1]));
//builder.AppendLine(string.Format("{0}; {1}", temp[0], temp[1]));
}
System.IO.File.WriteAllText(YOUR OUTPUT FILE, builder.ToString());
File.ReadAllLines: Read all lines of a file into a string[]
File.WriteAllText: Write a string into a file.
StringBuilder: This class represents a string-like object.
AppendLine: Adds a string line to the StringBuilder instance.
string.Format: Replaces each format item in a specified string with the text equivalent of a corresponding object's value. More
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.
It has soon been 12h of trying this and I can't get it to work. I have read all the threads I can find and nothing helps. I have tried to fiddle around with Excel Wrapper as well but it doesn't work. I'm new to C# and I'm trying to do a bullshit generator. What I am trying to do is reading a bunch of words from A1-A5, B1-B5 and C1-C5 in an .xlsx-file and putting them together in a textbox (I'm using Visual Studio) when clicking a button.
If anyone reads this and could give me a hint it would be much appreciated. Thanks in advance.
First you should pull the info from the xls doc to a collection (array, list, etc...)
The code for this should be easy to find online.
You are also going to need a random number generator:
Random rnd = new Random();
Then you are going to have the button click event select 2 random numbers, one for row and one for column, from your collection (2D array in this case):
int row_max = stuff[][].GetLength(0);
int col_max = stuff[][].GetLength(1);
int row = rnd.Next(0, row_max-1)
int col = rnd.Next(0, col_max-1); //between 0 and the number of columns
textbox1.text = textbox1.text + stuff[row][col].ToString();
This is indicative only, but all the parts of this can be easily googled.
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);