Monday, February 28, 2011
Disable Logging i shrepoint 2007
http://blogs.msdn.com/b/fooshen/archive/2007/01/31/throttling-moss-2007-logs.aspx
Wednesday, August 25, 2010
How to use Jquery in Update panels
I you have this Jquery method to be used
$(function () {
// initialize scrollable
$(".scrollable").scrollable();
});
Then use it inside a function as shown
function BindEvents() {
$(function () {
// initialize scrollable
$(".scrollable").scrollable();
});
}
Then add this script inside the update panel as shown below
<asp:UpdatePanel ID="UpdatePanel9" runat="server">
<ContentTemplate>
<script type="text/javascript">
Sys.Application.add_load(BindEvents);
</script>
//Ur Code Here
</ContentTemplate>
</asp:UpdatePanel>
$(function () {
// initialize scrollable
$(".scrollable").scrollable();
});
Then use it inside a function as shown
function BindEvents() {
$(function () {
// initialize scrollable
$(".scrollable").scrollable();
});
}
Then add this script inside the update panel as shown below
<asp:UpdatePanel ID="UpdatePanel9" runat="server">
<ContentTemplate>
<script type="text/javascript">
Sys.Application.add_load(BindEvents);
</script>
//Ur Code Here
</ContentTemplate>
</asp:UpdatePanel>
Sunday, July 25, 2010
Project cannot be opened because its project type (.csproj) is not supported by this version of the application.
To open this project, please use a version that supports this type of project.
Follow the below steps to resolve the above error.
Click Start> All Programs > Visual Studio 2008 > Visual Studio Tools > Click Visual Studio 2008 Command Prompt. Type the below command and press enter.
Click Start> All Programs > Visual Studio 2008 > Visual Studio Tools > Click Visual Studio 2008 Command Prompt. Type the below command and press enter.
"devenv.exe /resetskippkgs"
Executing this command should solve your problem.
Thursday, July 22, 2010
How to zip & extract files
You can use this ZipManager class for zipping files & unzipping.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace WebApplication1.Compress
{
public class ZipManager
{
private static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
{
ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
// find number of chars to remove // from orginal file path
TrimLength += 1; //remove '\'
FileStream ostream;
byte[] obuffer;
string outPath = inputFolderPath + @"\" + outputPathAndFile;
ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
if (password != null && password != String.Empty)
oZipStream.Password = password;
oZipStream.SetLevel(9); // maximum compression
ZipEntry oZipEntry;
foreach (string Fil in ar) // for each file, generate a zipentry
{
oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
oZipStream.PutNextEntry(oZipEntry);
if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
{
ostream = File.OpenRead(Fil);
obuffer = new byte[ostream.Length];
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
}
}
oZipStream.Finish();
oZipStream.Close();
}//code for zipping files
private static ArrayList GenerateFileList(string Dir)
{
ArrayList fils = new ArrayList();
bool Empty = true;
foreach (string file in Directory.GetFiles(Dir)) // add each file in directory
{
fils.Add(file);
Empty = false;
}
if (Empty)
{
if (Directory.GetDirectories(Dir).Length == 0)
// if directory is completely empty, add it
{
fils.Add(Dir + @"/");
}
}
foreach (string dirs in Directory.GetDirectories(Dir)) // recursive
{
foreach (object obj in GenerateFileList(dirs))
{
fils.Add(obj);
}
}
return fils; // return file list
}//to get folder files
private void UnZipFiles(Stream stream, string outputFolder)
{
//ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
ZipInputStream s = new ZipInputStream(stream);
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = outputFolder;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
string fullPath = directoryName + "\\" + theEntry.Name;
fullPath = fullPath.Replace("\\ ", "\\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
s.Close();
}
public static void ZipFiles(string inputFolderPath, string outputFolderPath,string filenameWithExtension, string password)
{
if (Directory.Exists(outputFolderPath))//if folder exists delete and recreate it
{
DirectoryInfo ifo = new DirectoryInfo(outputFolderPath);
ifo.Delete(true);
}
Directory.CreateDirectory(outputFolderPath);// create folder for lecturer
ZipFiles(outputFolderPath, filenameWithExtension, password);//zip the crearted folder
}
public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
if (password != null && password != String.Empty)
s.Password = password;
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = outputFolder;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
string fullPath = directoryName + "\\" + theEntry.Name;
fullPath = fullPath.Replace("\\ ", "\\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
s.Close();
if (deleteZipFile)
File.Delete(zipPathAndFile);
}
}
}
For Zipping you can call as shown below.
Compress.ZipManager.ZipFiles("D:/TestFiles/", "D:/TestBackup/", "Backup1.zip", "");
For unzipping
/*unzipping files*/
Compress.ZipManager.UnZipFiles("D:/TestBackup/Control_demo.zip", "D:/TestBackup/", "sahan", false);
You have to download
ICSharpCode.SharpZipLib
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;
namespace WebApplication1.Compress
{
public class ZipManager
{
private static void ZipFiles(string inputFolderPath, string outputPathAndFile, string password)
{
ArrayList ar = GenerateFileList(inputFolderPath); // generate file list
int TrimLength = (Directory.GetParent(inputFolderPath)).ToString().Length;
// find number of chars to remove // from orginal file path
TrimLength += 1; //remove '\'
FileStream ostream;
byte[] obuffer;
string outPath = inputFolderPath + @"\" + outputPathAndFile;
ZipOutputStream oZipStream = new ZipOutputStream(File.Create(outPath)); // create zip stream
if (password != null && password != String.Empty)
oZipStream.Password = password;
oZipStream.SetLevel(9); // maximum compression
ZipEntry oZipEntry;
foreach (string Fil in ar) // for each file, generate a zipentry
{
oZipEntry = new ZipEntry(Fil.Remove(0, TrimLength));
oZipStream.PutNextEntry(oZipEntry);
if (!Fil.EndsWith(@"/")) // if a file ends with '/' its a directory
{
ostream = File.OpenRead(Fil);
obuffer = new byte[ostream.Length];
ostream.Read(obuffer, 0, obuffer.Length);
oZipStream.Write(obuffer, 0, obuffer.Length);
}
}
oZipStream.Finish();
oZipStream.Close();
}//code for zipping files
private static ArrayList GenerateFileList(string Dir)
{
ArrayList fils = new ArrayList();
bool Empty = true;
foreach (string file in Directory.GetFiles(Dir)) // add each file in directory
{
fils.Add(file);
Empty = false;
}
if (Empty)
{
if (Directory.GetDirectories(Dir).Length == 0)
// if directory is completely empty, add it
{
fils.Add(Dir + @"/");
}
}
foreach (string dirs in Directory.GetDirectories(Dir)) // recursive
{
foreach (object obj in GenerateFileList(dirs))
{
fils.Add(obj);
}
}
return fils; // return file list
}//to get folder files
private void UnZipFiles(Stream stream, string outputFolder)
{
//ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
ZipInputStream s = new ZipInputStream(stream);
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = outputFolder;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
string fullPath = directoryName + "\\" + theEntry.Name;
fullPath = fullPath.Replace("\\ ", "\\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
s.Close();
}
public static void ZipFiles(string inputFolderPath, string outputFolderPath,string filenameWithExtension, string password)
{
if (Directory.Exists(outputFolderPath))//if folder exists delete and recreate it
{
DirectoryInfo ifo = new DirectoryInfo(outputFolderPath);
ifo.Delete(true);
}
Directory.CreateDirectory(outputFolderPath);// create folder for lecturer
ZipFiles(outputFolderPath, filenameWithExtension, password);//zip the crearted folder
}
public static void UnZipFiles(string zipPathAndFile, string outputFolder, string password, bool deleteZipFile)
{
ZipInputStream s = new ZipInputStream(File.OpenRead(zipPathAndFile));
if (password != null && password != String.Empty)
s.Password = password;
ZipEntry theEntry;
string tmpEntry = String.Empty;
while ((theEntry = s.GetNextEntry()) != null)
{
string directoryName = outputFolder;
string fileName = Path.GetFileName(theEntry.Name);
// create directory
if (directoryName != "")
{
Directory.CreateDirectory(directoryName);
}
if (fileName != String.Empty)
{
if (theEntry.Name.IndexOf(".ini") < 0)
{
string fullPath = directoryName + "\\" + theEntry.Name;
fullPath = fullPath.Replace("\\ ", "\\");
string fullDirPath = Path.GetDirectoryName(fullPath);
if (!Directory.Exists(fullDirPath)) Directory.CreateDirectory(fullDirPath);
FileStream streamWriter = File.Create(fullPath);
int size = 2048;
byte[] data = new byte[2048];
while (true)
{
size = s.Read(data, 0, data.Length);
if (size > 0)
{
streamWriter.Write(data, 0, size);
}
else
{
break;
}
}
streamWriter.Close();
}
}
}
s.Close();
if (deleteZipFile)
File.Delete(zipPathAndFile);
}
}
}
For Zipping you can call as shown below.
Compress.ZipManager.ZipFiles("D:/TestFiles/", "D:/TestBackup/", "Backup1.zip", "");
For unzipping
/*unzipping files*/
Compress.ZipManager.UnZipFiles("D:/TestBackup/Control_demo.zip", "D:/TestBackup/", "sahan", false);
You have to download
ICSharpCode.SharpZipLib
How to uplaod a file to a ftp server
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
namespace WebApplication1.FTP
{
public class FTPManager
{
public string FtpFilePath { get; set; }
public string FtpInputFilePath { get; set; }
public string FtpHost { get; set; }
public string FtpUserName { get; set; }
public string FtpPAssword { get; set; }
public void UploadFile(FTPManager fm)
{
//try
//{
//string ftphost = "203.143.11.98";
//here correct hostname or IP of the ftp server to be given
string ftpfullpath = "ftp://" + fm.FtpHost + fm.FtpFilePath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(fm.FtpUserName, fm.FtpPAssword);
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(fm.FtpInputFilePath);
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
try
{
// Stream to which the file to be upload is written
Stream strm = ftp.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload
// Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch
{
throw new WebException();
}
}
public void UploadFile(FTPManager fm,byte[] buffer)
{
try
{
//string ftphost = "203.143.11.98";
//here correct hostname or IP of the ftp server to be given
string ftpfullpath = "ftp://" + fm.FtpHost + fm.FtpFilePath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(fm.FtpUserName, fm.FtpPAssword);
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
//FileStream fs = File.OpenRead(fm.FtpInputFilePath);
//byte[] buffer = new byte[fs.Length];
//fs.Read(buffer, 0, buffer.Length);
//fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch
{
throw new InvalidDataException();
}
}
}
}
This is the way to use this class
FTP.FTPManager fm = new FTP.FTPManager();
fm.FtpUserName = "com";
fm.FtpPAssword = "Com123$";
fm.FtpInputFilePath = @"D:/from desktop 2010-06-08.zip";
fm.FtpFilePath = @"/TestFtp/from desktop 2010-06-08.zip";
fm.FtpHost = "203.143.11.98";
if (FileUpload1.HasFile)
{
fm.FtpFilePath = @"/TestFtp/"+FileUpload1.PostedFile.FileName;
fm.UploadFile(fm, FileUpload1.FileBytes);
}
else
fm.UploadFile(fm);
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
namespace WebApplication1.FTP
{
public class FTPManager
{
public string FtpFilePath { get; set; }
public string FtpInputFilePath { get; set; }
public string FtpHost { get; set; }
public string FtpUserName { get; set; }
public string FtpPAssword { get; set; }
public void UploadFile(FTPManager fm)
{
//try
//{
//string ftphost = "203.143.11.98";
//here correct hostname or IP of the ftp server to be given
string ftpfullpath = "ftp://" + fm.FtpHost + fm.FtpFilePath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(fm.FtpUserName, fm.FtpPAssword);
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
FileStream fs = File.OpenRead(fm.FtpInputFilePath);
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
try
{
// Stream to which the file to be upload is written
Stream strm = ftp.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Till Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload
// Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch
{
throw new WebException();
}
}
public void UploadFile(FTPManager fm,byte[] buffer)
{
try
{
//string ftphost = "203.143.11.98";
//here correct hostname or IP of the ftp server to be given
string ftpfullpath = "ftp://" + fm.FtpHost + fm.FtpFilePath;
FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
ftp.Credentials = new NetworkCredential(fm.FtpUserName, fm.FtpPAssword);
//userid and password for the ftp server to given
ftp.KeepAlive = true;
ftp.UseBinary = true;
ftp.Method = WebRequestMethods.Ftp.UploadFile;
//FileStream fs = File.OpenRead(fm.FtpInputFilePath);
//byte[] buffer = new byte[fs.Length];
//fs.Read(buffer, 0, buffer.Length);
//fs.Close();
Stream ftpstream = ftp.GetRequestStream();
ftpstream.Write(buffer, 0, buffer.Length);
ftpstream.Close();
}
catch
{
throw new InvalidDataException();
}
}
}
}
This is the way to use this class
FTP.FTPManager fm = new FTP.FTPManager();
fm.FtpUserName = "com";
fm.FtpPAssword = "Com123$";
fm.FtpInputFilePath = @"D:/from desktop 2010-06-08.zip";
fm.FtpFilePath = @"/TestFtp/from desktop 2010-06-08.zip";
fm.FtpHost = "203.143.11.98";
if (FileUpload1.HasFile)
{
fm.FtpFilePath = @"/TestFtp/"+FileUpload1.PostedFile.FileName;
fm.UploadFile(fm, FileUpload1.FileBytes);
}
else
fm.UploadFile(fm);
Tuesday, July 6, 2010
C# Class to Get MP3 Header Details
class MP3Header
{
// Public variables for storing the information about the MP3
public int intBitRate;
public string strFileName;
public long lngFileSize;
public int intFrequency;
public string strMode;
public int intLength;
public string strLengthFormatted;
// Private variables used in the process of reading in the MP3 files
private ulong bithdr;
private bool boolVBitRate;
private int intVFrames;
public bool ReadMP3Information(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
// Set the filename not including the path information
strFileName = @fs.Name;
char[] chrSeparators = new char[] { '\\', '/' };
string[] strSeparator = strFileName.Split(chrSeparators);
int intUpper = strSeparator.GetUpperBound(0);
strFileName = strSeparator[intUpper];
// Replace ' with '' for the SQL INSERT statement
strFileName = strFileName.Replace("'", "''");
// Set the file size
lngFileSize = fs.Length;
byte[] bytHeader = new byte[4];
byte[] bytVBitRate = new byte[12];
int intPos = 0;
// Keep reading 4 bytes from the header until we know for sure that in
// fact it's an MP3
do
{
fs.Position = intPos;
fs.Read(bytHeader, 0, 4);
intPos++;
LoadMP3Header(bytHeader);
}
while (!IsValidHeader() && (fs.Position != fs.Length));
// If the current file stream position is equal to the length,
// that means that we've read the entire file and it's not a valid MP3 file
if (fs.Position != fs.Length)
{
intPos += 3;
if (getVersionIndex() == 3) // MPEG Version 1
{
if (getModeIndex() == 3) // Single Channel
{
intPos += 17;
}
else
{
intPos += 32;
}
}
else // MPEG Version 2.0 or 2.5
{
if (getModeIndex() == 3) // Single Channel
{
intPos += 9;
}
else
{
intPos += 17;
}
}
// Check to see if the MP3 has a variable bitrate
fs.Position = intPos;
fs.Read(bytVBitRate, 0, 12);
boolVBitRate = LoadVBRHeader(bytVBitRate);
// Once the file's read in, then assign the properties of the file to the public variables
intBitRate = getBitrate();
intFrequency = getFrequency();
strMode = getMode();
intLength = getLengthInSeconds();
strLengthFormatted = getFormattedLength();
fs.Close();
return true;
}
return false;
}
private void LoadMP3Header(byte[] c)
{
// this thing is quite interesting, it works like the following
// c[0] = 00000011
// c[1] = 00001100
// c[2] = 00110000
// c[3] = 11000000
// the operator << means that we'll move the bits in that direction
// 00000011 << 24 = 00000011000000000000000000000000
// 00001100 << 16 = 000011000000000000000000
// 00110000 << 24 = 0011000000000000
// 11000000 = 11000000
// +_________________________________
// 00000011000011000011000011000000
bithdr = (ulong)(((c[0] & 255) << 24) | ((c[1] & 255) << 16) | ((c[2] & 255) << 8) | ((c[3] & 255)));
}
private bool LoadVBRHeader(byte[] inputheader)
{
// If it's a variable bitrate MP3, the first 4 bytes will read 'Xing'
// since they're the ones who added variable bitrate-edness to MP3s
if (inputheader[0] == 88 && inputheader[1] == 105 &&
inputheader[2] == 110 && inputheader[3] == 103)
{
int flags = (int)(((inputheader[4] & 255) << 24) | ((inputheader[5] & 255) << 16) | ((inputheader[6] & 255) << 8) | ((inputheader[7] & 255)));
if ((flags & 0x0001) == 1)
{
intVFrames = (int)(((inputheader[8] & 255) << 24) | ((inputheader[9] & 255) << 16) | ((inputheader[10] & 255) << 8) | ((inputheader[11] & 255)));
return true;
}
else
{
intVFrames = -1;
return true;
}
}
return false;
}
private bool IsValidHeader()
{
return (((getFrameSync() & 2047) == 2047) &&
((getVersionIndex() & 3) != 1) &&
((getLayerIndex() & 3) != 0) &&
((getBitrateIndex() & 15) != 0) &&
((getBitrateIndex() & 15) != 15) &&
((getFrequencyIndex() & 3) != 3) &&
((getEmphasisIndex() & 3) != 2));
}
private int getFrameSync()
{
return (int)((bithdr >> 21) & 2047);
}
private int getVersionIndex()
{
return (int)((bithdr >> 19) & 3);
}
private int getLayerIndex()
{
return (int)((bithdr >> 17) & 3);
}
private int getProtectionBit()
{
return (int)((bithdr >> 16) & 1);
}
private int getBitrateIndex()
{
return (int)((bithdr >> 12) & 15);
}
private int getFrequencyIndex()
{
return (int)((bithdr >> 10) & 3);
}
private int getPaddingBit()
{
return (int)((bithdr >> 9) & 1);
}
private int getPrivateBit()
{
return (int)((bithdr >> 8) & 1);
}
private int getModeIndex()
{
return (int)((bithdr >> 6) & 3);
}
private int getModeExtIndex()
{
return (int)((bithdr >> 4) & 3);
}
private int getCoprightBit()
{
return (int)((bithdr >> 3) & 1);
}
private int getOrginalBit()
{
return (int)((bithdr >> 2) & 1);
}
private int getEmphasisIndex()
{
return (int)(bithdr & 3);
}
private double getVersion()
{
double[] table = { 2.5, 0.0, 2.0, 1.0 };
return table[getVersionIndex()];
}
private int getLayer()
{
return (int)(4 - getLayerIndex());
}
private int getBitrate()
{
// If the file has a variable bitrate, then we return an integer average bitrate,
// otherwise, we use a lookup table to return the bitrate
if (boolVBitRate)
{
double medFrameSize = (double)lngFileSize / (double)getNumberOfFrames();
return (int)((medFrameSize * (double)getFrequency()) / (1000.0 * ((getLayerIndex() == 3) ? 12.0 : 144.0)));
}
else
{
int[, ,] table = {
{ // MPEG 2 & 2.5
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0}, // Layer III
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0}, // Layer II
{0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256,0} // Layer I
},
{ // MPEG 1
{0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,0}, // Layer III
{0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384,0}, // Layer II
{0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448,0} // Layer I
}
};
return table[getVersionIndex() & 1, getLayerIndex() - 1, getBitrateIndex()];
}
}
private int getFrequency()
{
int[,] table = {
{32000, 16000, 8000}, // MPEG 2.5
{ 0, 0, 0}, // reserved
{22050, 24000, 16000}, // MPEG 2
{44100, 48000, 32000} // MPEG 1
};
return table[getVersionIndex(), getFrequencyIndex()];
}
private string getMode()
{
switch (getModeIndex())
{
default:
return "Stereo";
case 1:
return "Joint Stereo";
case 2:
return "Dual Channel";
case 3:
return "Single Channel";
}
}
private int getLengthInSeconds()
{
// "intKilBitFileSize" made by dividing by 1000 in order to match the "Kilobits/second"
int intKiloBitFileSize = (int)((8 * lngFileSize) / 1000);
return (int)(intKiloBitFileSize / getBitrate());
}
private string getFormattedLength()
{
// Complete number of seconds
int s = getLengthInSeconds();
// Seconds to display
int ss = s % 60;
// Complete number of minutes
int m = (s - ss) / 60;
// Minutes to display
int mm = m % 60;
// Complete number of hours
int h = (m - mm) / 60;
// Make "hh:mm:ss"
return h.ToString("D2") + ":" + mm.ToString("D2") + ":" + ss.ToString("D2");
}
private int getNumberOfFrames()
{
// Again, the number of MPEG frames is dependant on whether it's a variable bitrate MP3 or not
if (!boolVBitRate)
{
double medFrameSize = (double)(((getLayerIndex() == 3) ? 12 : 144) * ((1000.0 * (float)getBitrate()) / (float)getFrequency()));
return (int)(lngFileSize / medFrameSize);
}
else
return intVFrames;
}
}
{
// Public variables for storing the information about the MP3
public int intBitRate;
public string strFileName;
public long lngFileSize;
public int intFrequency;
public string strMode;
public int intLength;
public string strLengthFormatted;
// Private variables used in the process of reading in the MP3 files
private ulong bithdr;
private bool boolVBitRate;
private int intVFrames;
public bool ReadMP3Information(string FileName)
{
FileStream fs = new FileStream(FileName, FileMode.Open, FileAccess.Read);
// Set the filename not including the path information
strFileName = @fs.Name;
char[] chrSeparators = new char[] { '\\', '/' };
string[] strSeparator = strFileName.Split(chrSeparators);
int intUpper = strSeparator.GetUpperBound(0);
strFileName = strSeparator[intUpper];
// Replace ' with '' for the SQL INSERT statement
strFileName = strFileName.Replace("'", "''");
// Set the file size
lngFileSize = fs.Length;
byte[] bytHeader = new byte[4];
byte[] bytVBitRate = new byte[12];
int intPos = 0;
// Keep reading 4 bytes from the header until we know for sure that in
// fact it's an MP3
do
{
fs.Position = intPos;
fs.Read(bytHeader, 0, 4);
intPos++;
LoadMP3Header(bytHeader);
}
while (!IsValidHeader() && (fs.Position != fs.Length));
// If the current file stream position is equal to the length,
// that means that we've read the entire file and it's not a valid MP3 file
if (fs.Position != fs.Length)
{
intPos += 3;
if (getVersionIndex() == 3) // MPEG Version 1
{
if (getModeIndex() == 3) // Single Channel
{
intPos += 17;
}
else
{
intPos += 32;
}
}
else // MPEG Version 2.0 or 2.5
{
if (getModeIndex() == 3) // Single Channel
{
intPos += 9;
}
else
{
intPos += 17;
}
}
// Check to see if the MP3 has a variable bitrate
fs.Position = intPos;
fs.Read(bytVBitRate, 0, 12);
boolVBitRate = LoadVBRHeader(bytVBitRate);
// Once the file's read in, then assign the properties of the file to the public variables
intBitRate = getBitrate();
intFrequency = getFrequency();
strMode = getMode();
intLength = getLengthInSeconds();
strLengthFormatted = getFormattedLength();
fs.Close();
return true;
}
return false;
}
private void LoadMP3Header(byte[] c)
{
// this thing is quite interesting, it works like the following
// c[0] = 00000011
// c[1] = 00001100
// c[2] = 00110000
// c[3] = 11000000
// the operator << means that we'll move the bits in that direction
// 00000011 << 24 = 00000011000000000000000000000000
// 00001100 << 16 = 000011000000000000000000
// 00110000 << 24 = 0011000000000000
// 11000000 = 11000000
// +_________________________________
// 00000011000011000011000011000000
bithdr = (ulong)(((c[0] & 255) << 24) | ((c[1] & 255) << 16) | ((c[2] & 255) << 8) | ((c[3] & 255)));
}
private bool LoadVBRHeader(byte[] inputheader)
{
// If it's a variable bitrate MP3, the first 4 bytes will read 'Xing'
// since they're the ones who added variable bitrate-edness to MP3s
if (inputheader[0] == 88 && inputheader[1] == 105 &&
inputheader[2] == 110 && inputheader[3] == 103)
{
int flags = (int)(((inputheader[4] & 255) << 24) | ((inputheader[5] & 255) << 16) | ((inputheader[6] & 255) << 8) | ((inputheader[7] & 255)));
if ((flags & 0x0001) == 1)
{
intVFrames = (int)(((inputheader[8] & 255) << 24) | ((inputheader[9] & 255) << 16) | ((inputheader[10] & 255) << 8) | ((inputheader[11] & 255)));
return true;
}
else
{
intVFrames = -1;
return true;
}
}
return false;
}
private bool IsValidHeader()
{
return (((getFrameSync() & 2047) == 2047) &&
((getVersionIndex() & 3) != 1) &&
((getLayerIndex() & 3) != 0) &&
((getBitrateIndex() & 15) != 0) &&
((getBitrateIndex() & 15) != 15) &&
((getFrequencyIndex() & 3) != 3) &&
((getEmphasisIndex() & 3) != 2));
}
private int getFrameSync()
{
return (int)((bithdr >> 21) & 2047);
}
private int getVersionIndex()
{
return (int)((bithdr >> 19) & 3);
}
private int getLayerIndex()
{
return (int)((bithdr >> 17) & 3);
}
private int getProtectionBit()
{
return (int)((bithdr >> 16) & 1);
}
private int getBitrateIndex()
{
return (int)((bithdr >> 12) & 15);
}
private int getFrequencyIndex()
{
return (int)((bithdr >> 10) & 3);
}
private int getPaddingBit()
{
return (int)((bithdr >> 9) & 1);
}
private int getPrivateBit()
{
return (int)((bithdr >> 8) & 1);
}
private int getModeIndex()
{
return (int)((bithdr >> 6) & 3);
}
private int getModeExtIndex()
{
return (int)((bithdr >> 4) & 3);
}
private int getCoprightBit()
{
return (int)((bithdr >> 3) & 1);
}
private int getOrginalBit()
{
return (int)((bithdr >> 2) & 1);
}
private int getEmphasisIndex()
{
return (int)(bithdr & 3);
}
private double getVersion()
{
double[] table = { 2.5, 0.0, 2.0, 1.0 };
return table[getVersionIndex()];
}
private int getLayer()
{
return (int)(4 - getLayerIndex());
}
private int getBitrate()
{
// If the file has a variable bitrate, then we return an integer average bitrate,
// otherwise, we use a lookup table to return the bitrate
if (boolVBitRate)
{
double medFrameSize = (double)lngFileSize / (double)getNumberOfFrames();
return (int)((medFrameSize * (double)getFrequency()) / (1000.0 * ((getLayerIndex() == 3) ? 12.0 : 144.0)));
}
else
{
int[, ,] table = {
{ // MPEG 2 & 2.5
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0}, // Layer III
{0, 8, 16, 24, 32, 40, 48, 56, 64, 80, 96,112,128,144,160,0}, // Layer II
{0, 32, 48, 56, 64, 80, 96,112,128,144,160,176,192,224,256,0} // Layer I
},
{ // MPEG 1
{0, 32, 40, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,0}, // Layer III
{0, 32, 48, 56, 64, 80, 96,112,128,160,192,224,256,320,384,0}, // Layer II
{0, 32, 64, 96,128,160,192,224,256,288,320,352,384,416,448,0} // Layer I
}
};
return table[getVersionIndex() & 1, getLayerIndex() - 1, getBitrateIndex()];
}
}
private int getFrequency()
{
int[,] table = {
{32000, 16000, 8000}, // MPEG 2.5
{ 0, 0, 0}, // reserved
{22050, 24000, 16000}, // MPEG 2
{44100, 48000, 32000} // MPEG 1
};
return table[getVersionIndex(), getFrequencyIndex()];
}
private string getMode()
{
switch (getModeIndex())
{
default:
return "Stereo";
case 1:
return "Joint Stereo";
case 2:
return "Dual Channel";
case 3:
return "Single Channel";
}
}
private int getLengthInSeconds()
{
// "intKilBitFileSize" made by dividing by 1000 in order to match the "Kilobits/second"
int intKiloBitFileSize = (int)((8 * lngFileSize) / 1000);
return (int)(intKiloBitFileSize / getBitrate());
}
private string getFormattedLength()
{
// Complete number of seconds
int s = getLengthInSeconds();
// Seconds to display
int ss = s % 60;
// Complete number of minutes
int m = (s - ss) / 60;
// Minutes to display
int mm = m % 60;
// Complete number of hours
int h = (m - mm) / 60;
// Make "hh:mm:ss"
return h.ToString("D2") + ":" + mm.ToString("D2") + ":" + ss.ToString("D2");
}
private int getNumberOfFrames()
{
// Again, the number of MPEG frames is dependant on whether it's a variable bitrate MP3 or not
if (!boolVBitRate)
{
double medFrameSize = (double)(((getLayerIndex() == 3) ? 12 : 144) * ((1000.0 * (float)getBitrate()) / (float)getFrequency()));
return (int)(lngFileSize / medFrameSize);
}
else
return intVFrames;
}
}
Subscribe to:
Posts (Atom)