RSS

Monthly Archives: July 2010

Changing CSS file dynamically using javascript

When there will be a requirement to change the web page layout on the fly with different colors and backgrounds than following is a PERFECT solution for that.

You need to follow the three steps to implement the same.

1). Add .CSS file that is mentioned below.

2). Add “changeCSS” function in .aspx/html file which is mentioned below.

3). The “changeCSS” will take FOUR arguments as below:

1. The name of the style sheet.

2. The name of the class that you want to edit // in below case it is “.nishant”

3. The element of class, of which you want to modify the value // in below case it is “color”

4. The value of the element // in below case it is “green”

Following is a  HTML code:

<head runat=”server”>
<title></title>
<link href=”style/style.css” rel=”stylesheet” type=”text/css” media=”all” /> // style sheet is given below

<script language=”javascript”>
function ChangeColor() {
changeCSS(document.styleSheets[0], ‘.nishant’, ‘color’, ‘green’);
}

function changeCSS(theCSS, theClass, element, value) {
//Last Updated on July 28, 2010
//documentation for this script at
//http://www.shawnolson.net/a/503/altering-css-class-attributes-with-javascript.html
var cssRules;

var added = false;

if (theCSS[‘rules’]) {
cssRules = ‘rules’;
} else if (theCSS[‘cssRules’]) {
cssRules = ‘cssRules’;
} else {
//no rules found… browser unknown
}

for (var R = 0; R < theCSS[cssRules].length; R++) {
if (theCSS[cssRules][R].selectorText == theClass) {
if (theCSS[cssRules][R].style[element]) {
theCSS[cssRules][R].style[element] = value;
added = true;
break;
}
}
}
if (!added) {
if (theCSS.insertRule) {
theCSS.insertRule(theClass + ‘ { ‘ + element + ‘: ‘ + value + ‘; }’, theCSS[cssRules].length);
} else if (theCSS.addRule) {
theCSS.addRule(theClass, element + ‘: ‘ + value + ‘;’);
}
}
}

</script>

</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<input type=”button” onclick=”ChangeColor();” title=”Click” /> // function calling
<p>
nishant dave</p> // this value will be changed
</div>
</form>
</body>
</html>

Following is .CSS file

@charset “utf-8”;

.nishant
{
padding-right: 10px;
color:Red;
font-size:large;
font-family:Verdana;
}

Happy Programming!

 
8 Comments

Posted by on July 28, 2010 in Javascript

 

Tags: , , , , ,

Get modified file/folders using C#.net

If there is a requirement for which you need to collect the files and folders which are modified in last 24 or 48 hours than the following two functions will give you the solution:

// Code starts

TimeSpan t = new TimeSpan(24, 0, 0); // you need to define the hour here
DateTime dtTargetDate = DateTime.Now.Subtract(t);

sourceDirectory: From which you want to collect latest modified files and folders.

targetDirectory: Where you want latest file/folder to be pasted.

dtTargetDate : The function will return you files/folders which modified date is greater than dtTargetDate

public static void CopyLastetDirectory(string sourceDirectory, string targetDirectory, DateTime dtTargetDate)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
try
{
CopyAllLastestFiles(diSource, diTarget, dtTargetDate);
}
catch (Exception ex)
{
throw ex;
}
finally
{
diSource = null;
diTarget = null;
}
}

public static void CopyAllLastestFiles(DirectoryInfo source, DirectoryInfo target, DateTime dtTargetDate)
{
// Check if the target directory exists, if not, create it.
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}

// Copy each file into it’s new directory.
foreach (FileInfo fi in source.GetFiles())
{
//Console.WriteLine(@”Copying {0}\{1}”, target.FullName, fi.Name);
if (fi.LastWriteTime.Date >= dtTargetDate.Date && !fi.Name.Contains(“Web.config”))
fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);
}

// Copy each subdirectory using recursion.
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
// Folder names that you want to omit
if (diSourceSubDir.Name.Contains(“.svn”) || diSourceSubDir.Name.Contains(“Vagaro.BackupService”))
continue;
DirectoryInfo nextTargetSubDir =
target.CreateSubdirectory(diSourceSubDir.Name);
CopyAllLastestFiles(diSourceSubDir, nextTargetSubDir, dtTargetDate);
nextTargetSubDir = null;
}
}

That is it.

Happy Programming ! 🙂

 
Leave a comment

Posted by on July 20, 2010 in System.IO

 

Tags: , , , ,

Unzip/zip Folder/File Programmatically using C#.Net

You can achieve  Zip/Unzip functionality by following only two steps:

Step 1: Download ICSharpCode.SharpZipLib.Zip DLL following link

http://www.icsharpcode.net/opensource/sharpziplib/download.aspx

Step 2: Add following class in your project. Now you are good to go with zip/unzip functionality.

using System;
using System.Collections;
using System.IO;
using ICSharpCode.SharpZipLib.Zip;

/*
* Zip library in C#
* Author: Nishant Dave
* Date  : 23/02/2010
* capability to zip/unzip entire folders and sub folders including files up to 9 LEVELS.
*/
namespace Vagaro.Web.Business
{
public static class ZipUtils
{
public 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);
ostream.Close();
ostream.Dispose();
}
oZipEntry = null;
}
oZipStream.Finish();
oZipStream.Close();
oZipStream.Dispose();
ar = null;
}

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
}

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();
streamWriter.Dispose();
}
}
}
s.Close();
s.Dispose();
theEntry = null;
if (deleteZipFile)
File.Delete(zipPathAndFile);
}
}
}

HAPPY PROGRAMMING!!! 🙂

 
Leave a comment

Posted by on July 2, 2010 in Zip/Unzip

 

Tags: , , ,