Feature name: Audio file playback (.wav)
What it aims to do: Enable playback of .wav files
Can you code it yourself: Yes, though it's not complex, just a function in .NET that people might not know about. I'm a bit of a noob, but I'm trying to help. :/
Example:
Code:
using System;
using System.Media;
public static void WavPlay(string FileName)
{
try //Code placed in try...catch block to catch exceptions.
{ //Note: Not all exceptions are handled by this code. Just a common one.
SoundPlayer Player = new SoundPlayer(); //Instance of SoundPlayer class
Player.SoundLocation = FileName; //Set Soundlocation to FileName.
Player.Play(); //Call the Play() function on our SoundPlayer object.
}
catch (FileNotFoundException)
{
return; //If this exception is thrown, the function simply returns.
}
}
NOTE: You can, instead of calling Play(), use PlayLooping to loop a .wav file.
EDIT: There's no need to create a new thread for playing .wav in this manner, these functions, when called, automatically create a new thread for playing the file.
Feature name: Ping() function with overloads
What it aims to do: Ping an IP address/Remote host
Can you code it yourself: It's another simple function, with added simplification and overloads for easier use. Again, I'm a noob and I just wanna help, so please give constructive criticism and don't flame and emotionally scar me. -_-
Code:
using System;
using System.Net.NetworkInformation;
public static void Ping(string AddressOrHost)
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
ping.Send(AddressOrHost, 1000);
}
catch (System.Net.NetworkInformation.PingException)
{
return;
}
}//Pings the specified IP Address or Host Name. One second timeout.
public static void Ping(string AddressOrHost, int TimeOut)
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
ping.Send(AddressOrHost, TimeOut);
}
catch (System.Net.NetworkInformation.PingException)
{
return;
}
}//Pings the specified IP Address or Host Name with the specified Timeout.
public static void Ping(string AddressOrHost, int TimeOut, byte[] PingBuffer)
{
try
{
System.Net.NetworkInformation.Ping ping = new System.Net.NetworkInformation.Ping();
ping.Send(AddressOrHost, TimeOut, PingBuffer);
}
catch (System.Net.NetworkInformation.PingException)
{
return;
}
}
NOTE: You can either use SendAsync() or start another thread and execute this function on it if you want your app to be multi-threaded.
Edit: Forgot to mention in comments, not all possible exceptions are handled, just a common one. Also, it's 3:15 A.M. and I'm a nub so please don't flame too hard if everything I post fails utterly.
Feature: Get a list of all available drives and display info on them in the console output stream.
what it aims to do: stated above.
can you code it yourself: Yes, no, maybe, probably, here it is:
Code:
using System;
using System.IO;
public static void DriveScan()
{
DriveInfo[] DriveList = System.IO.DriveInfo.GetDrives();
try
{
foreach (DriveInfo n in DriveList)
{///Returns info for every drive in the system.
if (n.IsReady == true)
{
Console.WriteLine("Drive Name: ");
Console.WriteLine(n.Name);
Console.WriteLine("Root Directory: ");
Console.WriteLine(n****otDirectory.FullName);
Console.WriteLine("File System Type/Format: ");
Console.WriteLine(n.DriveFormat);
Console.WriteLine("Total Size: ");
Console.WriteLine(n.TotalSize.ToString());
Console.WriteLine("Available Free Space: ");
Console.WriteLine(n.AvailableFreeSpace.ToString());
Console.WriteLine("Total Free Space: ");
Console.WriteLine(n.TotalFreeSpace.ToString());
Console.WriteLine("Type: ");
Console.WriteLine(n.DriveType.ToString());
Console.WriteLine("~:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:.:~");
}
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return;
}
catch (IOException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
return;
}
}
NOTE: All exceptions that the System.IO.DriveInfo.GetDrives() function can throw have been handled. Also note that this is obviously written for console programs, but the idea behind it is fairly universal and can be used in other types of apps. If this is of any use to anyone, I'm sure you'll know enough about C# to change it to suit your needs. Again, sorry for being a noob, if these noobish functions I'm posting are too nubby and shitty just tell me and I'll stop.
Feature name: CS and VB compiler
what it aims to do: compiles .cs or .vb source file to an executable or dll.(exe/dll changed in source)
can you code it yourself: No, I found this using google, so I used it to further my knowledge of C# and .NET. I'm sure someone can use this function.
Code:
using System;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using Microsoft.CSharp.RuntimeBinder;
using Microsoft.VisualBasic;
public static bool CompileExecutable(String sourceName)
{
FileInfo sourceFile = new FileInfo(sourceName);
CodeDomProvider provider = null;
bool compileOk = false;
retry:
// Select the code provider based on the input file extension.
if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".CS")
{
provider = CodeDomProvider.CreateProvider("CSharp");
}
else if (sourceFile.Extension.ToUpper(CultureInfo.InvariantCulture) == ".VB")
{
provider = CodeDomProvider.CreateProvider("VisualBasic");
}
else
{
Console.WriteLine("Source file must have a .cs or .vb extension");
Console.ReadLine();
goto retry;
}
if (provider != null)
{
// Format the executable file name.
// Build the output assembly path using the current directory
// and <source>_cs.exe or <source>_vb.exe.
String exeName = String.Format(@"{0}\{1}.exe", //change .exe to .dll
System.Environment.CurrentDirectory,
sourceFile.Name.Replace(".", "_"));
CompilerParameters cp = new CompilerParameters();
// Generate an executable instead of
// a class library.
cp.GenerateExecutable = true; //change this to false to compile .dll
// Specify the assembly file name to generate.
cp.OutputAssembly = exeName;
// Save the assembly as a physical file.
cp.GenerateInMemory = false;
// Set whether to treat all warnings as errors.
cp.TreatWarningsAsErrors = false;
// Invoke compilation of the source file.
CompilerResults cr = provider.CompileAssemblyFromFile(cp,
sourceName);
if (cr.Errors.Count > 0)
{
// Display compilation errors.
Console.WriteLine("Errors building {0} into {1}",
sourceName, cr.PathToAssembly);
foreach (CompilerError ce in cr.Errors)
{
Console.WriteLine(" {0}", ce.ToString());
Console.WriteLine();
}
}
else
{
// Display a successful compilation message.
Console.WriteLine("Source {0} built into {1} successfully.",
sourceName, cr.PathToAssembly);
}
// Return the results of the compilation.
if (cr.Errors.Count > 0)
{
compileOk = false;
}
else
{
compileOk = true;
}
}
return compileOk;
}
NOTE: change string exeName to .dll and GenerateExecutable to false to compile a class library to .dll; Also, I've found that there are methods to add references to .NET libraries, otherwise you can't use anything that needs assembly references. I'm sure someone will find a use, but still, let me know if I'm being a pest in this thread.