Sai Stuff to Developers

September 14, 2007

How to get the available RAM and the CPU usage?

Filed under: DotNet — tosaik @ 7:00 am
Tags:

These methods are very usefull in order to monitor the system and particulary the amount of the available RAM in MB (MegaBytes) and the cpu usage in percents.

protected PerformanceCounter cpuCounter;
protected PerformanceCounter ramCounter;

cpuCounter = new PerformanceCounter();

cpuCounter.CategoryName = “Processor”;
cpuCounter.CounterName = “% Processor Time”;
cpuCounter.InstanceName = “_Total”;

ramCounter = new PerformanceCounter(“Memory”, “Available MBytes”);

/*
Call this method every time you need to know
the current cpu usage.
*/

public string getCurrentCpuUsage(){
            cpuCounter.NextValue()+”%”;
}

/*
Call this method every time you need to get
the amount of the available RAM in Mb
*/
public string getAvailableRAM(){
            ramCounter.NextValue()+”Mb”;
}

THis code sample shows how you can allow visitors to download a file from the webpage without exposing the path or hyperlink of the file.

Filed under: DotNet — tosaik @ 6:53 am
Tags:

Do you want to record your voice from Microphone? If yes, you can use the Microsoft APIs to solve this issue. It’s a very simple approach to record your voice from Mic. I provided C#.net code to solve this issue.

1. Open C#.net web applications. And added the blow namespace.

using Microsoft.VisualBasic.Devices;
using Microsoft.VisualBasic;
using System.Runtime.InteropServices;

2. Add the below API.
[DllImport(“winmm.dll”, EntryPoint = “mciSendStringA”, CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
private static extern int mciSendString(string lpstrCommand, string lpstrReturnString, int uReturnLength, int hwndCallback);

3. Create three Buttons and given the below name and text for the buttons.

1. Record
2. SaveStop
3. Read

1. Under Record Button Click paste the below Code:

// record from microphone
mciSendString(“open new Type waveaudio Alias recsound”, “”, 0, 0);
mciSendString(“record recsound”, “”, 0, 0);

2. Under Save / Stop button Click,

// stop and save
mciSendString(“save recsound c:\\record.wav”, “”, 0, 0);
mciSendString(“close recsound “, “”, 0, 0);
Computer c = new Computer();
c.Audio.Stop();

3. Under Read Button Click

Computer computer = new Computer();
computer.Audio.Play(“c:\\record.wav”, AudioPlayMode.Background);

Save and Execute it.

Download file from webpage without showing file URL

Filed under: DotNet — tosaik @ 6:50 am
Tags:

THis code sample shows how you can allow visitors to download a file from the webpage without exposing the path or hyperlink of the file.

if (filename != “”)
{
          string path = Server.MapPath(filename);
          System.IO.FileInfo file = new System.IO.FileInfo(path);
          if (file.Exists)
          {
                   Response.Clear();
                   Response.AddHeader(“Content-Disposition”, “attachment; filename=” + file.Name);
                   Response.AddHeader(“Content-Length”, file.Length.ToString());
                   Response.ContentType = “application/octet-stream”;
                   Response.WriteFile(file.FullName);
                   Response.End();
          }
          else
          {
                   Response.Write(“This file does not exist.”);
          }
}

The Evolution Of LINQ And Its Impact On The Design Of C#

Filed under: DotNet — tosaik @ 6:43 am
Tags:

I was a huge fan of the Connections series, hosted by James Burke, when it aired on the Discovery Channel. Its basic premise: how seemingly unrelated discoveries influenced other discoveries, which ultimately led to some modern-day convenience. The moral, if you will, is that no advancement is made in isolation. Not surprisingly, the same is true for Language Integrated Query (LINQ).

In simple terms, LINQ is a series of language extensions that supports data querying in a type-safe way; it will be released with the next version Visual Studio, code-named “Orcas.” The data to be queried can take the form of XML (LINQ to XML), databases (LINQ-enabled ADO.NET, which includes LINQ to SQL, LINQ to Dataset and LINQ to Entities), objects (LINQ to Objects), and so on. The LINQ architecture is shown in Figure 1.

Figure 1 LINQ Architecture
Figure 1 LINQ Architecture (Click the image for a smaller view)

Figure 1 LINQ Architecture
Figure 1 LINQ Architecture (Click the image for a larger view)

Advantages of Object Oriented Programming.

Filed under: OOPS Languages — tosaik @ 6:37 am
Tags:

These are some of the major advantages of OOP.  

  • Simplicity: software objects model real world objects, so the complexity is reduced and the program structure is very clear.

 

  • Modularity: each object forms a separate entity whose internal workings are decoupled from other parts of the system.

  

  • Modifiability: it is easy to make minor changes in the data representation or the procedures in an OO program. Changes inside a class do not affect any other part of a program, since the only public interface that the external world has to a class is through the use of methods.

 

  • Extensibility: adding new features or responding to changing operating environments can be solved by introducing a few new objects and modifying some existing ones.

  

  • Maintainability: objects can be maintained separately, making locating and fixing problems easier.

 

  • Re-usability: objects can be reused in different programs.

 

Polymorphism:

Filed under: OOPS Languages — tosaik @ 6:36 am
Tags:

Polymorphism means having many forms. Polymorphism can be seen frequently in the English language. There are many English words, which have a different meaning depending on the context of use. The statements “close a book”, “close the file”, “close the door” and “close the deal” all make use of the verb ‘to close’ but the meaning of each statement depends on the context. Another example is the sentence, “I’ve cracked the exam”. The meaning of cracked in this case is different from the crack used in a sentence like, “The pot cracked”. In both sentences the word is the same but its interpretation varies depending on the context. In the same way you can think of many programming examples. For instance, consider the + operator. When it is used on numbers it will act as an addition operator, adding two numbers mathematically and giving the result. When the + acts on two strings, the result will be the concatenation of the two strings.

Data Encapsulation:

Filed under: OOPS Languages — tosaik @ 6:35 am
Tags:

Data encapsulation/ data hiding is an important feature of object oriented programming. The mechanism of hiding data is to put them in a class and make them private. The data is now hidden and safe from any accidental manipulations, i.e. no function (from outside the class) can change the member data. Actually there are two things you can hide: implementation of functions (to the user it doesn’t matter as to how you’ve implemented a particular function) and data. In procedural programming it is possible to only hide the implementation details but you cannot hide/ protect data. OOP lets you achieve this. A simple example is the case of the ‘car’ (Explained in Data Abstraction, go through the next question). We don’t want the user to directly access ‘speed’ and modify it. By making ‘speed’ private, we prevent the user from doing this.

1. What is the difference between Object Oriented Programming and Procedural Programming?

Filed under: Uncategorized — tosaik @ 6:34 am
Tags:

Procedural programming creates a step by step program that guides the application through a sequence of instructions. Each instruction is executed in order. Procedural programming also focuses on the idea that all algorithms are executed with functions and data that the programmer has access to and is able to change. Object-Oriented programming is much more similar to the way the real world works; it is analogous to the human brain. Each program is made up of many entities called objects. Objects become the fundamental units and have behavior, or a specific purpose, associated with them. Objects cannot directly access another object’s data. Instead, a message must be sent requesting the data, just like people must ask one another for information; we cannot see inside each other’s heads. Benefits of Object-Oriented programming include: 

  • ability to simulate real-world event much more effectively
  • code is reusable thus less code may have to be written
  • data becomes active
  • better able to create GUI (graphical user interface) applications
  • programmers are able to reach their goals faster
  • Programmers are able to produce faster, more accurate and better-written applications (in the case of a veteran programmer, by a factor of as much as 20 times compared with a procedural program).

Blog at WordPress.com.

%d bloggers like this: