C# Foreach Loop

25 03 2008

ok so this is a little code snippet. Lets say I have 10 text files in a folder and I want to add my ASCII signature to the end of all of them. I can put them in an array, and use a Foreach loop to do it to all of them at once. Heres how:
public static void write(String[] files)
{
foreach (String s in files)
{
FileStream fs = new FileStream(s, FileMode.Open, FileAccess.Write);
String[] line = {" ..jjLLtt \r\n",
" GGffiiGGLL \r\n",
" iiDD ,,EE,, \r\n",
" ttGG EEii \r\n",
" ;;EE.. ;;EEtt \r\n",
" LLEEffGGEEtt \r\n",
" ;;;;..KK;; \r\n",
" ;;,, ;;KK.. ;;;; \r\n",
" ..KKDD ii,,;;EEtt KKEE \r\n",
" ..LLtt LLDDDDtt LLjj \r\n"};
for (int i = 0; i < line.Length; i++)
{
Byte[] buffer = System.Text.Encoding.ASCII.GetBytes(line[i]);
try
{
fs.Write(buffer, 0, line[i].Length);
}
catch (Exception e)
{
// Do Nothing
}
}
fs.Close();
}

Now unfortunately because it gets rid of all the tabs, you can see what it actually looks like, but you can see it here

The parameter for the write method is an array of strings. The strings in this case happen to be file names. So the method will take all the filenames into it, and for each one, open them in a filestream, and using the filestream for each one, will write the ASCII signature in the file, and then close the filestream.





Ever Have Trouble Sending Things Between Your Home Computers? … Me Too

19 01 2008

If you’re at all like me, I always have my friends bringing their computers over. We like to share stuff obviously, and although I always come up with some solution to the problem of “How do I get this file from my computer to yours?”, it’s never really the best answer, and generally involves a lot more work than it really needs to.

Last night, I was having a little LAN Party with 2 of my friends, and we were just hanging out playing all sorts of games like we do occasionally. But then of course one of my friends says, “Hey Ryan, lemme give you this file.”. Alright…too bad we don’t have a way for you to send it to me.

First try: “I’ll share a folder over the network and you can just drop it in there!” – FAILURE – Computers not in the same windows work group…

Second try: “Get on AIM, I’ll just send it to you!” – FAILURE – AIM sucks, and for whatever reason, I can never use it to send or receive files from any of the people that it matters for.

Third try: “Oh hold on I’ll put it on my USB Drive!” – FAILURE – “F***! I can’t find it. “

At this point, we are all angry. Suddenly I was struck with a thought. “Why don’t I just make my own program for LAN file transfers?”. Brilliance. Next came, “Why did it take me so long to think of that…”

So I spent a good bit of last night programming the night away like I generally do. I decided to write a simple thing in C#. Its nothing complex but it almost works. I’m having trouble making it so you can send files over about 50 MB though, which is a problem…

I get an exception on this line:


i = stream.Read(bytes, 0, bytes.Length);

It gives me an IOException and says:

Unable to read data from the transport connection: An operation on a socket could not be performed because the system lacked sufficient buffer space or because a queue was full.

I’m not really sure what to do about that. I’ve tried fiddling with a few things, but it didn’t do anything. I’m going to ask one of my friends who is a professional developer about it when i talk to him next. If anyone else has suggestions, though, I would really appreciate them.

So thats pretty much what I’ve done recently. Nothing huge, but hopefully once I get past this error it will be done. I’ll let ya know once I get it completely working. If someone would like to download it or use it, I will prolly package it and put it on one of my servers and post a link.