|
I am working on some code to replace all of the LF end of line chars with CRLF in a text file. I wanted to account for the scenario of if the file does contain CRLF already and am not sure how. My current code will replace CRLF with CRCRLF which is not good. Does anyone have any ideas? Thanks in advance for your assistance!
string data = null;
using (StreamReader srFileName = new StreamReader(FileName))
{
data = srFileName.ReadToEnd();
data = data.Replace("\n","\r\n");
}
using (StreamWriter swFileName = new StreamWriter(FileName))
{
swFileName.Write(data);
}
|
|
|
|
|
One easy method would be:
data = data.Replace("\r\n","\n");
data = data.Replace("\n","\r\n");
But thats doing two replaces which might be a performance issue.
Another method I think should work is to use File.ReadAllLines() followed by File.WriteAllLines().
Yet another method would be to use regex to match \n's that are NOT preceeded by \r's.
|
|
|
|
|
Hi every body
I want to send email in windows application.
when i trace code all is OK but no message is received in mailbox ,my code is below:
MailAddressCollection receivers = new MailAddressCollection();
MailMessage msg = new MailMessage();
msg.IsBodyHtml = true;
msg.From = new MailAddress("telavat@telavat.com", "Alireza Doroudian");
msg.To.Add("a.doroudian@gmail.com");
msg.Subject = "testofHtml";
msg.Body = "ww";
SmtpClient smpt = new SmtpClient();
smpt.Host = "localhost";
smpt.Port = 25;
smpt.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
smpt.Send(msg);
thanks for any guides
Regards
Ali
|
|
|
|
|
My first suggestion would be to remove your email addresses from your post . You are about to getting spammed into oblivion as spammers spider this forum for addresses.
|
|
|
|
|
smpt.Host = "localhost";
Do you have an SMTP server running on your localhost machine? The smtp host target needs to be a system that is running a mail server and can transport the mail message to its destination.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Use this class to send email on windows application:
public class Email
{
public void SendEmail(string userName, string password, string mailFrom, string mailTo, string subject, string message, bool isBodyHtml, string SmtpAdd, int port)
{
MailMessage msg = new MailMessage(mailFrom, mailTo, subject, message);
msg.IsBodyHtml = isBodyHtml;
System.Net.NetworkCredential cred = new
System.Net.NetworkCredential(userName, password);
System.Net.Mail.SmtpClient mailClient = new
System.Net.Mail.SmtpClient(SmtpAdd, port);
mailClient.EnableSsl = false;
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = cred;
mailClient.Send(msg);
}
|
|
|
|
|
Hi All,
I have got stuck with some issue with export option of gridview to excel sheet. Kindly help me is sloving the porblem. Attachments are not coming in the excel exported.
Thank you.
Here is code
public void Export(string fileName, GridView gv)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
HttpContext.Current.Response.ContentType = "application/ms-excel";
using (StringWriter sw = new StringWriter())
{
using (HtmlTextWriter htw = new HtmlTextWriter(sw))
{
Table table = new Table();
table.GridLines = GridLines.Both;
if (gv.HeaderRow != null)
{
PrepareControlForExport(gv.HeaderRow);
table.Rows.Add(getheader());
table.Rows.Add(gv.HeaderRow);
table.Rows[0].BackColor = System.Drawing.Color.FromArgb(37, 96, 133);
}
foreach (GridViewRow row in gv.Rows)
{
PrepareControlForExport(row);
table.Rows.Add(row);
}
if (gv.FooterRow != null)
{
PrepareControlForExport(gv.FooterRow);
table.Rows.Add(gv.FooterRow);
}
table.RenderControl(htw);
HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End();
}
}
}
private static void PrepareControlForExport(Control control)
{
for (int i = 0; i < control.Controls.Count; i++)
{
Control current = control.Controls[i];
if (current is LinkButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
}
else if (current is ImageButton)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
}
else if (current is HyperLink)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
}
else if (current is DropDownList)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
}
else if (current is CheckBox)
{
control.Controls.Remove(current);
control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
}
if (current.HasControls())
{
PrepareControlForExport(current);
}
}
}
|
|
|
|
|
Hello,
I am developing an all which requires me to update the screen rather often. At the moment it is set to 1 second, I can change it, but i wouldn't want to move it much past 5 seconds.
At the moment I have a BackgroundWorker doing the TableAdapter.Fill(), and then I am clearing the DataSet and Merging it for the final outcome. However, this is slow enough to block the UI, making things very jerky.
I tried moving the clear and Merge to the background thread, however when I did that it throws errors when you try to interact with the DataGridView whilst it's updating.
Any ideas?
Thanks,
Brendan
|
|
|
|
|
Not possible to have a real time app polling a database, sorry . Rethink your design. I don't even know what your app does, but I'm guessing its not changing the entire table every second. Rethink your approach. You will likely need to write an IOCP based UDP server to be able to keep up with real time updates on multiple clients and stop sending entire tables to the client. Its not necessary. You just need to send new data.
|
|
|
|
|
Basically my app is a monitoring application for status codes on alarms. The reason I need fast refreshes is that the application needs to see when the other operators do things - i.e. acknowledge a status code, etc.
Ahh, bugger. I was hoping the apps could talk directly to the SQL server itself. I'll look into your suggestion though! Thanks!
|
|
|
|
|
I think there is such a thing as wiring up events between sql server and c#. I have never pursued this info as I find the concept horrifying!
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Yes, SQL is able to call code or COM objects, etc. It is NOT intended to be used as a push mechanism to multiple clients. Thats even stated in the docs. Its intended to be used as a notification mechanism for a single server process. More efficient then hitting up the database every second, but he's still going to need a server piece in which case he might as well "do it right" .
|
|
|
|
|
I have never actually read the docs, not something I would recommend to anyone, live updates of data changes from the server yech!
If you were running WCF could that act as the choke point monitoring all changes rather than the TCIP suggestion you put forward?
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Nah, WCF sucks. I will never use it again. It is easy to implement though. We have a couple of WCF components in my current project (not my idea!). WCF is slow (too much XML traffic) and has no push mechanism. I don't think WCF would scale too well either. The only thing that scales into 10's of thousands of connections per server is going to be TCP/IP (or UDP) using IOCPs. Not sure if the OP has to go that far though .
|
|
|
|
|
Gotcha. Yeah, I don't think polling the database server constantly is going to work very well with multiple clients . Since you probably don't want missed updates, I'd probably go with TCP/IP vs. UDP. Although UDP wouldn't necessarily be a bad thing here. TCP/IP is probably going to be easier to implement a reliable system with though. Your design would probably be that everybody connects to the TCP/IP IOCP server through a simple text or binary protocol you invent. When somebody updates something, you push the update out to all the clients. The TCP/IP server would be the only process writing stuff to the database in the background.
|
|
|
|
|
I am just wondering if there is a pattern for programming conditions which are not known by the programmer at programming time; i.e. the conditions are set by the user.
For example, the user may define a set IF conditions, and corresponding actions. If the IFs were finite, that would be easy. However, the user has a variable set of conditions/rules. A good example of this is a ERP-purchasing module application. The routing of a purchase order depends on approval level. The levels of approval are determined by the company policy. There could be any number of approval levels.
In other word
IF condition 1... then action 1
IF condition 2... then action 2
..
IF Condition n.... then action n
but N is unknown at programming time. So I can't use the the typical if-else block.
Any suggestions on how to handle this.
|
|
|
|
|
Is this the sort of thing you mean? User Defined Dynamic Workflows in Workflow Foundation 4?[^]
I'm almost certain that I've seen an article posted here about creating 1 to n dynamic workflows - I'll leave you to search for it.
I would also think it possibel to do this by giving the users a blotter on which they can describe a workflow which is stored in a database. You then create forms based on the steps they have stored. That was off the top of my head so feel free to poke hole sin it. The idea, not my head...
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
If I understood correctly you may use "not" !. I mean may be you shouldn't check all possibilities for user input. For example instead of checking this
string userInput; //Got this from user input sould be "c"
if(userInput == "a")
{}
else if(userInput == "b")
{}
else if(userInput == "c")
{//Correct}
You can use
if(userInput == "c")
{//Correct}
else
{}
|
|
|
|
|
I find myself wondering how this question could possibly reflect a real-world scenario.
The fact that there are "any number of approval levels" does not justify an assumption that the possible conditions are "infinite:" there is probably a fixed number of maximum approvals possible, or required. And, probably, a certain minimum of approvals required to even process the order.
That the set of all possible permutations of approvals would require a unique action for each permutation: also seems a dubious assumption.
And, isn't there some "hierarchy" involved here: a decision by upper-management to cancel/void/dis-approve takes precedence over other lower-level decision makers, which means you can deduce the action required without consideration of certain other levels ?
In the real world, "hierarchy" also "kicks-in," in financial transactions, dependent upon customer credit, and $ize of order, as well as customer history, or internal (or external credit agency) rating.
My guess is there is a finite, and well-defined, set of actions to be taken based on approvals of purchase order.
So, present an interface with CheckBoxes: logical analysis of which are Checked then can be mapped to a list of appropriate actions. Or, present a TreeView with CheckBoxes, where you handle "hierarchy" by activating, or de-activting, sub-nodes, depending on whether "parent" nodes are checked. Then iterate the TreeView (recursively), and generate a list of actions based on all Checked Nodes.
"The first principle is that you must not fool yourself, and you are the easiest person to fool." Richard Feynman
|
|
|
|
|
The answer to this depends on how flexible the user input and required response is supposed to be. It can go from simply holding a list of allowed values to embedding a scripting engine. To give any useful guidance we'll need to know more about what the user is expecting to be able to enter as conditions and actions.
|
|
|
|
|
SilimSayo wrote: I am just wondering if there is a pattern for programming conditions which are
not known by the programmer at programming time; i.e. the conditions are set by
the user.
That is not specific enough. There are a number of ways of dealing with that of which the specifics matter which way to implement it.
SilimSayo wrote: For example, the user may define a set IF conditions, and corresponding actions
As expressed that requires an interpreter idiom (which is a pattern.)
SilimSayo wrote: but N is unknown at programming time. So I can't use the the typical if-else
block.
That of course has nothing to do with the problem as you expressed it. If you have defined conditions/actions and ALL the user does is pick from that set then the solution is trivial - use a set with an appropriate definition block. This is a very, very simple form of interpreter.
However that doesn't solve it if the conditions/actions themselves are user defined.
|
|
|
|
|
I would consider a collection of Func<bool>, Action pairs. then simply foreach the collection and if the Func<bool> returns true invoke the Action.
building the collection is another matter and would really depend on what conditions you want the user to be able to define
Pedis ex oris
Quidquid latine dictum sit, altum sonatur
|
|
|
|
|
Please help! do I need to synchronize mysql to Access database. preferably via xml. I managed to get mysql database structure:
Table name="data">
<Field name="id" type="int(11)" null="NO" key="PRI" default="" autoincrement="false"/>
<Field name="data" type="varchar(255)" null="NO" key="" default="" autoincrement="false"/>
<Field name="data2" type="varchar(255)" null="NO" key="" default="" autoincrement="false"/>
</Table>
I do not know how to write the database access.When I need to add a column in MySQL to create and access databases, without losing the original data.I also needed to synchronize data that has changed. many thanks for your help
|
|
|
|
|
What do you mean "write the database access?"
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
MS Access does not read XML. You can create a MYSQL connection in ODBC, and you can link MS Access to any ODBC data source i.e. use linked tables. Linked tables don't store data so if you make a change in the data in Access, you would be changing data in MYSQL and vice-versa.
|
|
|
|