Rabu, 30 November 2011

OpenXML Word #2 :: Membuat dokumen Word sederhana dengan OpenXML

Hello guys,
Untuk membuat Word dengan Open XML, sebelumnya instal-lah OpenXML SDK ny terlebih dahulu..

Bagi yg ingin menginstall OpenXML SDK ini, dapat di dapat di :
\\storage\binus\Syahdan\IT\upload\OpenXML\OpenXMLSDKv2.msi
atau jika ternyata installer yg saya upload ternyata sudah dihapus, dapat di download di sini :
http://www.microsoft.com/download/en/details.aspx?id=5124

Setelah terinstall, kopi DocumentFormat.OpenXml.dll yg berada di lokasi file tempat install :
C:\Program Files\Open XML SDK\V2.0\lib\
atau
[path install]\Open XML SDK\V2.0\lib\

dan Paste lah dll tersebut ke dalam Folder Bin aplikasi anda..

Setelah itu buka aplikasi anda,
dan klik kanan Reference, untuk menambahkan Reference baru..
Browse lah k file DocumentFormat.OpenXml.dll tersebut..
Jangan lupa menambahkan WindowsBase.dll



Buat lah sebuah tombol di aplikasi anda..
Lalu di fungsi OnClick nya, kita masukkan kodingan kita

Pertama kita siapkan nama file yg akan dibuat, beserta Path tempat file akan disimpan terlebih dahulu..
//Masukkan Path folder yang ingin di-save & nama File
            String SaveFolderPath = Server.MapPath("~") + "Temp\\";
String namaFile = "WordTest.docx";


Lalu kita buat MainDocument nya..
WordprocessingDocument document = 
                WordprocessingDocument.Create(SaveFolderPath + namaFile, WordprocessingDocumentType.Document);
            MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();
            mainDocumentPart.Document = new Document();
sama halnya klo di WordProcessingML nya, itu kita buat yg bagian ini :
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
.
.
.
</w:wordDocument>


Lalu kita buat bagian Body nya..
Body documentBody = new Body();
sama halnya klo di WordProcessingML nya, itu kita buat yg bagian ini :
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    <w:body>
.
.
.
    </w:body>
</w:wordDocument>


Masih ingatkan? di OpenXML Word #1, saya sudah menjelaskan mengenai Paragraph, Run, & Text..
Jika kita ingin membuat dan memasukkan Text "Hello World", Text tersebut harus dimasukkan ke dalam Run.. Dan Run tersebut harus dimasukkan ke dalam Paragraph..

Jadi seperti ini..
<w:p>
    <w:r>
        <w:t>Hello, World!</w:t>
    </w:r>
</w:p>

Mari kita masukkan ke dalam kodingan, jadinya seperti ini :
Paragraph paragraph1 = new Paragraph();
            Run run1 = new Run();
            Text text1 = new Text { Text = "Hello, World!" };
            run1.Append(text1);
            paragraph1.Append(run1);
sama halnya klo di WordProcessingML nya, itu kita buat yg bagian ini :
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    <w:body>
        <w:p>
            <w:r>
                <w:t>Hello, World.</w:t>
            </w:r>
        </w:p>
    </w:body>
</w:wordDocument>

Lalu kita harus meng-'append' Text ke dalam Run.. dan meng-'append' Run ke dalam Paragraph..
Seperti di bawah ini :
run1.Append(text1);
            paragraph1.Append(run1);


Terakhir, kita men-'append' Paragraph tersebut ke dalam Body yg telah kita buat tadi.. Dan kita Close..
Seperti di bawah ini :
documentBody.Append(paragraph1);
            mainDocumentPart.Document.Append(documentBody);
            document.MainDocumentPart.Document.Save();
            document.Close();
            document.Dispose();

OpenXML akan langsung secara otomatis men-save file yg kita buat, apabila kita sudah men-close Document nya..


Hasil keseluruhan tampak seperti ini :

protected void btnPrint_Click(object sender, EventArgs e)
{
            //Masukkan Path folder yang ingin di-save & nama File
            String SaveFolderPath = Server.MapPath("~") + "Temp\\";
            String namaFile = "WordTest.docx";
   
            //Kita buat Document Word nya dengan OpenXML
            WordprocessingDocument document = 
                WordprocessingDocument.Create(SaveFolderPath + namaFile, WordprocessingDocumentType.Document);
            MainDocumentPart mainDocumentPart = document.AddMainDocumentPart();
            mainDocumentPart.Document = new Document();

            Body documentBody = new Body();
            Paragraph paragraph1 = new Paragraph();
            Run run1 = new Run();
            Text text1 = new Text { Text = "Hello, World!" };
            run1.Append(text1);
            paragraph1.Append(run1);
            documentBody.Append(paragraph1);

            mainDocumentPart.Document.Append(documentBody);
            document.MainDocumentPart.Document.Save();
            document.Close();
            document.Dispose();

            //Memprint nya keluar
            Response.Clear();
            Response.AddHeader("content-disposition", "attachment;filename=" + namaFile);
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
            Response.WriteFile(SaveFolderPath + namaFile);
            Response.End();
(Perhatikan
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
MIME di atas adalah tipe file .docx..
Gunakan MIME yg berbeda untuk men-save file .doc, atau excel atau PowerPoint..)

Selamat mencoba.. ^^ Berikut ScreenShot nya..


OpenXML Word #1 :: Mari mengenal OpenXML :: WordProcessingML

Haloooo.. bagi yg sering menggunakan Interop Word untuk meng-generate Word dari aplikasi web..
Ketahuilah fakta ini, bahwa :
Interop itu tidak cocok digunakan di aplikasi berbasis server.. Interop cocok digunakan untuk aplikasi desktop atau non-server...
Lagipula, untuk menggunakan Interop, di server harus diinstall Microsoft Office Word terlebih dahulu..

Nah.. mari kita belajar menggunakan OpenXML.. selain ringan.. (bisa men-generate ratusan page dalam beberapa detik saja.. Sudah pernah ditest), Server tidak perlu meng-install Ms Office Word yg kadang makan beratus2 Mb.. OpenXML cukup membutuhkan SDK saja..

Bagi yg ingin menginstall OpenXML SDK ini, dapat di dapat di :
\\storage\binus\Syahdan\IT\upload\OpenXML\OpenXMLSDKv2.msi
atau jika ternyata installer yg saya upload ternyata sudah dihapus, dapat di download di sini :
http://www.microsoft.com/download/en/details.aspx?id=5124
(cukup install yg SDK saja)

Sekarang saya akan jelaskan sedikit tentang WordProcessingML.. (Bagi yg udah kebelet pengen membuat dokumen Word dengan OpenXML, anda dapat langsung menuju artikel : "OpenXML Word #2")

WordProcessingML, itu adalah XML yg digunakan oleh Word untuk merangkai dokumennya..
Untuk melihat secara langsung seperti apa XML Word itu? Anda dapat mencoba melihat tutorial yg ditulis oleh Rendy ini :
http://personal.binus.edu/personal/rkristyadi/Blog/Lists/Posts/Post.aspx?ID=6

Yang terpenting adalah Tag utama nya ini :


<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    <w:body>
</w:body>
</w:wordDocument>


dan jika kita ingin menambahkan sebuah Teks..
Maka kita harus memasukkan Teks tersebut ke dalam Run..
Dan Run tersebut harud dimasukkan ke sebuah Paragraph..

Jadi jika kita ingin membuat sebuah Dokumen Word yg berisi Hello World, maka kita harus menambahkan seperti ini :

<?xml version="1.0"?>
<w:wordDocument xmlns:w="http://schemas.microsoft.com/office/word/2003/wordml">
    <w:body>
        <w:p>
            <w:r>
                <w:t>Hello, World.</w:t>
            </w:r>
        </w:p>
    </w:body>
</w:wordDocument>

Berikutnya kita akan belajar membuat dokumen Word..

Senin, 31 Oktober 2011

Create Twitter Logo | Only with CSS

Sebenarnya dengan hanya CSS.. kita bisa membuat logo dan gambar apa saja.. hanya dengan membtuhkan kesabaran dan ketelitian.. Tidak hanya itu, jika kita mendesain dengan CSS, tentu saja, gambar ini dapat di-load dengan sangat ringan dan cepat..

Ini Screenshot hasilnya : (cantik y? jika tidak percaya, silahkan dicoba sendiri.. :D)

Bagaimana cara membuatnya?? Here we go :

Untuk kode HTML :

<div id="twitter">
    <div class="canvas">
        <div class="icon">
            <div class="twitter1 draw"></div>
            <div class="twitter2 mask"></div>
            <div class="twitter3 draw"></div>
            <div class="twitter4 mask"></div>
            <div class="twitter5 draw"></div>
            <div class="twitter6 draw"></div>
            <div class="twitter7 draw"></div>
            <div class="twitter8 mask"></div>
            <div class="twitter9 draw"></div>
            <div class="twitter10 draw"></div>
            <div class="twitter11 mask"></div>
            <div class="twitter12 draw"></div>
            <div class="twitter13 draw"></div>
            <div class="twitter14 draw"></div>
            <div class="twitter15 draw"></div>
            <div class="twitter16 draw"></div>
            <div class="twitter17 draw"></div>
            <div class="twitter18 draw"></div>
            <div class="twitter19 draw"></div>
            <div class="twitter20 draw"></div>
            <div class="twitter21 draw"></div>
            <div class="twitter22 draw"></div>
            <div class="twitter23 draw"></div>
            <div class="twitter24 draw"></div>
            <div class="twitter25 draw"></div>
            <div class="twitter26 draw"></div>
            <div class="twitter27 draw"></div>
        </div>
    </div>
</div>

Dan ini untuk kode CSS nya :

.canvas { 
    border-radius: 5px;
    box-shadow: 0 1px 0 rgba(250,250,250,0.2) inset, 0 2px 2px RGBA(0,0,0,0.5);
    height: 300px;
    overflow: hidden;
    position: relative;
    text-indent:-9999px;
    width: 530px;
    z-index: 10;
}
.icon, .icon * {
    position: absolute;
}#twitter .canvas { 
    background: #33ccff ;
}
#twitter .icon { 
    height: 200px;
    left:105px;
    position: absolute;
    top:50px;
    width:290px;
}
#twitter .draw { 
    background: white;
    opacity:1.6;
}
#twitter .mask { 
    background: #33ccff;
    opacity:1.6;
}
.twitter1,
.twitter3 { 
    border-radius:56px/33px;
    height:33px;
    width:56px;
}
.twitter1 { 
    clip: rect(17px 70px 40px 0);
left: 234px;
top: 72px;
-moz-transform: skewy(-4deg);
-webkit-transform: skewy(-4deg);
transform: skewy(-4deg);
}
.twitter3 { 
    clip:rect(18px 70px 40px 0);
    left:232px;
    top:56px;
    -moz-transform: skewy(-5deg);
    -webkit-transform: skewy(-5deg);
    transform: skewy(-5deg);
}
.twitter2,
.twitter4 { 
    border-radius:38px/10px;
    height: 10px;
    width:38px;
}
.twitter2 { 
    left:254px;
    top:83px;
    -moz-transform:rotate(5deg);
    -webkit-transform:rotate(5deg);
    transform:rotate(5deg);
}
.twitter4 { 
    left:251px;
    top:67px;
}
.twitter5 { 
    border-radius: 148px;
    clip: rect(0 150px 72px 60px);
    height: 148px;
    left: 109px;
    top: 16px;
    width: 148px;
}
.twitter6 { 
    border-radius: 258px/167px;
    bottom: 0;
    clip: rect(50px 120px 170px 0px);
    height: 167px;
    left: -5px;
    width: 258px;
}
.twitter7 { 
    border-radius: 259px/208px;
    bottom: 0;
    clip: rect(91px 260px 220px 117px);
    height: 208px;
    left: -3px;
    -moz-transform: skewx(-5deg);;
    -webkit-transform: skewx(-5deg);
    transform: skewx(-5deg);
    width: 259px;
}
.twitter8 { 
    border-radius: 149px;
    height: 149px;
    left: -22px;
    top: 16px;
    width: 149px;
}
.twitter9, .twitter10 { 
    border-radius: 63px/157px;
    height: 157px;
    width: 63px;
}
.twitter9 { 
    clip:rect(49px 20px 160px 0px);
    left: 149px;
    top: -20px;
    -moz-transform: rotate(27deg);
    -webkit-transform: rotate(27deg);
    transform: rotate(27deg);
}
.twitter10 { 
    clip:rect(26px 20px 50px 0px);
    left: 149px;
    top: -20px;
    -moz-transform: rotate(27deg);
    -webkit-transform: rotate(27deg);
    transform: rotate(27deg);
}
.twitter11 { 
    height: 10px;
    left: 182px;
    top: 3px;
    -moz-transform: rotate(20deg);
    -webkit-transform: rotate(20deg);
    transform: rotate(20deg);
    width: 10px;
}
.twitter12 { 
    border-radius: 8px/30px;
    height: 30px;
    left: 198px;
    top: -3px;
    -moz-transform: rotate(66deg);
    -webkit-transform: rotate(66deg);
    transform: rotate(66deg);
    width: 8px;
}
.twitter13 { 
    border-radius: 7px 13px 0 0/64px;
    height: 44px;
    left: 180px;
    top: -8px;
    -moz-transform: rotate(53deg);
    -webkit-transform: rotate(53deg);
    transform: rotate(53deg);
    width: 9px;
}
.twitter14 { 
    border-radius: 27px 137px 137px 137px/50px;
    clip: rect(0 130px 7px 14px);
    height: 50px;
    left: 29px;
    top: 47px;
    -moz-transform: rotate(24deg) skewx(32deg);
    -webkit-transform: rotate(24deg) skewx(32deg);
    transform: rotate(24deg) skewx(32deg);
    width: 137px;
}
.twitter15 { 
    border-radius: 36px/58px;
    clip: rect(14px 8px 60px 0);
    height: 58px;
    left: 48px;
    top: 5px;
    -moz-transform: rotate(-31deg);
    -webkit-transform: rotate(-31deg);
    transform: rotate(-31deg);
    width: 36px;
}
.twitter16 {
    clip: rect(0px 30px 3px 0);
    border-radius: 30px/10px;
    height: 10px;
    left: 50px;
    top: 59px;
    width: 30px;
}
.twitter17   {
    clip: rect(31px 76px 62px 0);
    border-radius: 80px/62px;
    height: 62px;
    left: 51px;
    top: 31px;
    -moz-transform: skewx(10deg);
    -webkit-transform: skewx(10deg);
    transform: skewx(10deg);
    width: 80px;
}
.twitter18 {
    clip: rect(0px 30px 7px 0);
    border-radius: 30px/20px;
    height: 20px;
    left: 64px;
    top: 91px;
    width: 30px;
}
.twitter19 {
    clip: rect(16px 40px 34px 0);
    border-radius: 40px 40px 40px 40px/34px;
    height: 34px;
    left: 65px;
    top: 81.5px;
    -moz-transform: skewx(23deg);
    -webkit-transform: skewx(23deg);
    transform: skewx(23deg);
    width: 40px;
}
.twitter20 {
    border-radius: 40px 60px/34px;
    height: 34px;
    left: 86px;
    top: 109px;
    -moz-transform: rotate(-20deg) skewx(10deg) scale(0.9);
    -webkit-transform: rotate(-20deg) skewx(10deg) scale(0.9);
    transform: rotate(-20deg) skewx(10deg) scale(0.9);
    width: 50px;
}
.twitter21 {
    background: none !important;
    border: 56px solid white;
    border-right: 59px solid transparent;
    border-top: 33px solid transparent;
    height: 0;
    left: 77px;
    top: 30px;
    -moz-transform: skewx(30deg);
    -webkit-transform: skewx(30deg);
    transform: skewx(30deg);
    width: 0;
}
.twitter22 {
    background: none !important;
    border: 20px solid transparent;
    border-right-color: white;
    border-bottom-color: white;
    top: 54px;
    left: 130px;
}

Ini hanya berlaku untuk Chrome dan Safari saja..
Klo untuk Firefox hasilnya jadi kayak gini :



Untuk melihat lebih banyak logo lainnya dapat dikunjungi di web berikut ini :
Sumber : http://www.ecsspert.com/twitter.php

Minggu, 30 Oktober 2011

Introduction

Halooo.. nama saya Frandy Jaya..
Sekarang bekerja sebagai Programmer di IS Dev School Simprug..
Dengan spesialisasi ASP.NET C#..

Blog ini didedikasikan untuk mengisi tips tips atau materi yg saya temukan untuk memecahkan masalah yg saya temui, baik dari percobaan saya sendiri ataupun dari browsing melalui internet..

Semoga blog ini bisa menjadi panduan bagi temen2 yg kesulitan dan menghadapi kebuntuan yg saya alami..

Terima kasih.. :D


Regards,

Frandy Jaya
Programmer

IT Directorate of BINA NUSANTARA
Jl. KH Syahdan No. 9
Jakarta Barat 11480
Phone: (021) 5345830 ext 4205
Website:
http://ict.binus.edu | http://www.binus.edu




 Desire backed by Faith knows no such word as Impossible - Napoleon Hill

Kamis, 27 Oktober 2011

Read Word Document using Interop.Word

You have a Microsoft Word document (.doc) and want to read it in your C# program. With the Microsoft.Office.Interop.Word assembly, you can get the contents and formatting from the document, as we demonstrate in this tutorial.


First, we show the file we will read: it contains three paragraphs containing one word each. The program first instantiates an Application instance and then we call Documents.Open on that variable. Next, we loop through the Words collection and read the Text property on each element. We then display the text content. Finally, we invoke Quit on the Application instance.
Word document [word.doc]

One

Two

three

Program that uses Microsoft Word interop [C#]

using System;
using Microsoft.Office.Interop.Word;

class Program
{
    static void Main()
    {
 // Open a doc file.
 Application application = new Application();
 Document document = application.Documents.Open("C:\\word.doc");

 // Loop through all words in the document.
 int count = document.Words.Count;
 for (int i = 1; i <= count; i++)
 {
     // Write the word.
     string text = document.Words[i].Text;
     Console.WriteLine("Word {0} = {1}", i, text);
 }
 // Close word.
 application.Quit();
    }
}

Output

Word 1 = One
Word 2 =
Word 3 = Two
Word 4 =
Word 5 = three
Word 6 =
Empty paragraphs. As you can see, Word 2, Word 4, and Word 6 are empty. The empty paragraphs in the input file are considered words. If you have multiple words in a paragraph, they will each be separate in the Words collection. This means a paragraph is made up of a collection of one or more words.
Quit. Why is the application.Quit statement important? If you don't include this, the WINWORD.EXE application will remain in the process list of the computer. Then, when this program is run again, a new one will be started parallel to it. This will waste memory and eventually cause resource usage problems.

Summary

In this tutorial, we took a look at the Microsoft.Office.Interop.Word assembly and learned how to read in data from a Word document. This can be very useful when you have DOC or DOCX files and want to programmatically read in data from your C# program.

Printing With CSS

In this tutorial we are going to construct a css file to allow us to send a formatted page to the printer. Normally this would incur producing a text only (printer friendly) page that the user could print out from their browsers Print button.
With CSS we have no need for that as we can specify a CSS file to be used when print output is required. This is simply achieved by placing media="print" in our stylesheet link tag. We also need to set the original stylesheet link tag to media="screen".
e.g.
<link type="text/css" media="screen" rel="stylesheet" href="main.css" />
<link type="text/css" media="print" rel="stylesheet" href="print.css" />

As you can see the first statement tells the browser that the CSS file called main.css is to be used for output to the screen (i.e. your monitor display). The second statement tells the browser that the file called print.css is to be used when print output is requested via the browsers print button.
One of the main things to remember is that all your presentation in your web page should be under the control of your main stylesheet for this to work properly. Obviously if fonts and colours are embedded in your mark up then turning the stylesheet on or off will make little difference to this mark up.
OK assuming that your page is constructed properly with CSS what do we need to do next to set up the page for printed output. The first thing to do is to copy your main.css file and call it something like print.css. This will allow us the to change all the necessary settings that apply to our page to make it format correctly for printing.
There are many things you can do to the css file to make your printed output more desirable.
e.g.
  • You can turn things (images, text etc) off by setting the display property to none.
  • Hide your navigation and links
  • Change pixel sizes to points
  • Add or Hide headings
  • Adjust your layout and flow of the document
Lets start with a small example page and some basic css.


If you view the source you will notice that that I have left the css code in the <head> sections of the page. This is so that you can view it more easily. It still works this way as I have set media="print" and media="screen" in the style tags.
e.g.
<style type="text/css" media="screen">
<!--css code goes here-->
</style>
<style type="text/css" media="print">
<!--css code goes here-->
</style>

One thing to note is that if you set your stylesheet to media="screen" then the printed version of the page will not use any of the css formatting and can sometimes be the only thing you need to do in order to arrange your page for printing.
On the example page you will notice that there is an image, some text and some simple navigation. However if you have tried to print the page you will notice that the image wasn't printed, the text colours were set to black and the sizes of the headings were different. Also you will notice that there was extra text printed that was not on the viewed web page.
All this can be accomplished quite simply as follows. In the main CSS file we have declared the main body text to be coloured and to be small (which is an absolute ref that browsers know about).
main.css
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: small;
color: #FF0000;
background-color: #CCCCCC;
}

print.css
body {
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 10pt;
color: #000000;
background-color: #FFFFFF;
}

As you can see from the above code all I have changed is the font-size to 10pt and the color to black (#000000). I have also set the background colour to white but this isn't strictly necessary as the background doesn't get printed anyway, but it is always good practice to specify a background colour and a foreground colour together.
This change means that all body text will now be black when printed and will have a point size of 10 points which printers understand correctly.
To stop the image being printed I have merely set up an img rule in the print.css stylesheet that sets the image display to none.
img {
display: none
}

Obviously this will affect all images on the page but that is probably what you want anyway. If you only want to hide specific images you can wrap them up in a class as follows.
add this to your stylesheet:
.hide img {
display: none;
}
and then in the body of your document you will need something like this:
<div class="hide">
<p><img src="pic5.jpg" width="100" height="66" />
</p>
</div>

This will ensure that only images inside a class of hide will be affected by the image display being turned off. And of course as this is in your print stylesheet the image will be displayed correctly in your viewed web page.
Everything should start falling in to place by now! If we want to hide things in our print out we can set their display properties todisplay:none. If we want different sized headings, margins and other properties we just change the settings in the print.css style sheet to suit.
But how do we add text to our print out that is not shown on the viewed page. This is achieved quite simply by reversing the procedures we used above. In the print.css stylesheet we set the display to block (or in-line depending upon circumstance) and in the main.css stylesheet we set the display properties to none. This will ensure that the content we only want printed is not seen on the viewed web page.
An easy way to do this is to wrap the text etc in a div with a class so that we can turn the whole div off and on as required.
e.g.



In our print.css stylesheet we add this code:
.printtext {
display: block;
}
and then in the main.css stylesheet we add:
.printtext {
display: none;
}
and then in the body of our page we add this:
<div class="printtext">
<p>This text and the text below will only get printed and will not show in the
webpage.</p>
<p>address1<br />
address2<br />
address3<br />
address4</p>
<p>Telephone 0101 0101010</p>
</div>


That's more or less it. You can of course format your printed output much more carefully and precisely than I have done here, but the general principles remain the same.
One thing that is useful is planning in advance to make things easier. If you are constructing a page for printing then design with that thought in mind. You could use one of the headings such as h3, h4 etc to be used as your printed text. i.e. text that is only the printout and not viewed on the screen. Then you would be able to turn all this text on or off by simply setting the property of theh3,h4 tag to display: none or display:block.
The best way to learn is to experiment and to practice and to keep on coding.

Menulis text ke file tanpa mereplace dan menulis baris baru di C#

Menyambung tutorial sebelumnya yang berjudul MENULIS DAN MEMBACA FILE (READ WRITE) DI C# tentang operasi operasi penulisan text ke file. jika pada tutorial tersebut hanya menjelaskan bagaiman text tersebut disimpan dan dibaca kembali kesuatu file, maka disini akan dibahas tentang bagaiamana melakukan replace file dan bagaimana menambahkan baris baru ke file. Berikut ini adalah source utama agar file text yang sudah ada tidak direplace oleh program
using (StreamWriter tulis = new StreamWriter(@"C:\fileku.txt", true))


source diatas sedikit berbeda dengan source pada tutorial  MENULIS DAN MEMBACA FILE (READ WRITE) DI C# . Yang membedakan adalah code TRUE dan FALSE disintak terakhir, jika dituliskan true maka program tidak mengizinkan untuk melakukan proses replace setiap kali file text disimpan di file yang sama, melainkan akan menuliskannya dibawah text yang sudah ada, begitu juga sebaliknya jika diisi dengan false maka setiap kali melakuakn proses penulisan kefile text-text yang sebelumnya ada akan dihapus dan diganti dengan text yang baru. Untuk mengimplementasikan source code diatas kedalam project, silahkan ikuti langkah demi langkah dibawah ini.

Langkah 1 : siapkan project kosong, klik file new project pada IDE visual studio. Akan muncuk kotak dialog project name, silahkan isi nama project sesuai dengan keinginan anda tidak ada aturan khusus yang mengatur penamaan project. Hanya saja karakter karakter tertentu dilarang digunakan dalam penamaan project
Langkah 2 : tambahkan beberapa komponen yang digunakan seperti textbox,button dan juga listbox. Atur dan tata sedemikian rupa sehingga tampilannya seperti pada gambar dibawah ini

Gambar 1. Desain Aplikasi Program 
Langkah 3 : doubel klik pada button1 (button yang berlabelkan Write) untuk membangkitkan single klik pada button1, masukan source code berikut ini pada jendela source code dari button1



try
{
 using (StreamWriter tulis = new StreamWriter(@"C:\fileku.txt", true))
 {
  tulis.WriteLine(textBox1.Text);
  textBox1.Text = "";
 }
}
catch (Exception error)
{
 MessageBox.Show("Terjadi kesalahan\n" + error.ToString());
}


Langkah 4 : Doubel klik pada button2, button yang berlabelkan READ untuk membangkitkan event single klik pada button tersebut, dan masukan source code berikut ini pada event single klik pada button2

String temp = "";
listBox1.Items.Clear();
            using (StreamReader sr = new StreamReader(@"C:\fileku.txt"))
            {
                String line;
                while ((line = sr.ReadLine()) != null)
                {
                    temp = temp+line;
                    listBox1.Items.Add(line);                     
                }
            }



dan jangan lupa pastikan bahwa anda telah menambahkan library header agar program dapat melakukan proses IO.


using System.IO;

Langkah 5 : Done, program selesai dibuat. Jika langkah langkah diatas dilakukan dengan benar maka program akan mampu melakukan proses penyimpanan text ke file secara berulang kali tanpa mereplace file text yang sudah ada sebelumnya. dan setiap penambahan text akan diletakkan pada baris baru. Berikut ini adalah preview dari program yang sudah dijalankan

Gambar 2. Preview saat pertama kali program dijalankan


Gambar 3. Preview program ketika button read ditekan


Gambar 4. Preview saat program menuliskan text ke file


 Gambar 5. Preview program setelah button Write ditekan