Smart Device Mobile Application Read and Write Text File |
|
|
|
Smart Device Mobile Application Read and Write Text File ง่าย ๆ กับ .NET Framework การเขียน Device Application แบบง่าย ๆ สำหรับการ จัดการ Text File จัดเก็บข้อมูล และ อ่านข้อมูลในรูปแบบของ Text File บน Smart Device
จากบทความ
Go to : .NET Smart Device เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6
Go to : Shared Folder or Map Drive in Emulator Smart Device Mobile Application
จะได้เรียนรู้โครงสร้างการทำงานและการเขียนโปรแกรมพื้นฐานกับ Smart Device ที่ทำงานบนอุปกรณ์ประเภท Smart Phone ที่รันบน Windows CE บทความนี้จะเป็น Workshop เกี่ยวกับการรับข้อมูลและการจัดเก็บข้อมูลลงใน Text File รวมทั้งการอ่านข้อมูล Text File ทีได้ถูกจัดเก็บไว้ใน อุปกรณ์ประเภท Smart Phone สำหรับตัวอย่างนี้ จะไม่ได้มีการเชื่อมต่อกับอุปกรณ์จริง แต่จะใช้การรันผ่าน Virtual Emulator ของ .NET Framework ซึ่งอ่านและทำความเข้าใจกับโครงสร้างของ Emulator ได้จากบทความนี้ => พื้นฐาน Emulator
เริ่มต้นจากการสร้าง Project ประเภท Smart Device -> Device Application

เริ่มต้นการสร้าง Project
การสร้าง Form
Program.cs
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();
}
}
}
ทดสอบการรันโปรแกรม

เลือก Pocket PC 2003 SE Emulator

หน้าจอหลัก

สำหรับรับข้อมูลและเขียนลงใน Text File

สำหรับนำข้อมูลจาก Text File มาแสดง

หลังจากโปรแกรมเสร็จสิ้นแล้ว เมื่อกลับไปยัง Emulator ก็จะพบโฟเดอร์ และไฟล์ที่โปรแกรมได้สร้างไว้

ไฟล์ที่ถูกสร้าง

รายละเอียดการจัดเก็บไฟล์
เพิ่มเติมสำหรับ Code ภาษา VB.NET
frmMain.vb
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
บทความที่เกี่ยวข้อง
Go to : .NET Smart Device เขียนโปรแกรมบน Smartphone, Pocket PC , Windows CE , Window Mobile 5-6
Go to : Shared Folder or Map Drive in Emulator Smart Device Mobile Application
|
|
|
|
 |
|
|
|
|
|
|
|
|
|
|
|
By : |
TC Admin
|
|
Score Rating : |
- |
|
Create Date : |
2011-07-30 00:38:25 |
|
Download : |
(0.43 MB) |
|
|
|
|
|
|
|