|
Yes, you can. It's not that hard.
Normally, this would be a console application and you just use the StandardInput stream like it was a text file. You can get the StandardInput stream by calling Console.OpenStandardInput(). You can send the text file to the application with a simple command line:
C:\>myapp.exe < someTextFile.txt
Simple.
using System;
using System.IO;
namespace StandardInputSnadbox
{
class Program
{
static void Main(string[] args)
{
using (StreamReader inStream = new StreamReader(Console.OpenStandardInput()))
{
while (!inStream.EndOfStream)
{
string inLine = inStream.ReadLine();
Console.WriteLine(inLine);
}
}
}
}
}
|
|
|
|
|
|
a
modified 10-Jul-14 13:22pm.
|
|
|
|
|
I don't see any question here.
Just a code dump...
A positive attitude may not solve every problem, but it will annoy enough people to be worth the effort.
|
|
|
|
|
As has been mentioned, there doesn't appear to be any question here - and in the absence of any other explanation, I can only assume you are proud of the code and want to share this with the rest of us.
We have an area for that, but there are rules: posting code with no explanation is not allowed - we need to know what they are, what they do, and how they do it. We also need to know how these are better than the alternatives, and why we would want to use them. A nicely packaged download helps as well.
There are two ways to post such info: as a Tip or an Article.
A Tip is single task focussed; an Article is much more in depth.
This is probably Tip material as it is hard to see how you would get a reasonable article out of stuff that (for the most part) has been covered numerous times.
To try writing this up as a Tip (or an article, but it's less likely to get published) start here: http://www.codeproject.com/script/Articles/Submit.aspx[^]
When you are happy (and it can take a long time to get it right) you can submit it for publication, at which point it enters moderation and is reviewed for quality and acceptability before either being rejected or accepted, either way you will probably get comments on ways to improve it.
But this forum is not the right place!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I have one file xml with one header and n body
<Header>
<C></C>
<D></D>
</Header>
<Body>
<F></F>
</Body>
<Body>
<G></G>
</Body>
<Body>
<H></H>
</Body>
I must create n new file xml with and one For example:
First.xml
<Header>
<B>
<C></C>
<D></D>
</B>
</Header>
<Body>
<F></F>
</Body>
Second.xml
<Header>
<B>
<C></C>
<D></D>
</B>
</Header>
<Body>
<G></G>
</Body>
Third.xml
<Header>
<B>
<C></C>
<D></D>
</B>
</Header>
<Body>
<H></H>
</Body>
Initially I tried to create new n documents equal to the original. After I take First.xml and I keep the first block Body and delete the other Body With Second.xml I keep the second block of the Body and delete the other I also accept any other suggestion.
Thanks
modified 9-Jul-14 5:06am.
|
|
|
|
|
You just need to create a new file for each <Body> element in the original. Since each has the same header section it should be fairly easy to figure it out. Assuming you are using the XMLDocument[^] class in all cases.
|
|
|
|
|
Linq to XML[^] makes this fairly simple:
XDocument document = XDocument.Parse(@"<root>
<Header>
<B>
<C></C>
<D></D>
</B>
</Header>
<Body>
<F></F>
</Body>
<Body>
<G></G>
</Body>
<Body>
<H></H>
</Body>
</root>");
XName rootName = document.Root.Name;
XDeclaration declaration = document.Declaration;
XElement header = document.Root.Element("Header");
IEnumerable<XDocument> newDocuments = document.Root.Elements("Body")
.Select(body => new XDocument(
declaration,
new XElement(rootName,
header,
body
)
));
foreach (XDocument newDocument in newDocuments)
{
Console.WriteLine(newDocument);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
i'am want to encrypt and decrypt text in textboxt.text in second form but what to adding the code, this is the code. thanks
Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
namespace Rc5_v2
{
static class Program
{
/// summary
/// The main entry point for the application.
/// summary
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Main_form());
}
}
}
RC5.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Net.Mail;
using System.Net.Mime;
using System.Text.RegularExpressions;
using System.Web;
namespace Rc5_v2
{
class Rc5
{
uint[] s;
uint[] l;
uint b, u, t, c;
byte[] key;
int rounds;
public Rc5()
{
string str = "ModifyRC5";
key = GetKeyFromString(str);
rounds = 16;
b = (uint)key.Length;
u = 4;
t = (uint)(34);
c = 12 / u;
s = new uint[34];
l = new uint[12];
GenerateKey(key, rounds);
}
public Rc5(string password,int round)
{
key = GetKeyFromString(password);
rounds = round;
b = (uint)key.Length;
u = 4;
t = (uint)(2 * rounds + 2);
c = Math.Max(b, 1) / u;
s = new uint[2 * rounds + 2];
l = new uint[key.Length];
GenerateKey(key, rounds);
}
public Rc5(byte[] password, int round)
{
rounds = round;
key = password;
b = (uint)password.Length;
u = 4;
t = (uint)(2 * rounds + 2);
c = Math.Max(b, 1) / u;
s = new uint[2 * rounds + 2];
l = new uint[password.Length];
GenerateKey(key, rounds);
}
//to circulate int left
private uint leftRotate(uint x, int offset)
{
uint t1, t2;
t1 = x &gt;&gt; (32 - offset);
t2 = x &lt;&lt; offset;
return t1 | t2;
}
//to circulate int right
private uint RightRotate(uint x, int offset)
{
uint t1, t2;
t1 = x &lt;&lt; (32 - offset);
t2 = x &gt;&gt; offset;
return t1 | t2;
}
//encryption operation on two block
private void Encode(ref uint r1, ref uint r2, int rounds)
{
r1 = r1 + s[0];
r2 = r2 + s[1];
for (int i = 1; i &lt;= rounds; i++)
{
r1 = leftRotate(r1 ^ r2, (int)r2) + s[2 * i];
r2 = leftRotate(r2 ^ r1, (int)r1) + s[2 * i + 1];
}
}
//decryption operation on two block
private void Decode(ref uint r1, ref uint r2, int rounds)
{
for (int i = rounds; i &gt;= 1; i--)
{
r2 = (RightRotate(r2 - s[2 * i + 1], (int)r1)) ^ r1;
r1 = (RightRotate(r1 - s[2 * i], (int)r2)) ^ r2;
}
r2 = r2 - s[1];
r1 = r1 - s[0];
}
private void GenerateKey(byte[] key, int rounds)
{
uint P32 = uint.Parse("b7e15163", System.Globalization.NumberStyles.HexNumber);
uint Q32 = uint.Parse("9e3779b9", System.Globalization.NumberStyles.HexNumber);
for (int i = key.Length - 1; i &gt;= 0; i--)
{
l[i] = leftRotate((uint)i, 8) + key[i];
}
s[0] = P32;
for (int i = 1; i &lt;= t - 1; i++)
{
s[i] = s[i - 1] + Q32;
}
uint ii, jj;
ii = jj = 0;
uint x, y;
x = y = 0;
uint v = 3 * Math.Max(t, c);
//mixing key arrayes
for (int counter = 0; counter &lt;= v; counter++)
{
x = s[ii] = leftRotate((s[ii] + x + y), 3);
y = l[jj] = leftRotate((l[jj] + x + y), (int)(x + y));
ii = (ii + 1) % t;
jj = (jj + 1) % c;
}
}
//convert key from string to byte array
private byte[] GetKeyFromString(string str)
{
char[] mykeyinchar = str.ToCharArray();
byte[] mykeyinbytes = new byte[mykeyinchar.Length];
for (int i = 0; i &lt; mykeyinchar.Length; i++)
{
mykeyinbytes[i] = (byte)mykeyinchar[i];
}
return mykeyinbytes;
}
public void Encrypt(FileStream streamreader,FileStream streamwriter)
{
uint r1, r2;
System.IO.BinaryReader br = new System.IO.BinaryReader(streamreader);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
long filelength = streamreader.Length;
while (filelength &gt; 0)
{
try
{
r1 = br.ReadUInt32();
try
{
r2 = br.ReadUInt32();
}
catch
{
r2 = 0;
}
}
catch
{
r1 = r2 = 0;
}
Encode(ref r1, ref r2, rounds);
bw.Write(r1);
bw.Write(r2);
filelength -= 8;
}
streamreader.Close();
streamwriter.Close();
}
public void Decrypt(FileStream streamreader,FileStream streamwriter)
{
uint r1, r2;
System.IO.BinaryReader br = new System.IO.BinaryReader(streamreader);
System.IO.BinaryWriter bw = new System.IO.BinaryWriter(streamwriter);
long filelength = streamreader.Length;
while (filelength &gt; 0)
{
try
{
r1 = br.ReadUInt32();
r2 = br.ReadUInt32();
Decode(ref r1, ref r2, rounds);
if (!(r1 == 0 &amp;&amp; r2 == 0 &amp;&amp; (filelength - 8 &lt;= 0)))
{
bw.Write(r1);
bw.Write(r2);
}
if (r2 == 0 &amp;&amp; (filelength - 8 &lt;= 0))
{
bw.Write(r1);
}
filelength -= 8;
}
catch
{
System.Windows.Forms.MessageBox.Show("May be U try to decrypt an normal file (plain file)", "Error", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return;
}
}
streamreader.Close();
streamwriter.Close();
}
}
}
Form4.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
namespace Rc5_v2
{
public partial class Form4 : Form
{
SmtpClient obj_SMTPClient;
MailMessage Obj_MailMsg;
Attachment obj_Attachment;
//int rounds01;
//string fileName01;
//string saveFileName01;
//string password01;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
if (open.ShowDialog() == DialogResult.OK)
{
label3.Text = open.FileName;
}
}
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("Please require, column must be content !!!");
}
else
try
{
obj_SMTPClient = new SmtpClient("smtp.gmail.com");
Obj_MailMsg = new MailMessage();
obj_Attachment = new System.Net.Mail.Attachment(label3.Text);
Obj_MailMsg.From = new MailAddress(textBox1.Text);
Obj_MailMsg.To.Add(textBox3.Text);
Obj_MailMsg.Body = textBox5.Text;
Obj_MailMsg.Attachments.Add(obj_Attachment);
Obj_MailMsg.Subject = textBox4.Text;
SmtpClient smtps = new SmtpClient("smtp.gmail.com", 587);
obj_SMTPClient.Credentials = new NetworkCredential();
obj_SMTPClient.Port = 587;
obj_SMTPClient.Credentials = new System.Net.NetworkCredential(textBox1.Text, textBox2.Text);
obj_SMTPClient.EnableSsl = true;
obj_SMTPClient.Send(Obj_MailMsg);
obj_SMTPClient.EnableSsl = true;
obj_SMTPClient.Send(Obj_MailMsg);
MessageBox.Show("Message Successful!!!");
}
catch
{
MessageBox.Show("Request TimeOut!!!");
}
}
private void button4_Click(object sender, EventArgs e)
{
foreach (Control sayre in this.Controls)
{
if (sayre is TextBox)
{
(sayre as TextBox).Clear();
}
}
}
private void button8_Click(object sender, EventArgs e)
{
foreach (Control sayre in this.Controls)
{
if (sayre is TextBox)
{
(sayre as TextBox).Clear();
}
}
}
private void Encrypt_Click(object sender, EventArgs e)
{
}
private void Decrypt_Click(object sender, EventArgs e)
{
}
}
}
modified 9-Jul-14 20:03pm.
|
|
|
|
|
I am not wading through a large, unformatted pile of "code" looking for what you want me to help you with.
Edit your question, format the code using <pre> tags to preserve the formatting, and remove anything I don't need to help with your problem. Then, explain exactly what problem you have, and what help you need - remembering that we can't see your screen, access your HDD, or read your mind.
Help us to help you!
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Yes and ofcourse but i'am want to encrypt and decrypt some string in textboxt not other. thanks
|
|
|
|
|
...but you couldn't be bothered to read what I wrote and actually do anything?
Why, pray, would I want to help you if you can't be bothered to help me to do it?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
thanks for the answer, i will try in my own
|
|
|
|
|
Does the code throw an error?
If you're wondering why you cannot decrypt using this code after closing the application, the answer would be that it generated a new key.
What kind of secret are you trying to encrypt? If it is a password, than you're introducing a new problem by encrypting it. Even if only the server knows how to decrypt - as it would mean that you can decrypt the users password. That's a security-issue; keep in mind that people often re-use their passwords.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
1. Why does C# require the "new" keyword? why bother?? it's not like C++ where you need the functionality of memory allocation and calling the constructor
2. why does C# require the "return" keyword with a "get" function. What else would you use a get function for? besides returning data?? seems like an extra step where "get" is built in to the language
3. When CLR executes your code is the .net "engine" accessing kernel, user and or gdi.dll ? or does it have some other method of communicating with windows os kernel functions?
4. Does C# have an equivalent to the Doc/View architecture for windows apps?
thanks
|
|
|
|
|
1. shiftwik wrote: you need the functionality of memory allocation and calling the constructor It's an OO language. That means one would be creating objects.
2. shiftwik wrote: why does C# require the "return" keyword with a "get" function ..because that's how functions MUST return their value. The getter is merely function, and the last statement of such is always a return-statement.
3. shiftwik wrote: When CLR executes your code is the .net "engine" accessing kernel The WinAPI. So, yes.
4. shiftwik wrote: Does C# have an equivalent to the Doc/View architecture for windows apps? I'm too lazy to Google for Doc/View
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
thanks Eddy, great reply!!!
you must be a college professor
Can anybody here help me with some intelligent in depth answers??
|
|
|
|
|
1) For exactly the same reason as in C++.
2) If you don't like it, try VB.
You'll never get very far if all you do is follow instructions.
|
|
|
|
|
1. Because the new keyword tells the application to instantiate an object at that point. If we have several different constructors on an object, new is a great way to tell the application that you expect a certain constructor to be invoked at that point. Merely referencing the object wouldn't be a great place to instantiate it because you could instantiate it long before you need it - and this would play hell doing things like building class factories.
2. Get is synctatic sugar that hides the fact that you are actually calling a special method with the prefix get_, so your code has to follow the standards of other methods. Another point to consider is, how would you indicate what you are returning? It's quite common to have some form of lazy initialisation or return choice in the getter, so you have to have some functionality in there to indicate what's going to be returned. Of course, if you don't actually need to do anything, just create an automatic property and you don't have to write a return statement (although one is implicitly created behind the scenes).
3. Yes. The .NET runtime and .NET applications are still PE Format applications.
4. That's just a pattern. While there's no default implementation of it in the framework, it's incredibly trivial to create although, as anyone who's worked with DocView will attest, the DocView pattern is severely limited in what it can do. There are a number of superior patterns available to use, such as MVC.
|
|
|
|
|
Pete O'Hanlon wrote: There are a number of superior patterns available to use, such as MVC.
Would that be the Model View Controller pattern you're referring to there? I didn't realize that was applicable to anything but web applications, but I'll be happy to try a different pattern with C++.
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
I was using MVC back in the late 90s with MFC - yes, for desktop apps. Ah, those were some fun days.
|
|
|
|
|
|
1) Because if it didn't it wouldn't know when you wanted to create a new instance:
MyClass[] data = new MyClass[10];
MyClass instance;
for (int i = 0; i < 10; i++)
{
data = instance;
} How many different instances of MyClass should that produce? Zero? One? Ten? Or Eleven?
MyClass[] data = new MyClass[10];
MyClass instance;
for (int i = 0; i < 10; i++)
{
instance = new MyClass();
data = instance;
} Makes that abundantly clear.
2) See previous answers, particularly Pete's
3) Yes. Again, see Pete's answer.
4) No, thank goodness. D/V was good in it's time, but thankfully things have moved on a lot since those primitive days.
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
thanks ... I guess my question on "new" is why isn't it just automatic?
with C# it call delete for you. So why not new also?
would you ever not use it?
|
|
|
|
|
Simply because C# doesn't try to "guess" what you wanted.
Think about it: I don't want the system creating a new instance every time I try to use an existing one, because it means a trip to the DB and back to create an item that I may not use again - and the "real" DB item then doesn't get updated.
I want the system to create an instance only when I specifically tell it to.
If the system created them for me, then
MyClass[] data = MyClass[10];
Would create an array of references to MyClass instances and the instances to fill it with.
I may not want that: if MyClass always contains an enormous Bitmap (for example) that is a huge amount of time and memory being wasted, because I'm about to fill the array with the top ten instances from the DB when I call the method below it:
MyClass[] data = MyClass[10];
DAL.GetImageData(data, 10, "SEARCH CONDITION"); But the system can't know that because it doesn't have any idea what the method does - it's in a separate DLL!
The new keyword allow you to specify exactly when you want an instance created (and reminds you that this could take some time and / or resources when it does it)
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|