How to get duration of an audio file in c# ?

We can get the duration of an audio or video file by using the “Shell32” namespace in Visual Studio Dot Net. For that we need to add the reference of “Interop.Shell32.dll” in to our project. And follow the given method in below. By calling the following method with the parameter of full path of file name which might be an audio file or video file we can get the duration of that file.

private string GetDuration(string FileFullPath)
{
string duration = "";
string fName = FileFullPath.Substring(FileFullPath.LastIndexOf("\\") + 1);
string filePath = FileFullPath.Substring(0, FileFullPath.LastIndexOf("\\"));
Shell32.Shell shell = new Shell32.ShellClass();
Shell32.Folder folder = shell.NameSpace(filePath);
Shell32.FolderItem folderItem = folder.ParseName(fName);
if (folderItem != null)
{
duration = folder.GetDetailsOf(folderItem, 21);
}
folderItem = null;
folder = null;
shell = null;
return duration;
}

Hence we can get the duration of given file.

…S.VinothkumaR.

Find Age from given DateOfBirth using C#

Hi all,

Just copy the following method in your class and call it with the parameters of date of birth and current date in DateTime format.

public string FindAge(DateTime dob, DateTime currentDate)
{
int years = currentDate.Year - dob.Year;
int months = 0;
int days = 0;
if(currentDate < dob.AddYears(Years) && Years != 0)
{
--years;
}
dob = dob.AddYears(years);
if (dob.Year == currentDate.Year)
{
months = currentDate.Month - dob.Month;
}
else
{
months = (12 - dob.Month) + currentDate.Month;
}
if(currentDate < dob.AddMonths(months) && months != 0)
{
--months;
}
dob = dob.AddMonths(months);
days = (currentDate - dob).Days;
return years + " years " + months + " months " + days + " days";
}

Copy the above code in your class...and call the method..enjoy... ;)

...S.VinothkumaR.

Code for moving the form by mouse clicking.

Hi Viewers,

Here by I am trying to give you a sample code for moving the win form by clicking the mouse.

Just use the following code and test it.

using System.Runtime.InteropServices;

public const int WM_NCLBUTTONDOWN = 0xA1;

public const int HT_CAPTION = 0x2;

[DllImportAttribute("user32.dll")]
public static extern int SendMessage(IntPtr hWnd,int Msg, int wParam, int lParam);

[DllImportAttribute("user32.dll")]
public static extern bool ReleaseCapture();

private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
ReleaseCapture();
SendMessage(Handle, WM_NCLBUTTONDOWN, HT_CAPTION, 0);
}
}


Now you can implement the above code in your application and test it. Working fine for me

...S.VinothkumaR.

Add prerequisites of dotnet framework in visual studio setup project

When we developing an application using any platform we should prepare our application to be deployed properly. Specially for dotnet, the prerequisites of dotnet framework is must. So here by I am explaining how to add framework to be installed before our application installation process.

After developing our .Net application we need to create a setup project. Just follow the steps mentioned in below.

- Go to File menu, select Add New Project and then select Setup And Deployment in under “Other Project Types”.

- Give a name to your project as your wish.

- Then select your setup project solution and add Primary output of your developed project. You just need to right click your setup project in solution explorer, click add --> Project Output --> select your project.

- Then add custom action by right clicking setup project --> view --> Custom Actions --> right click Custom Actions --> Add Custom Actions.

- Now we are going to add the prerequisites for your setup project.

- Select your setup project right click and go to properties then click the Prerequisites button.

- Now select .Net Framework 2.0 tick box after selecting the “Create setup program to install prerequisite components”.

- Then specify the install location for prerequisites by selecting the option box.

- Here I am selecting the second one “Download prerequisites from the same location as my application”.

- Click ok and apply then ok.

- Now build your setup project, the dotnet framework will be added in your debug folder.

That's it...

...S.VinothkumaR.

FileSystemWatcher in C#

Hi all,

Here is the simple description to use FileSystemWatcher in your desktop application by using C#.

Just drag the FileSystemWatcher control in your form from under the Components tab in the toolbox.

And copy the following code in to load event,

private void Form1_Load(object sender, EventArgs e)
{
fileSystemWatcher.Path = flvPath;
fileSystemWatcher.Filter = "*.flv";
fileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.CreationTime | NotifyFilters.LastWrite;
fileSystemWatcher.EnableRaisingEvents = true;
}

If the file watcher found any newly created file, the following event will rise.

private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e)
{
MessageBox.Show(e.Name + " created in " + e.FullPath);
}


An event is rising when a file deleted.

private void fileSystemWatcher_Deleted(object sender, FileSystemEventArgs e)
{
MessageBox.Show(e.Name + " changed in " + e.FullPath);
}

An event is rising when a file renamed.


private void fileSystemWatcher_Renamed(object sender, RenamedEventArgs e)
{
MessageBox.Show(e.Name + " renamed in " + e.FullPath);
}

That’s it…

...S.VinothkumaR.

How to generate an xml from TreeView using C#?

Hi guys,

Here is the sample code for generating xml file from TreeView. I needed this for one of my project. I just referred lot of sites and finally came to conclusion with the following code. Just copy and paste this code and use it for yours.

using System.IO;

private static StreamWriter sr;

private void btnExportXML_Click(object sender, EventArgs e)
{
if (tvXML.Nodes.Count > 0) // Here tvXML is TreeView control
{
sr = new StreamWriter(path); // Here path is xml path
sr.Write("<" + tvXML.Nodes[0].Text + ">");
parseNode(tvXML.Nodes[0]);
sr.Close();
}
}

//Use the following method for getting value of each and every node

private static void parseNode(TreeNode tn)
{
IEnumerator ie = tn.Nodes.GetEnumerator();
string parentnode = "";
parentnode = tn.Text;
while (ie.MoveNext())
{
TreeNode ctn = (TreeNode)ie.Current;
if (ctn.GetNodeCount(true) == 0)
{
if (ctn.Tag != null)
{
sr.Write("<" + ctn.Text + ">");
sr.Write(ctn.Tag);
sr.Write("");
}
else
{
sr.Write("<" + ctn.Text + "/>");
}
}
else
{
sr.Write("<" + ctn.Text + ">");
}
if (ctn.GetNodeCount(true) > 0)
{
parseNode(ctn);
}
}
sr.Write("");
sr.WriteLine("");
}

...S.VinothkumaR.

How to bind Countries in a combobox using c#?

Find the code below to bind countries in a combo box using C#.


public void BindCountries()
{
foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.AllCultures & ~CultureTypes.NeutralCultures))
{
RegionInfo ri = new RegionInfo(ci.LCID);
comboBox1.Items.Add(ri.EnglishName);
}
comboBox1.Sorted = true;
comboBox1.SelectedIndex = 0;
}

...S.VinothkumaR.