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 8 and Storage - Temporary Data (Application Data)



Clound SSD Virtual Server

Windows Phone 8 and Storage - Temporary Data (Application Data)

Windows Phone 8 and Storage - Temporary Data (Application Data) สำหรับการจัดเก็บไฟล์ Storage แบบ Temporary Data บน Windows Phone 8 ใช้สำหรับการจัดเก็บไฟล์ทั่ว ๆ ไปได้ทุกประเภท เช่น Text file , Image , Media และอื่น ๆ โดยจะเป็นการจัดเก็บลงใน Temporary ของเครื่องที่ติดตั้ง Apps ซึ่งไฟล์ข้อมูลที่จัดเป็นแบบชั่วคราวเท่านั้น และอาจจะหายไปเมื่อมีการ Clear หรือ Delete ตัว Temp ไฟล์ออกจากเครื่องบนอุปกรณ์ Smartphone / Tablets

ตัวอย่างการจัดเก็บแบบ Text file

การจัดเก็บแบบ Text file ลงใน Temporary
            // Get the Temporary folder.
            StorageFolder temporary = Windows.Storage.ApplicationData.Current.TemporaryFolder;

            // Create a new folder name DataFolder.
            var dataFolder = await temporary.CreateFolderAsync("DataFolder",
                CreationCollisionOption.OpenIfExists);

            // Create a new file named DataFile.txt.
            var file = await dataFolder.CreateFileAsync("DataFile.txt",
            CreationCollisionOption.ReplaceExisting);

            // Write text file
            await file.WriteTextAsync(file, "String");

การอ่าน Text file
            // Get the Temporary folder.
            StorageFolder temporary = Windows.Storage.ApplicationData.Current.TemporaryFolder;

            if (temporary != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await temporary.GetFolderAsync("DataFolder");

                // Get the file.
                var file = await dataFolder.OpenStreamForReadAsync("DataFile.txt");

                // Read the data.
                using (StreamReader streamReader = new StreamReader(file))
                {
                    this.textBlock1.Text = streamReader.ReadToEnd();
                }

            }


ลองมาดูตัวอย่างเพื่อความเข้าใจมากขึ้น

Example สร้าง Text file และการอ่าน Text file (Temporary Data)

Windows Phone 8 and Storage - Temporary Data (Application Data)

สร้างหน้าจอสำหรับการ Input ข้อมูล และการ Read / Write ข้อมูลลงใน Text file

MainPage.xaml
        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0">
            <TextBox 
                Name="textBox1" 
                HorizontalAlignment="Left"
                Height="72" 
                Margin="0,22,0,0" 
                TextWrapping="Wrap" 
                Text="Enter text here." 
                VerticalAlignment="Top" Width="456"/>
            <Button 
                Name='btnWrite' 
                Content="Write"
                HorizontalAlignment="Left" 
                Margin="10,94,0,0" 
                VerticalAlignment="Top"
                Width="156" 
                Click="btnWrite_Click"/>
            <TextBlock 
                Name="textBlock1" 
                HorizontalAlignment="Left"
                Margin="10,293,0,0" 
                TextWrapping="Wrap" Text=""
                VerticalAlignment="Top" 
                Height="61" 
                Width="436"/>
            <Button 
                Name="btnRead" 
                Content="Read"
                HorizontalAlignment="Left" 
                Margin="10,374,0,0" 
                VerticalAlignment="Top"
                Width="156" 
                IsEnabled="False"
                Click="btnRead_Click"/>
        </Grid>









MainPage.xaml.cs
using System.IO;
using System.Threading.Tasks;
using Windows.Storage;


namespace myPhoneApp
{

    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
        }


        private async void btnWrite_Click(object sender, RoutedEventArgs e)
        {
            await WriteToFile();

            // Update UI.
            this.btnWrite.IsEnabled = false;
            this.btnRead.IsEnabled = true;
        }

        private async Task WriteToFile()
        {
            // Get the text data from the textbox. 
            byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(this.textBox1.Text.ToCharArray());

            // Get the Temporary folder.
            StorageFolder temporary = Windows.Storage.ApplicationData.Current.TemporaryFolder;

            // Create a new folder name DataFolder.
            var dataFolder = await temporary.CreateFolderAsync("DataFolder",
                CreationCollisionOption.OpenIfExists);

            // Create a new file named DataFile.txt.
            var file = await dataFolder.CreateFileAsync("DataFile.txt",
            CreationCollisionOption.ReplaceExisting);

            // Write the data from the textbox.
            using (var s = await file.OpenStreamForWriteAsync())
            {
                s.Write(fileBytes, 0, fileBytes.Length);
            }
        }


        private async void btnRead_Click(object sender, RoutedEventArgs e)
        {
            await ReadFile();

            // Update UI.
            this.btnWrite.IsEnabled = true;
            this.btnRead.IsEnabled = false;
        }

        private async Task ReadFile()
        {
            // Get the Temporary folder.
            StorageFolder temporary = Windows.Storage.ApplicationData.Current.TemporaryFolder;

            if (temporary != null)
            {
                // Get the DataFolder folder.
                var dataFolder = await temporary.GetFolderAsync("DataFolder");

                // Get the file.
                var file = await dataFolder.OpenStreamForReadAsync("DataFile.txt");

                // Read the data.
                using (StreamReader streamReader = new StreamReader(file))
                {
                    this.textBlock1.Text = streamReader.ReadToEnd();
                }

            }
        }

 
    }
}


Windows Phone 8 and Storage - Temporary Data (Application Data)

ทดสอบการ Input ข้อมูลและสร้าง Text file

Windows Phone 8 and Storage - Temporary Data (Application Data)

แสดงค่าใน Text file ที่ได้จากการจัดเก็บไว้ใน Temporary Data








อ่านเพิ่มเติม


   
Share


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


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


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2014-07-27 19:05:39 / 2017-03-25 22:34:48
  Download : No files
 Sponsored Links / Related

 
Windows Phone 8 and Isolated Storage Tools
Rating :

 
Windows Phone 8 and Isolated Storage (Settings , Files and folders)
Rating :

 
Windows Phone 8 and Storage - Local Data (Application Data)
Rating :

 
Windows Phone 8 and Storage - Roaming Data (Application Data)
Rating :

 
Windows Phone 8 and Storage - Encrypt Data for Windows Phone
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 อัตราราคา คลิกที่นี่