Showing posts with label writing. Show all posts
Showing posts with label writing. Show all posts

Thursday, 31 January 2013

Writing Data to Registry using JavaScript



Root Key Name
Abbreviation
HKEY_CURRENT_USER
HKCU
HKEY_LOCAL_MACHINE
HKLM
HKEY_CLASSES_ROOT
HKCR
HKEY_USERS
HKEY_USERS
HKEY_CURRENT_CONFIG
HKEY_CURRENT_CONFIG

Type
Description
In the form of
REG_SZ
A string
A string
REG_DWORD
A number
An integer
REG_BINARY
A binary value
An integer
REG_EXPAND_SZ
An expandable string (e.g., “%windir%\\calc.exe”)
A string

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Writing Data to Registry File</title>
        <script type="text/javascript">
            function writeValue() {
                var WshShell = new ActiveXObject("WScript.Shell");
                WshShell.RegWrite("HKCU\\Software\\ColdSoft Technologies\\W2SS", "Some Value", "REG_SZ");
                document.writeln("Value Changed <br />");
                var value = WshShell.RegRead("HKCU\\Software\\ColdSoft Technologies\\W2SS");
                document.writeln("New Value is \"" + value + "\"");
            }
        </script>
    </head>
    <body onload="javascript: writeValue()">

    </body>
</html>

Note: This code only works in Internet Explorer.

Wednesday, 30 January 2013

Writing Data to CSV File using JavaScript


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
    <head>
        <title>Writing Data to CSV</title>
        <script type="text/javascript">
            function addToCSVFile() {
                var csvData = new Array();  // To collect the names
                var csvFilePath = "F:\\Data.csv"; // File name

                // Collecting the names
                csvData[0] = document.getElementById('firstName').value;
                csvData[1] = document.getElementById('middleName').value;
                csvData[2] = document.getElementById('lastName').value;

                var fso = new ActiveXObject('Scripting.FileSystemObject');
                var oStream = fso.OpenTextFile(csvFilePath, 8, true, 0);
                oStream.WriteLine(csvData.join(','));
                oStream.Close();
                clearData();
                alert("Data Added Successfully");
            }

            function clearData() {
                document.getElementById('firstName').value = "";
                document.getElementById('middleName').value = "";
                document.getElementById('lastName').value = "";
            }
        </script>
    </head>
    <body>
        <p>
            First Name: <input type="text" id="firstName" />
            <br />
            Middle Name: <input type="text" id="middleName" />
            <br />
            Last Name: <input type="text" id="lastName" />
            <br />
            <input type="button" id="addButton" value="Add to CSV File" onClick="javascript: addToCSVFile()" />
        </p>
    </body>
</html>

Note: This code only works in Internet Explorer.



Writing Data to CSV File using C#



Step 1: Create New Project
Step 2: Add “System.IO” namespace.
Step 3: Create the form as follows.
Step 4: Write the following code.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WritingCSV
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            string FirstName = textBoxFirstName.Text;
            string LastName = textBoxLastName.Text;
            string Age = textBoxAge.Text;
            listBoxFirstName.Items.Add(FirstName);
            listBoxLastName.Items.Add(LastName);
            listBoxAge.Items.Add(Age);
            textBoxFirstName.ResetText();
            textBoxLastName.ResetText();
            textBoxAge.ResetText();
        }

        private void buttonWrite_Click(object sender, EventArgs e)
        {
            string FilePath = textBoxFilePath.Text;
            string[] FirstNames = new string[listBoxFirstName.Items.Count];
            string[] LastNames = new string[listBoxFirstName.Items.Count];
            string[] Ages = new string[listBoxFirstName.Items.Count];
            string[] TotalData = new string[listBoxFirstName.Items.Count];
            for (int i = 0; i < listBoxFirstName.Items.Count; i++)
            {
                FirstNames[i] = listBoxFirstName.Items[i].ToString();
                LastNames[i] = listBoxLastName.Items[i].ToString();
                Ages[i] = listBoxAge.Items[i].ToString();
                TotalData[i] = FirstNames[i] + "," + LastNames[i] + "," + Ages[i];
            }
            File.WriteAllLines(FilePath, TotalData);
            MessageBox.Show("CSV File Created Successfully", "Success");
        }
    }
}

Step 5: Output