Skip to main content

Posts

Showing posts from September, 2012

Set timer using java script

//Java Script <script language="javascript" type="text/javascript"> window.setTimeout("HideDiv()", 15000); //Hide Div after 15 seconds function HideDiv() { document.getElementById('Layer2').style.display = "none"; } </script> // HTML <div id="Layer2" > Sample Text Here </div>

How blink a text on IE Browser.

//Java Script <script type="text/javascript"> function doBlink() { var blink = document.all.tags("BLINK") for (var i = 0; i < blink.length; i++) blink .style.visibility = blink .style.visibility == "" ? "hidden" : "" } function startBlink() { if (document.all) setInterval("doBlink()", 500) } window.onload = startBlink; </script> //CSS <style type="text/css"> blink { -webkit-animation-name: blink; -webkit-animation-iteration-count: infinite; -webkit-animation-timing-function: cubic-bezier(1.0,0,0,1.0); -webkit-animation-duration: 1s; } </style> //HTML <span style="color: Red; font-size: 11px; font-family: Times New Roman;"> <blink>New!</b

Delete All Files (*.*) [C#]

This examples shows how to delete all files (*.*) from a folder in C#. First, you need to get the list of file names from the specified directory (using static method Directory.Get­Files . Then delete all files from the list. Delete all files [C#] using System.IO; string [] filePaths = Directory .GetFiles( @"c:\MyDir\" ); foreach ( string filePath in filePaths) File .Delete(filePath); Delete all files (one-row example) To delete all files using one code line, you can use Array.ForEach with combination of anonymous method. [C#] Array .ForEach( Directory .GetFiles( @"c:\MyDir\" ), delegate ( string path) { File .Delete(path); });

FileStream Read File [C#]

This example shows how to safely read file using FileStream in C# . To be sure the whole file is correctly read, you should call FileStream.Read method in a loop, even if in the most cases the whole file is read in a single call of FileStream.Read method. Read file using FileStream First create FileStream to open a file for reading. Then call FileStream.Read in a loop until the whole file is read. Finally close the stream. [C#] using System.IO; public static byte [] ReadFile ( string filePath) { byte [] buffer; FileStream fileStream = new FileStream (filePath, FileMode .Open, FileAccess .Read); try { int length = ( int )fileStream.Length; // get file length buffer = new byte [length]; // create buffer int count; // actual number of bytes read int sum = 0; // total number of bytes read // read until Read method returns 0 (end of the stream has been reached) while

Load Text from File to String [C#]

This example shows how to load text file to string variable. StreamReader To load text from file to string you can use StreamReader. Re­adToEnd method. First create new instance of StreamReader . As a parameter in constructor you can pass string with file path or Stream instance. Default encoding is UTF-8. [C#] using System.IO; StreamReader streamReader = new StreamReader (filePath); string text = streamReader. ReadToEnd() ; streamReader.Close();

Open File With Associated Application [C#]

This example demonstrates how to open file with an associated program . It shows, how to open text document in notepad, how to open image in a default viewer or how to open url address in a default web browser. Applications are launched using Process.Start method. The file path or url is passed as a parameter. [C#] // open text file in notepad (or another default text editor) System.Diagnostics. Process .Start( @"c:\textfile.txt" ); [C#] // open image in default viewer System.Diagnostics. Process .Start( @"c:\image.jpg" ); [C#] // open url in default web browser System.Diagnostics. Process .Start( "http://www.csharp-examples.net" ); [C#] // open PDF file System.Diagnostics. Process .Start( @"c:\document.pdf" ); Similarly you can open Word document or any other file from your .NET application

FileStream Open File [C#]

This example shows how to open files for reading or writing, how to load and save files using FileStream in C#. To open file create instance of FileStream class with FileMode and FileAccess enumerations as parameters. FileStream typical use This is typical code when opening file using FileStream. It's important to always close the stream. If you don't close the stream it can take a minute to be file again accessible (it will wait to garbage collector to free the FileStream instance and close the file). [C#] using System.IO; FileStream fileStream = new FileStream ( @"c:\file.txt" , FileMode .Open); try { // read from file or write to file } finally { fileStream.Close(); } Open file examples Following examples show the most common cases how to open a file for reading or writing or how to create a file. [C#] Open existing file for read and write. FileStream fileStream = new FileStream ( @"c:\file.txt" , FileMode .Open); [C#

Get Files from Directory [C#]

This example shows how to get list of file names from a directory (including subdirectories). You can filter the list by specific extension. To get file names from the specified directory, use static method Directory.Get­Files . Lets have these files and subfolders in „c:\MyDir“ folder: Get files from directory Method Directory.GetFiles returns string array with files names (full paths). [C#] using System.IO; string [] filePaths = Directory .GetFiles ( @"c:\MyDir\" ); // returns: // "c:\MyDir\my-car.BMP" // "c:\MyDir\my-house.jpg" Get files from directory (with specified extension) You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. „*.bmp“ to select files with the extension or „a*“ to select files beginning with letter „a“. [C#] string [] filePaths = Directory .GetFiles( @"c:\MyDir\" , "*.bmp" ); // returns: // "c:\MyDir\my-car.BMP" Get files from director

Verify a Check Box Using JavaScript

It is possible to see if a check box has been checked or not in JavaScript. This may come in handy during form validation. A check box has a checked property, which returns either a 0 for not checked and a 1 for checked. You can also set the property to 0 or 1 to programmatically control the check box. <html> <head> <script language=javascript> function validate(chk){ if (chk.checked == 1) alert("Thank You"); else alert("You didn't check it! Let me check it for you.") chk.checked = 1; } </script> </head> <body> <form> <input type=checkbox name=chk1>Please Check Me <p><input type=button value="check" onclick="return validate(chk1);"> </form> </body> </html>