C# listbox split to another 2 listboxes - c#

Problem:
I have 1 ListBox which loads a text file contain:
ip:port
ip:port
ip:port
What I want to do is, once I've loaded the text file into the list box, I want to have the 'ip' to go into a different listbox and the 'port' into a different listbox. This is first time working on a project like this.

// if you wanted to do it with LINQ.
// of course you're loading all lines
// into memory at once here of which
// you'd have to do regardless
var text = File.ReadAllLines("TestFile.txt");
var ipsAndPorts = text.Select(l => l.Split(':')).ToList();
ipsAndPorts.ForEach(ipAndPort =>
{
lstBoxIp.Items.Add(ipAndPort[0]);
lstBoxPort.Items.Add(ipAndPort[1]);
});

Something like
using (StreamReader sr = new StreamReader("TestFile.txt"))
{
String line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
string[] ipandport = line.split(":");
lstBoxIp.Items.Add( ipandport[0] );
lstBoxPort.Items.Add( ipandport[1] );
}
}

Related

In C# can we update a text file line with new record while reading the same file

I have .text File (with 400K rows). I need to read that file and If i find (AB,CB,DE,FG) in certain position (35, 2) in each row, I need to replace that with new value which I already have in dictionary object.
this is my dictionary with list of policy number and respective group:
ListofPolicy[10001,MM]
ListofPolicy[10005,KK]
ListofPolicy[10011,NN]
ListofPolicy[10018,YY]
ListofPolicy[10020,GG]
etc...
This is my sample .txt file: I need to read the line.Substring(35, 2) will tell us which group it is.
P00002398911100010131220111061553XXAB549099QSTJDK6016
HUI001004117577408867289000000007209171
P00002398918100058882220111061459YYLT518435BIVQZC1855
P00002398916726561656220111103331XXKY518435BIVQZC1855
PPP001CSTON
P00002398911001136778220111103329XXCB511100QSBUPO1128
HUI001004117577408867289000000007209171
P00002398911001888877220111103323XXKI518435BIVQZC1855
PMT001CSTON
P00002398911002066656220111103320YYFG511100QSBUPO1128
HUI001004117183000000007209169
P00002398917409185763220111103316XXDF511100QSBUPO1128
How do I approach this ?
If I understand you correctly, you need something like that:
string content = "";
using (StreamReader sr = new StreamReader("path\\to\\your\\file.txt"))
{
content = sr.ReadToEnd();
}
using (StreamWriter sw = new StreamWriter("path\\to\\your\\file.txt"))
{
foreach (string line in content.Split('\n'))
{
if (yourDictionary.Keys.Contains(line.Substring(35, 2)))
{
sw.WriteLine($"{line.Substring(0, 35)}{yourDictionary[line.Substring(35, 2)]}{line.Substring(37, line.Length - 37)}");
}
else
{
sw.WriteLine(line);
}
}
}

C# WPF How do I display the contents of a .txt file into a list box?

Say I have a txt file with this content:
Tom, 11
Jason, 12
Gary, 13
Ted, 14
The WPF is just a list box.
What would I need to do for the list box, to show the names inside the txt file when I start the program.
This is a very simple question, but I cant figure it out. I don't know where the txt file needs to be saved and I don't know how to call it in the ".cs"
This is code which read next line to list,then read how to add this to listbox
List<string> lines = new List<string>();
using (StreamReader r = new StreamReader(f))
{
string line;
while ((line = r.ReadLine()) != null)
{
lines.Add(line);
}
}
This is example how to binding list to listbox:
eventList.ItemsSource = lines;
Text file can be anywhere as you can specify path to it while opening. You can put it inside solution folder to make path shorter.
Then in main method you write something like
var MyList = new List<string>();
using (var streamReader = File.OpenText(pathToYourTextFile))
{
var s = string.Empty;
while ((s = streamReader.ReadLine()) != null)
MyList.Add(s);
}
myListbox.ItemsSource = MyList;

Reading Text from Comma-Delimited file

I have the following text file called urlmap.txt in App_Data
ShowProduct.aspx?ID=1143, laptops/1143/laptops-for-beginner-experienced-engineers
ShowProduct.aspx?ID=1142, desktops/1142/dynamic-difference-desktops
ShowProduct.aspx?ID=1141, keyboards/1141/bluetooth-ready-keyboards
ShowProduct.aspx?ID=1140, mouse/1140/microsoft-2key-mouse-with-pad
ShowProduct.aspx?ID=1139, mouse/1139/logitech-3key-mouse-auto-shutoff
and about 2000 such entries....
I want to pass in a string like "ShowProduct.aspx?ID=1140" and search and retrieve the text in front of it i.e. "mouse/1140/microsoft-2key-mouse-with-pad"
If this string is not found, it retrieves nothing.
Each string in urlmap.txt is unique, so there is no chance of duplication
How can I do this?
This is what I have so far but I am unable to determine how to retreive the text in front of it
string line;
StringBuilder sb = new StringBuilder();
using (System.IO.StreamReader file = new System.IO.StreamReader("App_Data\urlmap.txt"))
{
while ((line = file.ReadLine()) != null)
{
if (line.Contains(mySearchString))
{
}
}
}
Since the text file contains about 2000+lines, I also need an optimized way of retrieving the record.
Just use Split:
if (line.Contains(mySearchString))
{
var text = line.Split(",")[1];
/*do something else with text*/
}

Load Config file into Windows Forms

I am attempting to build a tool using C# to modify the config files for our software. The config files are in the following format:
SERVER_NAME TestServer
SERVER_IP 127.0.0.1
SERVICE_NUMBER 4
SERVICE_ID 1 2 3 4
And so on. Each line is prefaced with an identifier(Ex: SERVER_NAME), then the value. I need the tool to load the value for each identifier into a separate text box. When the user clicks save, it needs to write the updated information to the file.
I am entirely lost on how I should load the data into the text boxes, so if you could provide some help on that, I would appreciate it.
Writing it, I am assuming the easiest way, since all of the data will be loaded, is to erase the previous data, and write the new data to the file. This I should be able to handle without a problem. If there is a better way to do this, I am definitely willing to try.
I would greatly appreciate some pointers on how to get started with loading the data.
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFD.ShowDialog();
openFD.Title = "Open a Config File...";
openFD.InitialDirectory = "C:";
openFD.FileName = "";
openFD.Filter = "CONFIG|*.cfg";
string selected_file = "";
selected_file = openFD.FileName;
using (StreamReader sr = new StreamReader(selected_file))
{
string currLine;
while ((currLine = sr.ReadLine()) != null)
{
}
}
}
To read each line of the file you could do something like this:
// StreamReader is in System.IO
using(StreamReader sr = new StreamReader("config file path here"))
{
string currLine;
while((currLine = sr.ReadLine()) != null)
{
// currLine will have the current line value as a string
// You can then manipulate it any way you like
// Or store it in an array or List<>
}
}
If you need help with adding items into a textbox, just ask.
Hope this helps!

C# Listbox sort items

Im new to programming and Im having a problem with a listbox. Im reading text from a file, and I want the last file in the file to be the first in the listbox. How to I do this??
This is a school project :)
This is the code I have so far:
if (File.Exists(file))
{
FileInfo fileInfo = new FileInfo("nema.csv");
StreamReader read = fileInfo.OpenText();
while (!read.EndOfStream)
{
listBox1.Items.Add(read.ReadLine());
}
read.Close();
}
it's hard to tell without code but basically you have to use Insert(0,item) instead of Add(item) to reverse the order. The code coud look something like this:
using(var reader = System.IO.File.OpenText(pathOfFile))
{
myListBox.Items.Insert(0, reader.ReadLine());
}
Read the contents of the file.
Put them in a list
Add the items that
are in the list to the ListBox, but make sure you start from the last
item in the list, and go to the first.
To add a new object at the first place of the listbox
listbox.Items.Insert(0, objectToAdd)
I assume you to handle read textfile
While Reading TextFile store all string in a List Collection.
List<string> listItems = new List<string>();
FileStream fs = new FileStream(#"c:\YourFile.txt", FileMode.Open);
StreamReader sr = new StreamReader(fs);
string line = "";
int lineNo = 0;
do {
line = sr.ReadLine();
if (line != null) {
listItems.Add(line);
}
} while (line != null);
listItems.Sort();
foreach(string s in listItems)
{
yourListBox.Items.Add(s);
}
Just use Listview either than listbox.
Go to properties of ListView
Click the SORTING
Choose descending

Categories