syrian developers community
.net
Opening and closing the CD tray in .NET
Nov 26th
Microsoft .NET Framework doesn’t provide a method that you can simply call for opening or closing the CD or DVD tray of the computer drives. However, that doesn’t mean this can’t be easily accomplished by using the mciSendString from the Windows API. The project attached to this tutorial is created in Visual Studio 2005, but the code works in Visual Studio 2003 aswell.
Start by creating a C# Windows application project. And drag two buttons to the form, btnOpen and btnClose, which will open and close the CD / DVD tray when clicked. Now switch to code view, and add the following using directive, since we’ll More >
Converting Word Documents to RTF, HTML, TXT, XML
Nov 26th
Opening and manipulating Microsoft Office Word documents (.doc) can be done rather easily using the .NET Framework. You are capable of opening, editing and creating Word documents with only a few lines of code. However, since classes for managing the Word document format are not available in the .NET Framework, the solution is to reference COM objects into your project. The downside of this is that to be able to manage the Word documents with the application we’re going to create in this tutorial, the user running it will need to have Microsoft Word installed, preferably the same version that we More >
Shut down system using C#
Nov 26th
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- // Remember to add a reference to the System.Management assembly
- using System.Management;
- namespace ShutDown
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void btnShutDown_Click(object sender, EventArgs e)
- {
- ManagementBaseObject mboShutdown = null;
- ManagementClass mcWin32 = new ManagementClass(“Win32_OperatingSystem”);
- mcWin32.Get();
- // You can’t shutdown without security privileges
- mcWin32.Scope.Options.EnablePrivileges = true;
- ManagementBaseObject mboShutdownParams = mcWin32.GetMethodParameters(“Win32Shutdown”);
- // Flag 1 means we want to shut down the system
- mboShutdownParams["Flags"] = “1″;
- mboShutdownParams["Reserved"] = “0″;
- foreach (ManagementObject manObj in mcWin32.GetInstances())
- {
- mboShutdown = manObj.InvokeMethod(“Win32Shutdown”, mboShutdownParams, null);
- }
- }
- }
- }
Capturing screenshots using C#
Nov 26th
On the web, there are a few tutorials that will show you how to make a screen capture similar to the one that’s created and stored in the clipboard when the “Print Screen” button is pressed. However, you will find that these tutorials are a bit too complicated for such a simple task. In this tutorial we’re going to accomplish the same thing those tutorials do, but with only a few lines of code, thanks to the .NET 2.0 Framework, that introduced the new CopyFromScreen() method.
Start by creating a new Windows Application project in Visual Studio 2005. Add to it a button More >
File transfers through networks and the Internet
Nov 26th
In this tutorial we’re going to build a very interesting .NET application, and based on the code here you will hopefully be able to build various applications or to help you with extending your current work.
Before digging into the code, you should know that the code you see in this tutorial and in the attached project resumes to only the strict functionality of exchanging files between two computers using the .NET Framework. If you want to integrate this into a real-life application, you will need to put more effort into this code since currently it lacks crash prevention and recovery, testing More >
Convert ASCII values from hex to characters
Nov 26th
- // An object storing the hex value
- string HexValue = “4765656B7065646961″;
- // An object storing the string value
- string StrValue = “”;
- // While there’s still something to convert in the hex string
- while (HexValue.Length > 0)
- {
- // Use ToChar() to convert each ASCII value (two hex digits) to the actual character
- StrValue += System.Convert.ToChar(System.Convert.ToUInt32(HexValue.Substring(0, 2), 16)).ToString();
- // Remove from the hex object the converted value
- HexValue = HexValue.Substring(2, HexValue.Length – 2);
- }
- // Show what we just converted
- MessageBox.Show(StrValue);
Unzipping compressed files using GZipStream
Nov 26th
In the previous tutorial entitled Zipping files using GZipStream we reviewed how to compress files using the System.IO.Compression namespace, and we also learned that the GZipStream class of this namespace has some limitations. If you haven’t read that tutorial, I suggest that you read it, and among other things you will learn that we can’t compress multiple files into a single archive and/or give them each a file name like you are used to when using popular tools such as WinRAR or WinZIP. Details on why this isn’t possible and what workaround is available are in that tutorial.
This means we More >
Play WAV files using SoundPlayer
Nov 26th
Thanks to the new SoundPlayer class of .NET 2.0, playing WAV files takes only a few lines of code. You can start by creating a new Windows application in Visual Studio 2005 and adding to the newly created form, a textbox, four buttons and an OpenFileDialog object.
Name the controls: txtPath for the TextBox and btnBrowse, btnPlay, btnPlayLoop, btnStop for the four buttons. You can name the OpenFileDialog object openWav. The form should look similar to the one below:
Now that we have the form ready, switch to its code and place the following using directive to the new System.Media namespace:
using System.Media;
One other thing we need to do More >
Creating an advanced download manager in C#
Nov 26th
This is a continuation of the tutorial entitled “Creating a download manager in C#” in which we created a Windows application that was capable of downloading files via the http:// protocol and showing the progress of retrieving the bytes from the server to the local computer. In this tutorial we’ll see how we can put additional functionality into this application, by adding code that allows the user to pause and resume a download.
If you didn’t read the previous tutorial, you should, otherwise you will not understand much of this one since we’ll be developing on top of the code explained More >
Speaking Computer in C#
Nov 26th
This is very simple to accomplish, we will be using Windows SAPI (Speech Application Program Interface) along with Microsoft
’s Visual C# 2005 Express Edition. Our application will contain a text box that the user will be able to input text into and then push a button that will tell the application
to read everything in the text box, very simple. First off, you will need to create a new Windows Project and create a textbox and a button right below it, like so. Be sure to set multiline to true when creating this.
Next, we need to add a reference to the SAPI. More >