C# Web Requests

27 03 2008

Web Requests are amazingly easy in C# – All you need is this:

WebRequest myWebRequest = WebRequest.Create("http://www.google.com");
WebResponse myWebResponse = myWebRequest.GetResponse();
Stream ReceiveStream = myWebResponse.GetResponseStream();
StreamReader readStream = new StreamReader(ReceiveStream);
String Response = readStream.ReadToEnd();

Returns everything from wherever you request. So simple. Need I explain more?





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.





Is It Really Web 2.0?

24 03 2008

http://web2.0validator.com/

I found this cool little site this morning while surfing along. Type in any site to see if they really are Web 2.0 or not! Put your site in there and see what you get!





Microsoft – It’s a Love Hate Relationship

20 03 2008

So first off, let me say, I’m pretty neutral about the different major Operating Systems. I like Windows for surfing, programming, and basic stuff.

I like Macs for their power with photo and video editing and unique and creative features.

I like linux for programming, and using as a server. It’s quite good at those things.

So now we can see some of the nice things about each. Now for most of my computers I’m running windows. (except 2 that are running ubuntu  server) The reason for this is because I’ve been doing so much programming in the last few weeks. I’m putting out about 300-400 lines of code a day.  Thats a lot. Now, at this point you probably know that I’m going to talk about the .NET framework and the Visual Studios IDE’s. I really REALLY hate the .NET framework because it is Microsoft only, just like everything else Microsoft makes. Though with programs like Mono, which I talked about two or three posts ago, I don’t really have to worry about the portability of the .NET framework. So that is one thing that makes me happy about Microsoft; It is still portable despite the fact that they don’t want to be.

Despite those things, I still like the simplicity of most of Microsoft’s programs. Microsoft stuff like Office and Visual Studios are quite nice. They hog memory, but other than that they are very nice. I love Visual studios. It makes programming in any .NET language so much easier and the form designer is probably the best thing about it. Choose the things you want, and drag and drop them. The IDE is so great also for people like me who just wan to experiment. The intellisense in Visual Studios is very nice, and makes it easy to just explore the language itself so you don’t have to go looking through man-pages to find out how to use a function, and that is so convenient. Hooray,  Microsoft made something that I really like for once :]

I still hate Microsoft’s web site. It’s impossible to navigate…





Nifty Web 2.0 Gadgets

18 03 2008

I was just surfing along the other day, and I found 2 pretty cool web site’s that I thought were very useful and rather amusing at the same time. Here’s the links, go play with them:

http://www.lightsphere.com/dev/web20.html

http://www.dotomator.com/





Cross Platform .NET

18 03 2008

Finally, something to make it easier to develop programs for all platforms. I have been so sick of being stuck using the .NET framework, only running on Windows. But Mono has come to the rescue.

Mono provides the necessary software to develop and run .NET client and server applications on Linux, Solaris, Mac OS X, Windows, and Unix. Sponsored by Novell, the Mono open source project has an active and enthusiastic contributing community and is positioned to become the leading choice for development of Linux applications.

As you can see, for any programmer using a .NET language, this is an amazing tool. This allows us to not have to think so much about which programming language is the best to use. In my case, that is especially important because it is generally more like “what programming language should I learn to enable me to make this?”





A Change Of Plans

17 03 2008

Ok so basically, as of a few days ago, Fizzure was scrapped due to liability issues. Boo~ I’m sure you’re all so sad. But to me, it’s alright. Fizzure was a great learning experience. And to make up for it, I have another project – yet to be named – that will be a huge project and hopefully spread very nicely. I’m not going to reveal it to you (mostly because we havnt named it yet, [yes, I did say "we"] ), but it’s an original idea with some twists. We’ll see what happens. Not even sure what language we are going to use for it, though it is probably going to be C# again, despite my ever growing dislike for the .NET framework.

More updates later…





Simple C Example – Average Finder

16 03 2008

int main()
{
double x = 0.0, sum = 0.0;
int count = 0;

printf( "\t--- Calculate Averages ---\n" );
printf("\nEnter some numbers:\n"
"(Type a letter to end you input)\n" );
while ( scanf("%lf", &x ) == 1)
{
sum += x;
count++;
}

if (count == 0)
{
printf("No Input Data!\n");
}
else
printf("The Average of your numbers is %.2f\n", sum/count);
return 0;
}

This is a basic loop that lets you put in as many numbers as you want to, and then a letter to get the average of all said numbers. Very simple, but shows good use of basic constructs in C





Simple Threading With C#

9 03 2008

Here is just a basic example for how to use threads.

using System;
using System.Threading; // <-- if you dont want to include the entire Threading namespace, just prefix your threading object with System.Threading.[object]
namespace ThreadingExample
{
class ThreadingExample
{
public void Main(String[] args)
{
Console.WriteLine("Starting...");
Thread myThread = new Thread(myThreadedMethod); // <-- remember, don't put the () after the method you want running on that thread!
myThread.Start();
while (true)
{
// do stuff here
}
}
public void myThreadedMethod()
{
while(true)
{
//do other stuff here too
}
}
}
}

Now, both of those while loops will be running forever simultaneously. Like i said, this is just a simple example. Threading can get extremely complicated. If you want to know about threading with a method that needs parameters, you should go look through the MSDN library on it. It has an -ok- example. and some explanation I believe.





C# Random String Generators

8 03 2008

Heres a little bit of code for making random strings. I’m sure someone will find it useful. I used to use it a lot in web development. Not sure how useful it would be for software development but use it if you’d like to :)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Ruf
{
public static class StringGnerator
{
public static Char[] charArray = { ‘-’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘p’, ‘q’, ‘r’, ’s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘1′, ‘2′, ‘3′, ‘4′, ‘5′, ‘6′, ‘7′, ‘8′, ‘9′, ‘0′, ‘_’ };

public static string RandomStr()
{
String returns = “”;
Random r = new Random();
for (int i = 0; i < 11; i++)
{
returns += “” + charArray[(int)(r.NextDouble() + 1.0 * 38.0)];
}
return returns;
}

public static string RandomStr(int Length)
{
String returns = “”;
Random r = new Random();
for (int i = 0; i < Length; i++)
{
returns += “” + charArray[(int)(r.NextDouble() + 1.0 * 38.0)];
}
return returns;
}

public static string RandomStr(int Seed, int Length)
{
String returns = “”;
Random r = new Random(Seed);
for (int i = 0; i < Length; i++)
{
returns += “” + charArray[(int)(r.NextDouble() + 1.0 * 38.0)];
}
return returns;
}

}
}