How to store data in datagrid using regex and remove dublicates C# [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed yesterday.
Improve this question
I need some help please. I need to remove dublicates and use regex when load a list of email adresses in datagrid. Regex must check for syntax email adress to be correct.
Here is my code:
public void StoreEmailToDataGrid(string filePath, DataGridView dataGridView)
{
try
{
var baca = File.ReadLines(filePath);
foreach (var line in baca)
{
var rowIndex = dataGridView.Rows.Add();
dataGridView.Rows[rowIndex].Cells[0].Value = rowIndex + 1;
dataGridView.Rows[rowIndex].Cells[1].Value = line;
}
this.totalRows = dataGridView.Rows.Count;
}
catch (IOException)
{
}
}
private void buttonImportMailist_Click(object sender, EventArgs e)
{
string file = "";
openFileDialog1.Filter = "Text Files|*.txt";
openFileDialog1.Title = "Select Mailist";
openFileDialog1.FileName = file;
DialogResult result = openFileDialog1.ShowDialog(); // Show the dialog.
if (result == DialogResult.OK) // Test result.
{
file = openFileDialog1.FileName;
try
{
email.StoreEmailToDataGrid(file, dataMailist);
}
catch (IOException)
{
MessageBox.Show(result.ToString());
}
}
}
UPD: Remove dublicates solved
var baca = File.ReadLines(filePath).Distinct().ToArray()
Remain, how to use regex to check correct email adresses.

Solved the problem. Just use regex.ismatch
Regex.IsMatch(strIn, #"^([\w-.]+)#(([[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.)|(([\w-]+.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$");

Related

C# high score in game [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
Bassicaly I'm stucked with displaying high scores via listbox by discending order(like 500 to 1). Here is my code, keep in mind that label1 is score in the game, so if anyone may help me please?
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{
label1.Text = Form2.passingText;
StreamWriter q = new StreamWriter("C:\\Users\\BS\\Desktop\\tex.txt", true);
q.WriteLine(label1.Text);
q.Close();
StreamReader sr = new StreamReader("C:\\Users\\BS\\Desktop\\tex.txt");
string g = sr.ReadLine();
while (g != null)
{
listBox1.Items.Add(g);
g = sr.ReadLine();
}
sr.Close();
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
You can read file as list of lines and then sort it using Linq
So, instead of using SteamReader, try this:
using System.Linq;
//....
List<string> hiscores = File.ReadAllLines("C:\\Users\\BS\\Desktop\\tex.txt").ToList();
hiscores.Sort();
foreach (string s in hiscores)
listBox1.Items.Add(s);
EDIT:
since you have to use StreamReader, here is this approach (but the principle is same):
List<string> hiscores = new List<string>();
StreamReader sr = new StreamReader("C:\\Users\\BS\\Desktop\\tex.txt");
string g = sr.ReadLine();
while (g != null)
{
hiscores.Add(g);
g = sr.ReadLine();
}
sr.Close();
hiscores.Sort();
hiscores.Reverse();
//alternatively, instead of Sort and then reverse, you can do
//hiscores.OrderByDescending(x => x);
foreach(string s in hiscores)
{
listBox1.Items.Add(s);
}

Selenium find array contents on page

I'm using Visual Studio with Selenium to build an application that goes to a web page and finds if the contents of an array are on the page. I'm running into an issue with searching the page for the array contents.. Right now it finds nothing, so it clicks to go to the next page when it shouldn't.
The array comes from a CSV file I'm loading in and it needs to search the page for a match of any of the records from the CSV file and stops.
Here is what I have so far:
OpenFileDialog ofd = new OpenFileDialog();
private double timeOut;
private void bttnImportBrowse_Click(object sender, EventArgs e)
{
ofd.Filter = "CSV|*.csv";
var fileInputs = new List<string>();
if (ofd.ShowDialog() == DialogResult.OK)
{
String chosenFile = ofd.FileName;
String safeFileName = ofd.SafeFileName;
try
{
// Create an instance of StreamReader to read from a file.
// The using statement also closes the StreamReader.
using (StreamReader sr = new StreamReader(chosenFile))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
//Console.WriteLine(line);
fileInputs.Add(line);
//Console.Write(string.Join(" ", fileInputs));
var driver = new ChromeDriver(#"C:\Users\andre_000\Documents\Visual Studio 2015\Projects\MyProject\");
driver.Navigate().GoToUrl("MySite");
var WebDriverWait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeOut)).Until(ExpectedConditions.ElementExists((By.XPath("/html/body/a[2]"))));
while (1==1) {
try
{
var result = driver.FindElement(By.LinkText(fileInputs.ToString()));
break;
}
catch (NoSuchElementException n)
{
var nextBttn = driver.FindElementByXPath("/html/body/a[2]");
nextBttn.Click();
}
}
}
}
}
catch (Exception entry)
{
// Let the user know what went wrong.
Console.WriteLine("The file could not be read:");
Console.WriteLine(entry.Message);
}
}
}
Sorry i would have left a comment but am not allowed to yet. Do you have the CSV file?
I believe you are trying to find the Link text incorrectly.
Currently calling ToString() on the list
var result = driver.FindElement(By.LinkText(fileInputs.ToString()));
should probably be
var result = driver.FindElement(By.LinkText(line));

C#: About System.ArgumentNullException [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
static void X(String a,String b,String c)
{
TextBox textBox3 = new TextBox();
a = textBox3.Text;
if (a == " ")
{
throw new ArgumentNullException(a);
}
TextBox textBox4 = new TextBox();
b = textBox4.Text;
if (b == null)
{
throw new ArgumentNullException(b);
}
TextBox textBox5 = new TextBox();
c = textBox5.Text;
if (c == null)
{
throw new ArgumentNullException(c);
}
}
private void Go2_Click(object sender, EventArgs e)
{
try
{
........//my code
X(Sx,Sy,V);
........// my some code
}
catch (System.ArgumentNullException)
{
MessageBox.Show("Your String is not correct");
}
}
My program cannot do System.ArgumentNullException. How can I solve this program?Please, guide.
You need to use the string.IsNullOrEmpty() to check the values for your string variables. As you are assigning them values in your method using textbox.Text. They get the string.Empty values assigned to them. And Your if statements never evolves to true and they never throw the exception.
If you alter your conditions like this then that would work,
if (string.IsNullOrWhiteSpace(a))
{
throw new ArgumentNullException("Thrown from first condition");
}
to check if a string is null use the dedicated functions for this and initialize the ArgumentNullException with the parameter name and not with the parameter.
For example instead of
if (a == " ")
{
throw new ArgumentNullException(a);
}
write
if (string.IsNullOrWhiteSpace(a))
{
throw new ArgumentNullException("a");
}
for
if (c == null)
{
throw new ArgumentNullException(c);
}
write
if (string.IsNullOrEmpty(c))
{
throw new ArgumentNullException("c");
}
Try instead of :
throw new ArgumentNullException(c);
catch (System.ArgumentNullException)
{
MessageBox.Show("Your String is not correct");
}
This :
throw new ArgumentNullException("Your String is not correct");
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

How can I check if the path entered by user is valid or not? [duplicate]

This question already has answers here:
Check whether a path is valid
(12 answers)
Closed 8 years ago.
I have found this code while I was busy searching for an answer!
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog saveFileDialogBrowse = new OpenFileDialog();
saveFileDialogBrowse.Filter = "Pcap file|*.pcap";
saveFileDialogBrowse.Title = "Save an pcap File";
saveFileDialogBrowse.ShowDialog();
var pcapFile = saveFileDialogBrowse.FileName; //do whatever you like with the selected filename
if (pcapFile != "")
{
FileInfo fileInfo = new FileInfo(pcapFile);
txtFilePath.Text = fileInfo.FullName;
}
}
There is no easy way.
You can use File.Exists to check for file existence on the path, but a change can still happen before the execution of the next line. Your best option is to combine File.Exists with try-catch to catch any possible exceptions.
private void btnOpenFile_Click(object sender, EventArgs e)
{
OpenFileDialog saveFileDialogBrowse = new OpenFileDialog();
saveFileDialogBrowse.Filter = "Pcap file|*.pcap";
saveFileDialogBrowse.Title = "Save an pcap File";
saveFileDialogBrowse.ShowDialog();
var pcapFile = saveFileDialogBrowse.FileName; //do whatever you like with the selected filename
try
{
if (File.Exists(pcapFile))
{
FileInfo fileInfo = new FileInfo(pcapFile);
txtFilePath.Text = fileInfo.FullName;
}
}
catch (FileNotFoundException fileNotFoundException)
{
//Log and handle
}
catch (Exception ex)
{
//log and handle
}
}
You can use the File.Exists method:
string fullPath = #"c:\temp\test.txt";
bool fileExists = File.Exists(fullPath);
You can use the File.Exists method, which is documented here.

How to open the file if path is known? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am having path in listbox.I want to open the file on listbox item click.
I am using SelectedIndexChanged,From that I am able to know item which is clicked. Now, I want to open that.
public void OpenDialogBox()
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "*YOURTEXTBOX.text";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
//Do something with Open File
}
}
catch (Exception ex)
{
// You messed up
}
}
}

Categories

Resources