How to get input from textbox in C#? - c#

First things first,I am very new to C# and programming all around. I have a standalone program that will read XML files in a certain location and convert them to plaintext files.
And I have a windows forms app that has a file directory and will display the chosen file in a textbox.
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((myStream = openFileDialog1.OpenFile())!= null)
{
string strfilename = openFileDialog1.FileName;
string filetext = File.ReadAllText(strfilename);
textBox1.Text = filetext;
}
}
}
Below is a snippet of my conversion program.
string[] files = Directory.GetFiles("C:\\articles");
foreach (string file in files)
{
List<string> translatedLines = new List<string>();
string[] lines = File.ReadAllLines(file);
foreach(string line in lines)
{
if (line.Contains("\"check\""))
{
string pattern = "<[^>]+>";
string replacement = " ";
Regex rgx = new Regex(pattern);
string result = rgx.Replace(line, replacement);
translatedLines.Add(result);
}
}
How would I modify my program to take the input from the textbox and then perform its conversion?
(And Yes I'm aware I have to combine the two programs.)

Use XDocument class to parse the XML formatted string to XML Document so that you can get values on each Nodes of the XML
XDocument xDoc = new XDocument();
xDoc = XDocument.Parse(filetext);
Now read the content:
var textValue = xDoc.Descendants("Response").First().Attribute("Status").Value;

Related

How to split a line to a new line when specific condition meets in a csv file?

I have the below data in my csv file, I want to split each line to a new line when "05/16/2019" text is identified using c#.
Currently in my csv file data format is like below:
05/16/2019,PAPER,190516-TRE-5419,GbK,R0000001,1,05/16/2019,PAPER,190516-TRE-5419,GSK,R0000001,1,05/16/2019,PAPER,190516-TRE-5419,GSK,R0000001,1
I want to change like below:
05/16/2019,PAPER,190516-TRE-5419,GSK,R0000001,1
05/16/2019,PAPER,190516-TRE-5419,GSK,R0000001,1
05/16/2019,PAPER,190516-TRE-5419,GSK,R0000001,1
I am new to c# programming, Can anyone please help me with the Code?
1st Step: I am Uploading my csv file
2nd Step: I am trying to change my csv as above requirement
3rd Step: Exporting the modified csv to a new location
List<string> csvfiles = new List<string>();
private void btnimport_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.CheckFileExists = true;
openFileDialog.AddExtension = true;
openFileDialog.Multiselect = true;
openFileDialog.Filter = "CSV files (*.csv)|*.csv";
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
foreach (string fileName in openFileDialog.FileNames)
{
csvfiles.Add(fileName);
}
}
}
public void csvedit()
{
String path = #"C:\Users\Sunil\Videos\original\GSK.csv";
Regex r = new Regex(#",(?=[0-9]{2}\/[0-9]{2}\/[0-9]{4})"); // looks for the comma just before the date
var newStr = r.Replace(path, "\r\n");
}
private void btnexport_Click(object sender, EventArgs e)
{
csvedit();
string installedPath = "C:\\Users\\Sunil\\Videos\\changed";
//Check whether folder path is exist
if (!System.IO.Directory.Exists(installedPath))
{
// If not create new folder
System.IO.Directory.CreateDirectory(installedPath);
}
//Save pdf files in installedPath ??
foreach (string sourceFileName in csvfiles)
{
string destinationFileName = System.IO.Path.Combine(installedPath, System.IO.Path.GetFileName(sourceFileName));
System.IO.File.Copy(sourceFileName, destinationFileName);
MessageBox.Show("File Exported Successfully");
}
}
Assuming that the current CSV format is available as string in your code, you can use the regular expressions for formatting the way you want. Try with this:
Regex r = new Regex(#",(?=[0-9]{2}\/[0-9]{2}\/[0-9]{4})"); // looks for the comma just before the date
var newStr = r.Replace(str, "\r\n"); // here 'str' is the input string which contains unformatted csv file content.
For your requirement, you need to modify the csvedit() as follows:
public void csvedit()
{
string path = #"C:\Users\Sunil\Videos\original\GSK.csv";
string csvContent = System.IO.File.ReadAllText(path);
Regex r = new Regex(#"("",""(?=[0-9]{2}\/[0-9]{2}\/[0-9]{4}))"); // looks for the 'double quotes - comma - double quotes' pattern just before the date
var newStr = r.Replace(csvContent, "\n").Trim("\"".ToArray());
System.IO.File.WriteAllText(path, newStr); //this will overwrite the text present in existing file.
}

How to link users file selection to a StringSplit

I'm trying to get the users chosen file to split into an array which is separated by , and /t.
The user can choose a file at the top of the code but how do I get their choice to split into an array lower down when they press the ValidateButton
The text file or ordered in this way
info,info,info,
info,info,info,
info,info,info,
If I can get them into an array then I can easily organises the data.
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = #"C:\"; // Start in C: drive
openFileDialog1.Title = "Browse Text Files";
openFileDialog1.RestoreDirectory = true;
openFileDialog1.DefaultExt = "txt"; // Extension of file is txt
openFileDialog1.Filter = "Text|*.txt||*.*"; //Only text files
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
FileNameBox.Text = openFileDialog1.FileName; // Chosen file name is displayed in text box
var fileStream = openFileDialog1.OpenFile();
using (StreamReader reader = new StreamReader(fileStream))
{
var fileContent = reader.ReadToEnd();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
//This is the file path the user has chosen
}
public void ValidateButton_Click(object sender, EventArgs e)
{
//Call Multi line text box
//Call PROGRESS BAR_
int counter = 0;
string lines;
string Patient = lines;
string[] split = Patient.Split(new Char[] { ',', '\t' });
foreach (string s in split)
if (s.Trim() != "")
Console.WriteLine(s);
{
Console.WriteLine(lines);
counter++;
}
Console.WriteLine("There were {0} records.", counter);
Console.ReadKey();
}
List<string> temp = new List<string>();
string[] finalArray;
using (StreamReader reader = new StreamReader(fileStream))
{
// We read the file then we split it.
string lines = reader.ReadToEnd();
string[] splittedArray = lines.Split(',');
// We will check here if any of the strings is empty (or just whitespace).
foreach (string currentString in splittedArray)
{
if (currentString.Trim() != "")
{
// If the string is not empty then we add to our temporary list.
temp.Add(currentString);
}
}
// We have our splitted strings in temp List.
// If you need an array instead of List, you can use ToArray().
finalArray = temp.ToArray();
}

How to get all the files form a directory where name does not contains 0?

Recently I have built a small converter that converts txt data to xml in a certain structure , I choose a folder and the program loops through all the files in that folder and write in a XML format all together in one xml document.
In the folder I have data names like:
Data.0001.txt
Data.0002.txt
Data.0003.txt
Data.0004.txt
Data.txt
and so on
I want only the files that dose NOT contain zeros in them because ones with zeros are just a back up copy for the others , and i have over 6000 file i can't filter them manually
Here is my code so far
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = #"C:\newpages";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
var txt = string.Empty;
string[] Files = Directory.GetFiles((SelectFolder.SelectedPath));
int i = 1;
foreach (string path1 in Files)
{
String filename = Path.GetFileNameWithoutExtension((path1));
using (StreamReader sr = new StreamReader(path1))
{
txt = sr.ReadToEnd();
XmlElement id = doc.CreateElement("Page.id");
id.SetAttribute("Page.Nr", i.ToString());
id.SetAttribute("Pagetitle", filename);
XmlElement name = doc.CreateElement("PageContent");
XmlCDataSection cdata = doc.CreateCDataSection(txt);
name.AppendChild(cdata);
id.AppendChild(name); // page id appenndchild
root.AppendChild(id); // roots appenedchild
doc.AppendChild(root); //Main root
}
i++;
}
}
Console.WriteLine("finished");
Console.ReadKey();
doc.Save(Path.ChangeExtension(path, ".xml"));
}
}
Any help would be really nice guys
GetFiles returns the name of a file in a specified directory. Its return type is string[] so you can easily apply a Where to filter the file names as follow:-
var files = Directory.GetFiles("PathToYourDirec").Where(name => !name.Contains("0"));
On the string filename you could make sure it doesn't contain "0"
if(!filename.Contains("0"))
{
}
On the Files variable you can use a regex to filter out the filenames which contains only letters
var reg = new Regex(#"^([^0-9]*)$");
var files = Directory.GetFiles("path-to-folder")
.Where(path => reg.IsMatch(path))
.ToList();
The whole code can be largely simplified while solving this issue. You don't need a StreamReader just to read the whole file, and you may as well get the filename early and filter instead of going into the foreach and filtering :
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = #"C:\newpages";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
// Don't declare txt here, you're overwriting and only using it in a nested loop, declare it as you use it there
// var txt = string.Empty;
//string[] Files = Directory.GetFiles((SelectFolder.SelectedPath));
// Change to getting FileInfos
var Files = new DirectoryInfo(SelectFolder.SelectedPath).GetFiles()
// Only keep those who don't contain a zero in file name
.Where(f=>!f.Name.Contains("0"));
int i = 1;
foreach (var file in Files)
{
//String filename = Path.GetFileNameWithoutExtension((path1));
// Don't need a StreamReader not a using block, just read the whole file at once with File.ReadAllText
//using (StreamReader sr = new StreamReader(path1))
//{
//txt = sr.ReadToEnd();
var txt = File.ReadAllText(file.FullName);
XmlElement id = doc.CreateElement("Page.id");
id.SetAttribute("Page.Nr", i.ToString());
id.SetAttribute("Pagetitle", file.FullName);
XmlElement name = doc.CreateElement("PageContent");
XmlCDataSection cdata = doc.CreateCDataSection(txt);
name.AppendChild(cdata);
id.AppendChild(name); // page id appenndchild
root.AppendChild(id); // roots appenedchild
doc.AppendChild(root); //Main root
//}
i++;
}
}
Console.WriteLine("finished");
Console.ReadKey();
doc.Save(Path.ChangeExtension(path, ".xml"));
}
I would also advise not working with the XML api you're using but with the more recent and simpler linq to XML one as that would simplify creating your elements too, see bellow a very simplified version of the whole code as i'd have writen it with LINQ and XElements
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = #"C:\newpages";
var root = new XElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
var FilesXML = new DirectoryInfo(SelectFolder.SelectedPath).GetFiles()
.Where(f => !f.Name.Contains("0"))
// Note that the index is 0 based, if you want to start with 1 just replace index by index+1 in Page.Nr
.Select((file, index) =>
new XElement("Page.id",
new XAttribute("Page.Nr",index),
new XAttribute("Pagetitle",file.FullName),
new XElement("PageContent",
new XCData(File.ReadAllText(file.FullName))
)));
// Here we already have all your XML ready, just need to add it to the root
root.Add(FilesXML);
}
Console.WriteLine("finished");
Console.ReadKey();
root.Save(Path.ChangeExtension(path, ".xml"));
}
You can try this, but I would suggest if you change the logic of creating name of backup files. It should not depend on "0" as character in it but instead of that firm text like "backup" should be mentioned in file name.
static void Main(string[] args)
{
FolderBrowserDialog SelectFolder = new FolderBrowserDialog();
String path = #"C:\newpages";
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("Pages");
if (SelectFolder.ShowDialog() == DialogResult.OK)
{
var txt = string.Empty;
string[] Files = Directory.GetFiles((SelectFolder.SelectedPath));
int i = 1;
foreach (string path1 in Files)
{
String filename = Path.GetFileNameWithoutExtension((path1));
if (!filename.Contains(".0"))
{
using (StreamReader sr = new StreamReader(path1))
{
txt = sr.ReadToEnd();
XmlElement id = doc.CreateElement("Page.id");
id.SetAttribute("Page.Nr", i.ToString());
id.SetAttribute("Pagetitle", filename);
XmlElement name = doc.CreateElement("PageContent");
XmlCDataSection cdata = doc.CreateCDataSection(txt);
name.AppendChild(cdata);
id.AppendChild(name); // page id appenndchild
root.AppendChild(id); // roots appenedchild
doc.AppendChild(root); //Main root
}
}
i++;
}
}
Console.WriteLine("finished");
Console.ReadKey();
doc.Save(Path.ChangeExtension(path, ".xml"));
}

how to select words in ms word searching for tags

I got a word document which I open from c# code (see code below). The word document could contain whatever a word document can (tables, pictures and whatsoever).
Enough information about the document. What I want is to search for a specific mark and then select the whole word.
EXAMPLE:
This could be text before a word which I want to find,
!#Command1 this could be text after a word I want to find
!#Command2 maybe a picture here or something
!#Command3
I want to select the 3 words (!#Command1, !#Command2, !#Command3) and add them to a list.
CODE:
public List<string> getListOfCommands()
{
List<string> commandList = new List<string>();
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
object fileName = ofd.FileName;
Word.Application wapp = GetWordApp();
var document = wapp.Documents.Open(fileName);
object findText = "!#"; //Commands tag
wapp.Selection.Find.ClearFormatting();
if(wapp.Selection.Find.Execute(findText))
{
//Add the !#Command to a list here
}
else
{
}
return commandList;
}
I spent some more time on it and found a workaround for the problem by addind and endTag to the commands.
The new code:
public List<string> getListOfCommands()
{
List<string> commandList = new List<string>();
OpenFileDialog ofd = new OpenFileDialog();
ofd.ShowDialog();
object fileName = ofd.FileName;
Word.Application wapp = GetWordApp();
var document = wapp.Documents.Open(fileName);
var range1 = document.Range();
var range2 = document.Range();
string findTextStart = "!#"; //Commands tagStart
string findTextEnd = "#!"; //Commands tagEnd
wapp.Selection.Find.ClearFormatting();
range1.Find.Execute(findTextStart);
while (range1.Find.Found)
{
if (range1.Text.Contains(findTextStart))
{
range2.Find.Execute(findTextEnd);
if (range1.End < range2.Start)
{
Word.Range temprange = document.Range(range1.End, range2.Start);
commandList.Add(temprange.Text);
}
else
{
commandList.Add("Error - Are you missing a start or end tag?");
}
}
range1.Find.Execute(findTextStart);
}
return commandList;
}

Reading Regular Expression in ASP.NET C#

I'm able to read and download list of .jpg files on a page using this regular expression
MatchCollection match = Regex.Matches(htmlText,#"http://.*?\b.jpg\b", RegexOptions.RightToLeft);
Output example: http://somefiles.jpg from this line
<img src="http://somefiles.jpg"/> in html
Question:How could I read files in this kind of format?
I just want to extract files with .exe on the page. So on the example above ^ I just want to get the datavoila-setup.exe file. Sorry I'm a little noob and confuse how to do it T_T. Thanks in advance for anyone who could help me. :)
this is my updated codes but I'm getting error on the HtmlDocument doc = new HtmlDocument(); part "No Source Available" and I'm getting an null value for list :(
protected void Button2_Click(object sender, EventArgs e)
{
//Get the url given by the user
string urls;
urls = txtSiteAddress.Text;
StringBuilder result = new StringBuilder();
//Give request to the url given
HttpWebRequest requesters = (HttpWebRequest)HttpWebRequest.Create(urls);
requesters.UserAgent = "";
//Check for the web response
WebResponse response = requesters.GetResponse();
Stream streams = response.GetResponseStream();
//reads the url as html codes
StreamReader readers = new StreamReader(streams);
string htmlTexts = readers.ReadToEnd();
HtmlDocument doc = new HtmlDocument();
doc.Load(streams);
var list = doc.DocumentNode.SelectNodes("//a[#href]")
.Select(p => p.Attributes["href"].Value)
.Where(x => x.EndsWith("exe"))
.ToList();
doc.Save("list");
}
this is Flipbed answer it works but not I'm not getting a clean catch :( I think there is something to edit on splitting the html to text
protected void Button2_Click(object sender, EventArgs e)
{
//Get the url given by the user
string urls;
urls = txtSiteAddress.Text;
StringBuilder result = new StringBuilder();
//Give request to the url given
HttpWebRequest requesters = (HttpWebRequest)HttpWebRequest.Create(urls);
requesters.UserAgent = "";
//Check for the web response
WebResponse response = requesters.GetResponse();
Stream streams = response.GetResponseStream();
//reads the url as html codes
StreamReader readers = new StreamReader(streams);
string htmlTexts = readers.ReadToEnd();
WebClient webclient = new WebClient();
string checkurl = webclient.DownloadString(urls);
List<string> list = new List<string>();//!3
//Splits the html into with \ into texts
string[] parts = htmlTexts.Split(new string[] { "\"" },//!3
StringSplitOptions.RemoveEmptyEntries);//!3
//Compares the split text with valid file extension
foreach (string part in parts)//!3
{
if (part.EndsWith(".exe"))//!3
{
list.Add(part);//!3
//Download the data into a Byte array
byte[] fileData = webclient.DownloadData(this.txtSiteAddress.Text + '/' + part);//!6
//Create FileStream that will write the byte array to
FileStream file =//!6
File.Create(this.txtDownloadPath.Text + "\\" + list);//!6
//Write the full byte array to the file
file.Write(fileData, 0, fileData.Length);//!6
//Download message complete
lblMessage.Text = "Download Complete!";
//Clears the textfields content
txtSiteAddress.Text = "";
txtDownloadPath.Text = "";
//Close the file so other processes can access it
file.Close();
break;
}
}
This is not an answer but too long for a comment. (I'll delete it later)
To resolve the issue it works, it doesn't work etc; a complete code, for those who may want to check
string html = #"";
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(html);
//Anirudh's Solution
var itemList = doc.DocumentNode.SelectNodes("//a//#href")//get all hrefs
.Select(p => p.InnerText)
.Where(x => x.EndsWith("exe"))
.ToList();
//returns empty list
//correct one
var itemList2 = doc.DocumentNode.SelectNodes("//a[#href]")
.Select(p => p.Attributes["href"].Value)
.Where(x => x.EndsWith("exe"))
.ToList();
//returns download/datavoila-setup.exe
Regex is not a good choice for parsing HTML files..
HTML is not strict nor is it regular with its format..
Use htmlagilitypack
You can use this code to retrieve all exe's using HtmlAgilityPack
HtmlWeb web = new HtmlWeb();
HtmlDocument doc = web.Load("http://yourWebSite.com");
var itemList = doc.DocumentNode.SelectNodes("//a[#href]")//get all hrefs
.Select(p => p.Attributes["href"].Value)
.Where(x=>x.EndsWith("exe"))
.ToList();
itemList now contain all exe's
I would use FizzlerEx, it adds jQuery like syntax to HTMLAgilityPack. Use the ends-with selector to test the href attribute:
using HtmlAgilityPack;
using Fizzler.Systems.HtmlAgilityPack;
var web = new HtmlWeb();
var document = web.Load("http://example.com/page.html")
var page = document.DocumentNode;
foreach(var item in page.QuerySelectorAll("a[href$='exe']"))
{
var file = item.Attributes["href"].Value;
}
And an explanation of why it is bad to parse HTML with RegEx: http://www.codinghorror.com/blog/2009/11/parsing-html-the-cthulhu-way.html
Instead of using regular expressions you could just use normal code.
List<string> files = new List<string>();
string[] parts = htmlText.Split(new string[]{"\""},
StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
if (part.EndsWith(".exe"))
files.Add(part);
}
In this case you would have all the found files in the files list.
EDIT:
You could do:
List<string> files = new List<string>();
string[] hrefs = htmlText.Split(new string[]{"href=\""},
StringSplitOptions.RemoveEmptyEntries);
foreach (string href in hrefs)
{
string[] possibleFile = href.Split(new string[]{"\""},
StringSplitOptions.RemoveEmptyEntries);
if (possibleFile.Length() > 0 && possibleFile[0].EndsWith(".exe"))
files.Add(possibleFile[0]);
}
This would also check that the exe file is within a href.

Categories

Resources