using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace SmartDevice
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[MTAThread]
static void Main()
{
Application.Run(new frmMain());
}
}
}
1. Form ชื่อ frmMain.cs
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace SmartDevice
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnWrite_Click(object sender, EventArgs e)
{
frmWrite frm = new frmWrite();
frm.ShowDialog();
}
private void btnRead_Click(object sender, EventArgs e)
{
frmRead frm = new frmRead();
frm.ShowDialog();
}
}
}
2. Form ชื่อ frmWrite.cs
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SmartDevice
{
public partial class frmWrite : Form
{
public frmWrite()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtWrite_Click(object sender, EventArgs e)
{
if (this.txtInput.Text.Trim() == "")
{
MessageBox.Show("Please input Text", "Input Text", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1);
}
else
{
//*** Create ThaiCreate ***//
DirectoryInfo DirTc = new DirectoryInfo("\\ThaiCreate");
if (!DirTc.Exists)
{
DirTc.Create();
}
DirTc = null;
//*** Write to Text file ***/
string strPath = "\\ThaiCreate\\win.txt";
StreamWriter StrWer = default(StreamWriter);
StrWer = new StreamWriter(strPath, true);
StrWer.WriteLine(this.txtInput.Text);
StrWer.Close();
this.txtInput.Text = "";
MessageBox.Show("Write Text Successful", "Write Text", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
}
}
}
}
2. Form ชื่อ frmRead.cs
Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SmartDevice
{
public partial class frmRead : Form
{
public frmRead()
{
InitializeComponent();
string strPath = "\\ThaiCreate\\win.txt";
StreamReader StrRer;
string strLine;
StrRer = File.OpenText(strPath);
while (!(StrRer.EndOfStream))
{
strLine = StrRer.ReadLine().ToString();
this.txtShow.Text += strLine + "\r\n";
}
StrRer.Close();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Namespace SmartDevice
Public Partial Class frmMain
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnWrite_Click(sender As Object, e As EventArgs)
Dim frm As New frmWrite()
frm.ShowDialog()
End Sub
Private Sub btnRead_Click(sender As Object, e As EventArgs)
Dim frm As New frmRead()
frm.ShowDialog()
End Sub
End Class
End Namespace
frmWrite.vb
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Namespace SmartDevice
Public Partial Class frmWrite
Inherits Form
Public Sub New()
InitializeComponent()
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs)
Me.Close()
End Sub
Private Sub txtWrite_Click(sender As Object, e As EventArgs)
If Me.txtInput.Text.Trim() = "" Then
MessageBox.Show("Please input Text", "Input Text", MessageBoxButtons.OK, MessageBoxIcon.Hand, MessageBoxDefaultButton.Button1)
Else
'*** Create ThaiCreate ***//
Dim DirTc As New DirectoryInfo("\ThaiCreate")
If Not DirTc.Exists Then
DirTc.Create()
End If
DirTc = Nothing
'*** Write to Text file ***/
Dim strPath As String = "\ThaiCreate\win.txt"
Dim StrWer As StreamWriter = Nothing
StrWer = New StreamWriter(strPath, True)
StrWer.WriteLine(Me.txtInput.Text)
StrWer.Close()
Me.txtInput.Text = ""
MessageBox.Show("Write Text Successful", "Write Text", MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1)
End If
End Sub
End Class
End Namespace
frmRead.vb
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports System.IO
Namespace SmartDevice
Public Partial Class frmRead
Inherits Form
Public Sub New()
InitializeComponent()
Dim strPath As String = "\ThaiCreate\win.txt"
Dim StrRer As StreamReader
Dim strLine As String
StrRer = File.OpenText(strPath)
While Not (StrRer.EndOfStream)
strLine = StrRer.ReadLine().ToString()
Me.txtShow.Text += strLine & vbCr & vbLf
End While
StrRer.Close()
End Sub
Private Sub btnClose_Click(sender As Object, e As EventArgs)
Me.Close()
End Sub
End Class
End Namespace