I have some code that is currently doing the following at the end of the code:
Console.WriteLine(vid.totalCurrentCharges + " : " + vid.assetId + " : " + vid.componentName + " : " + vid.recurringCharge);
Console.readline().
I need to take the output of vid.totalCurrentCharges + " : " + vid.assetId + " : " + vid.componentName + " : " + vid.recurringCharge, and write It to a csv file. Can someone here help with this? Currently all it is doing is writing the output to console.
Instead of Console.Writeline you need to create StreamWriter. Just like this:
using (StreamWriter writer = new StreamWriter("important.csv"))
{
//probably a loop here
writer.WriteLine(vid.totalCurrentCharges + " , " + vid.assetId + " , " + vid.componentName + " , " + vid.recurringCharge);
}
Moreover you can use String.Join to concatenate strings instead of "adding" them, so above WriteLine can be converted into:
writer.WriteLine(String.Join(",", vid.totalCurrentCharges, vid.assetId, vid.componentName, vid.recurringCharge));
Related
I am having a problem where the user enters his inputs into a text box (like a name). If the name is Jim Bob it will output like Jim System.Windows.Form.Label Bob System.Windows.Form.Label. I searched the properties tab and I am following along from the book but they do not have the same problem I have. I added some code below but I don't think it has anything to do with the problem.
string output;
output = textBoxPrefT.Text + ". " + textBoxFirstN + " " + textBoxMiddleN + " " + textBoxLastN;
labelOutput.Text = output;
You have to include .Text for all textboxes so it should look like this
string output;
output = textBoxPrefT.Text + ". " + textBoxFirstN.Text + " " + textBoxMiddleN.Text + " " + textBoxLastN.Text;
labelOutput.Text = output;
I have the following code example (C# using Newtonsoft Json.NET) that serializes a Json string into a JObject:
var obj = JObject.Parse("{\"F01\" : \"f01\", " +
"\"F02\" : \"f02\", " +
"\"L01\" : [" +
"{" +
"\"L01F01\" : \"l01f01\", " +
"\"L01F02\" : \"l01f02\"" +
"}, " +
"{" +
"\"L01F01\" : \"l01f01\", " +
"\"L01F02\" : \"l01f02\"" +
"}, " +
"{" +
"\"L01F03\" : \"l01f03\" " +
"}, " +
"{" +
"\"L01F03\" : \"l01f03\", " +
"\"L01L02\" : [" +
"{" +
"\"L01L02F01\" : \"l01l02f01\"" +
"}" +
"]" +
"}" +
"]" +
"}");
Notice that the JSON has a dynamic structure where not all items in arrays has same names and attributes.
And I make the following JTokens:
var test01 = obj["F01"];
var test02 = obj["L01"][0]["L01F01"];
var test03 = obj["L01"][3]["L01L02"][0]["L01L02F01"];
I would like to know if somebody know any way to obtain the "path" of the test variables in this a way similar to:
GetPathOf(test01) => "F01"
GetPathOf(test02) => "L01[0].L01F01"
GetPathOf(test03) => "L01[3].L01L02[0].L01L02F01"
Thanks!
A Path property was added to JToken in release 5.0.1 (April 7, 2013) which does exactly what you describe.
So in your example you could simply write:
string path03 = test03.Path;
The path03 variable would then contain L01[3].L01L02[0].L01L02F01.
Fiddle: https://dotnetfiddle.net/5Dhc4i
i currently have a textbox field, everytime a change is made, i add who it was updated by and what time
right now it keep appending that text
how can i find the line that says "LastEdited: ", and only replace the time stamp value?
this is what i do now
maybe someone can post me example of code how to grab the timestamp and replace it?
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.AppendText("LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
i am not an expert, so an example of code would bevery useful
i do use this textbox to save other notes as well (which i wouldn't wanna loose)
can i do this
foreach (var line in txtMemberNotes.Lines) {
if (line.StartsWith("Last Edited: "))
{
txtMemberNotes.Text = txtMemberNotes.Text.Replace(line, "Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
else
{
txtMemberNotes.AppendText("Last Edited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine);
}
}
The easiest way to solve your problem seems to stop appending text and just set the text value. No parsing is necessary, just overwrite the exiting values with your newer value.
if (txtMemberNotesOriginally != txtMemberNotesChanged) {
txtMemberNotes.Text = "LastEdited: " + DateTime.Now.ToString() + " By: " + MyProgramName.Username + Environment.NewLine;
I guess theres always Regex if you really wanted:
string s = #"Some notes here etc etc..
Last edited: 12/12/2012 12:00:00 AM";
Console.Write(Regex.Replace(s, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString()));
Or more specifically using your code:
txtMemberNotes.Text = Regex.Replace(txtMemberNotes.Text, #"Last edited: .*$", "Lasted edited: " + DateTime.Now.ToString());
How do I get file name only by using Save File Dialog?
MessageBox.Show("File was created with name: " + SOME CODE HERE +
Environment.NewLine + Environment.NewLine +
"You can find it at: " + Environment.NewLine + sfdNewFile.FileName);
See
System.IO.Path.GetFileName;
You can call it with the parameter sfdNewFile.FileName
Use:
System.IO.Path.GetFileName(sfdNewFile.FileName);
Example:
MessageBox.Show("File was created with name: " + SOME CODE HERE +
Environment.NewLine + Environment.NewLine +
"You can find it at: " + Environment.NewLine + System.IO.Path.GetFileName(sfdNewFile.FileName));
I have the following bits of code:
public static void WriteHTML(string cFile, List<Movie> mList)
{
int lineID = 0;
string strMovie, strGenre, tmpGenre = null;
// initiates streamwriter for catalog output file
FileStream fs = new FileStream(cFile, FileMode.Create);
StreamWriter catalog = new StreamWriter(fs);
string strHeader = "<style type=\"text/css\">\r\n" + "<!--\r\n" + "tr#odd {\r\n" + " background-color:#e2e2e2;\r\n" + " vertical-align:top;\r\n" + "}\r\n" + "\r\n" + "tr#even {\r\n" + " vertical-align:top;\r\n" + "}\r\n" + "div#title {\r\n" + " font-size:16px;\r\n" + " font-weight:bold;\r\n" + "}\r\n" + "\r\n" + "div#mpaa {\r\n" + " font-size:10px;\r\n" + "}\r\n" + "\r\n" + "div#genre {\r\n" + " font-size:12px;\r\n" + " font-style:italic;\r\n" + "}\r\n" + "\r\n" + "div#plot {\r\n" + " height: 63px;\r\n" + " font-size:12px;\r\n" + " overflow:hidden;\r\n" + "}\r\n" + "-->\r\n" + "</style>\r\n" + "\r\n" + "<html>\r\n" + " <body>\r\n" + " <table>\r\n";
catalog.WriteLine(strHeader);
foreach (Movie m in mList)
{
strMovie = lineID == 0 ? " <tr id=\"odd\" style=\"page-break-inside:avoid\">" : " <tr id=\"even\" style=\"page-break-inside:avoid\">";
catalog.WriteLine(strMovie);
foreach (string genre in m.Genres)
tmpGenre += ", " + genre;
try
{ strGenre = tmpGenre.Substring(2); }
catch (Exception)
{ strGenre = null; }
strMovie = " <td>\r\n" + " <img src=\".\\images\\" + m.ImageFile + "\" width=\"75\" height=\"110\">\r\n" + " </td>\r\n" + " <td>\r\n" + " <div id=\"title\">" + m.Title + "</div>\r\n" + " <div id=\"mpaa\">" + m.Certification + " " + m.MPAA + "</div>\r\n" + " <div id=\"genre\">" + strGenre + "</div>\r\n" + " <div id=\"plot\">" + m.Plot + "</div>\r\n" + " </td>\r\n" + " </tr>\r\n";
catalog.WriteLine(strMovie);
lineID = lineID == 0 ? 1 : 0;
}
catalog.WriteLine(" </table>\r\n" + " </body>\r\n" + "</html>");
catalog.Close();
}
public static void WritePDF(string cFile, string pdfFile)
{
// Sets up PDF to write to
EO.Pdf.HtmlToPdf.Options.PageSize = new SizeF(8.5f, 11f);
EO.Pdf.HtmlToPdf.Options.OutputArea = new RectangleF(0.5f, .25f, 7.5f, 10.25f);
HtmlToPdf.ConvertUrl(cFile, pdfFile);
}
My HTML file writes fine, but when it tried to convert the HTML file to PDF I get an exception that it times out.
I did a test previously, and had it convert the code (not the file) within the WriteHTML function and it worked great. I have confirmed that the cFile exists and is a valid file (created previously in WriteHTML). The path to pdfFile is valid, and the documentation does not state the file needs to already exist (.ConvertHTML did not need an existing file).
Only thing I can think of is that the catalog.html file isn't released and ready to read yet. I made sure I closed it in the WriteHTML function. How can I test that the file is ready to be read?
Tried setting .MaxLoadWaitTime = 120000 with no luck.
Any clues would be greatly appreciated!
After a battery of further testing, and scouring the EO support forums, it appears to be a limitation of the free version of EO. It seems to have difficulty with HTML files over 3MB.
It's a shame since the EO product is very good, but not unfortunately not worth $250 IMO.