Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Windows Phone Create and Write Text File In Isolated Storage (Application Storage)



Clound SSD Virtual Server

Windows Phone Create and Write Text File In Isolated Storage (Application Storage)

Windows Phone Create and Write Text File In Isolated Storage (Application Storage) ตัวอย่างการสร้าง Create Text file และการเขียน Write Text file ลงใน Isolated Storage ของ Application บน Windows Phone แบบง่าย โดยใช้ NameSpace ของ System.IO และ System.IO.IsolatedStorage เข้ามาจัดการกับ Input และ Output ที่จะเขียนลงบน Isolated Storage

Windows Phone Create and Write Text File In  Isolated Storage


Basic Windows Phone and Isolated Storage (Application Storage)

สำหรับพื้นฐาน Isolated Storage กับ Windows Phone ควรอ่าน 2 บทความนี้ เพื่อความเข้าใจ

Example การสร้าง Text file และการเขียน Text file แบบง่าย ๆ บน Windows Phone

MainPage.xaml

Windows Phone Create and Write Text File In  Isolated Storage

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBlock Height="30" Name="txtTitle" Text="Input Yout Text" TextAlignment="Center" FontSize="22" />
            <TextBox Height="72" Name="txtMessage" Text="" Width="460" />
            <Button Content="Save" Height="72" Name="btnSave" Width="160" Click="btnSave_Click" />
        </StackPanel>
        
    </Grid>









MainPage.xaml.vb (VB.NET)
Imports System.IO
Imports System.IO.IsolatedStorage

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    Private strFileName As String = "thaicreate.txt"

    ' Constructor
    Public Sub New()
        InitializeComponent()

        '*** for Create Text File ***'
        Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        If Not isoStore.FileExists(strFileName) Then
            Dim dataFile As IsolatedStorageFileStream = isoStore.CreateFile(strFileName)
        End If

    End Sub


    Private Sub btnSave_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
        '*** for Write Text File ***'
        Dim isoStore As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
        Dim sw As New StreamWriter(New IsolatedStorageFileStream(strFileName, FileMode.Append, isoStore))
        sw.WriteLine(Me.txtMessage.Text)
        sw.Close()

        MessageBox.Show("Create and Save Completed.", "Result", MessageBoxButton.OK)

    End Sub

End Class

MainPage.xaml.cs (C#)
using System;
using System.Windows;
using System.Net;
using System.IO;
using System.Text;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Collections.Generic;
using System.Windows.Controls;

namespace PhoneApp
{
    public partial class MainPage : PhoneApplicationPage
    {

        private string strFileName = "thaicreate.txt";
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            //*** for Create Text File ***'
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (!isoStore.FileExists(strFileName))
            {
                IsolatedStorageFileStream dataFile = isoStore.CreateFile(strFileName);
            }

        }

        private void btnSave_Click(System.Object sender, System.Windows.RoutedEventArgs e)
        {
            //*** for Write Text File ***'
            IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
            StreamWriter sw = new StreamWriter(new IsolatedStorageFileStream(strFileName, FileMode.Append, isoStore));
            sw.WriteLine(this.txtMessage.Text);
            sw.Close();

            MessageBox.Show("Create and Save Completed.", "Result", MessageBoxButton.OK);

        }


    }
}

ในตัวอย่างนี้มี Code ทั้งที่เป็น VB.NET และ C# และสามารถดาวน์โหลด All Code ทั้งหมดได้จากส่วนท้ายของบทความ (Login สมาชิกก่อน)

Screenshot

Windows Phone Create and Write Text File In  Isolated Storage

ทดสอบการเขียน Text file ลงไป

Windows Phone Create and Write Text File In  Isolated Storage

แสดงข้อความหลังจากที่เขียน Text file เรียบร้อยแล้ว

Windows Phone Create and Write Text File In  Isolated Storage

ดูไฟล์ที่ถูกสร้างและเขียนบน Isolated Storage








เพิ่มเติม

ใน Property ของ FileMode จะมีอยู่หลายตัวเช่น

Windows Phone Create and Write Text File In  Isolated Storage

โดย Append คือการเขียนต่อจากไฟล์เดิมที่มีอยู่แล้ว

   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-15 11:37:39 / 2017-03-25 22:16:15
  Download : Download  Windows Phone Create and Write Text File In  Isolated Storage (Application Storage)
 Sponsored Links / Related

 
Windows Phone and Isolated Storage (Application Storage)
Rating :

 
Windows Phone ListBox Image Binding and Resource from Isolated Storage (Application Storage)
Rating :

 
Windows Phone Play MP3 or Video MediaElement from Isolated Storage (Application Storage)
Rating :

 
Windows Phone Copy Transfer file from PC Between Isolated Storage (Application Storage)
Rating :

 
Windows Phone Download Save file to Isolated Storage (Application Storage)
Rating :

 
Windows Phone Read Text File in Isolated Storage (Application Storage)
Rating :

 
Windows Phone Delete / Remove File In Isolated Storage (Application Storage)
Rating :

 
Windows Phone List Show File In Isolated Storage (Application Storage)
Rating :

 
Windows Phone Image Source From Isolated Storage (Application Storage)
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 05
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่