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 > Windows Store Apps > Windows Store and Storage / Data (C#) > Copy ไฟล์ลง Storage แสดงชื่อไฟล์ใน Storage บน Windows Store Apps (C#)



Clound SSD Virtual Server

Copy ไฟล์ลง Storage แสดงชื่อไฟล์ใน Storage บน Windows Store Apps (C#)

Copy ไฟล์ลง Storage แสดงชื่อไฟล์ใน Storage บน Windows Store Apps (C#) หัวข้อนี้จะเป็นการเขียน Apps เพื่อ Copy ไฟล์ลงใน Storage ของ Apps แบบง่าย ๆ โดยจะเลือกไฟล์ที่อยู่บน Desktop ในเครื่อง Client สามารถเลือกได้หลาย ๆ รูป เพื่อ Copy ไฟล์ไปไว้บน Storage ของ Windows Store Apps ซึ่งประกอบด้วย Local data, Roaming data และ Temporary data.

Syntax การ Copy ไฟล์ลงใน Storage
            foreach (var fs in file)
            {
                await fs.CopyAsync(ApplicationData.Current.LocalFolder, fs.Name, NameCollisionOption.GenerateUniqueName);
            }

ตัวอย่างการเขียน Apps เพื่อ Copy ไฟล์ลงใน Storage บน Windows Store Apps

Windows Store Apps Copy File Storage

ไฟล์ที่อยู่บน Desktop ของ Client โดยเราจะใช้การ Browse เพื่อเลือก Copy ไฟล์ทั้งหมดนี้ไปจัดเก็บไว้บน Storage ของ Apps

Windows Store Apps Copy File Storage

สร้าง Page ขึ้นมา 2 เพจ คือ MainPage.xaml (สำหรับ Browse และ Save ไฟล์) และ Page2.xaml (สำหรับแสดงรายการไฟล์รูป)

Windows Store Apps Copy File Storage

เพจของ MainPage.xaml

Windows Store Apps Copy File Storage

เพจของ Page2.xaml








Example 1 สร้าง Apps เพื่อ Copy เลือกไฟล์ และจัดเก็บใน Local Data บน Windows Store Apps

MainPage.xaml
<Page
    x:Class="WindowsStoreApps.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">


    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

        <TextBlock x:Name="lblStatus" HorizontalAlignment="Left" Margin="93,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="30" Text="Status"/>
        <Button x:Name="btnSelectfile" Content="Select file" HorizontalAlignment="Left" Margin="90,138,0,0" VerticalAlignment="Top" Height="59" Width="125" Click="btnSelectfile_Click"/>
        <Button x:Name="btnGoPage2" Content="Go to Page 2" HorizontalAlignment="Left" Margin="233,138,0,0" VerticalAlignment="Top" Height="59" Width="125" Click="btnGoPage2_Click"/>

    </Grid>

</Page>

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private async void btnSelectfile_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".bmp");
            picker.SuggestedStartLocation = PickerLocationId.Desktop;
            picker.ViewMode = PickerViewMode.Thumbnail;
            var file = await picker.PickMultipleFilesAsync();

            foreach (var fs in file)
            {
                await fs.CopyAsync(ApplicationData.Current.LocalFolder, fs.Name, NameCollisionOption.GenerateUniqueName);
            }

            if (file.Count >0)
            { 
             this.lblStatus.Text = "All file has been copy to LocalFolder";
            }

        }

        private void btnGoPage2_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Page2));
        }
       
    }
}

Page2.xaml
<Page
    x:Class="WindowsStoreApps.Page2"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WindowsStoreApps"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <TextBlock x:Name="lblResult" HorizontalAlignment="Left" Margin="93,69,0,0" TextWrapping="Wrap" VerticalAlignment="Top" FontSize="30" Text="Result"/>
    </Grid>
</Page>

Page2.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using System.Text;
using Windows.UI.Xaml.Media.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Page2 : Page
    {
        public Page2()
        {
            this.InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {

            var folder = Windows.Storage.ApplicationData.Current.LocalFolder;
            var query = folder.CreateFileQuery();
            var files = await query.GetFilesAsync();

            StringBuilder fileName = new StringBuilder();

            for (int j = 0; j < files.Count; j++)
            {
                Windows.Storage.StorageFile file = files[j];

                // Get top level file properties.
                fileName.AppendLine("File name: " + file.Name);
                //fileProperties.AppendLine("File type: " + file.FileType);
            }

            this.lblResult.Text = fileName.ToString();

        }
    }
}

Screenshot

Windows Store Apps Copy File Storage

คลิกเลือก Select File

Windows Store Apps Copy File Storage

ในขั้นตอนนี้ File Dialog จะชี้ Path ไปที่ Desktop ซึ่งสามารถเลือกไฟล์ได้หลาาย ๆ ไฟล์

Windows Store Apps Copy File Storage

หลังจากนั้นไฟล์ทั้งหมดจะถูก Copy ไปไว้ใน Storage ของ Windows Store Apps และให้คลิกที่ Go to Page 2 เพื่อดูรายการไฟล์

Windows Store Apps Copy File Storage

แสดงรายการไฟล์ที่อยู่บน Local Data

สำหรับตัวอย่างแรกเป็นการจัดเก็บลงใน Local data แต่ในกรณีที่ต้องการจัดเก็บลงใน Roaming หรือ Temporary สามารถดูได้ต่อในตัวอย่างที่ 2 และ 3

Example 2 ทดสอบการจัดเก็บลงใน Roaming (RoamingFolder)

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private async void btnSelectfile_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".bmp");
            picker.SuggestedStartLocation = PickerLocationId.Desktop;
            picker.ViewMode = PickerViewMode.Thumbnail;
            var file = await picker.PickMultipleFilesAsync();

            foreach (var fs in file)
            {
                await fs.CopyAsync(ApplicationData.Current.RoamingFolder, fs.Name, NameCollisionOption.GenerateUniqueName);
            }

            if (file.Count >0)
            {
                this.lblStatus.Text = "All file has been copy to RoamingFolder";
            }

        }

        private void btnGoPage2_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Page2));
        }
       
    }
}

Page2.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using System.Text;
using Windows.UI.Xaml.Media.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Page2 : Page
    {
        public Page2()
        {
            this.InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {

            var folder = Windows.Storage.ApplicationData.Current.RoamingFolder;
            var query = folder.CreateFileQuery();
            var files = await query.GetFilesAsync();

            StringBuilder fileName = new StringBuilder();

            for (int i = 0; i < files.Count; i++)
            {
                Windows.Storage.StorageFile file = files[i];

                // Get top level file properties.
                fileName.AppendLine("File name: " + file.Name);
                //fileProperties.AppendLine("File type: " + file.FileType);
            }

            this.lblResult.Text = fileName.ToString();

        }
    }
}









Example 3 ทดสอบการจัดเก็บลงใน Temporary (TemporaryFolder)

MainPage.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Devices.Geolocation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Core;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using Windows.UI.Xaml.Media.Imaging;
using Windows.Storage;
using Windows.Storage.AccessCache;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.Storage.Streams;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    /// 

    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {

        }

        private async void btnSelectfile_Click(object sender, RoutedEventArgs e)
        {
            FileOpenPicker picker = new FileOpenPicker();
            picker.FileTypeFilter.Add(".png");
            picker.FileTypeFilter.Add(".jpg");
            picker.FileTypeFilter.Add(".gif");
            picker.FileTypeFilter.Add(".bmp");
            picker.SuggestedStartLocation = PickerLocationId.Desktop;
            picker.ViewMode = PickerViewMode.Thumbnail;
            var file = await picker.PickMultipleFilesAsync();

            foreach (var fs in file)
            {
                await fs.CopyAsync(ApplicationData.Current.TemporaryFolder, fs.Name, NameCollisionOption.GenerateUniqueName);
            }

            if (file.Count >0)
            {
                this.lblStatus.Text = "All file has been copy to TemporaryFolder";
            }

        }

        private void btnGoPage2_Click(object sender, RoutedEventArgs e)
        {
            this.Frame.Navigate(typeof(Page2));
        }
       
    }
}

Page2.xaml.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using System.Text;
using Windows.UI.Xaml.Media.Imaging;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace WindowsStoreApps
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class Page2 : Page
    {
        public Page2()
        {
            this.InitializeComponent();
        }

        protected async override void OnNavigatedTo(NavigationEventArgs e)
        {

            var folder = Windows.Storage.ApplicationData.Current.TemporaryFolder;
            var query = folder.CreateFileQuery();
            var files = await query.GetFilesAsync();

            StringBuilder fileName = new StringBuilder();

            for (int i = 0; i < files.Count; i++)
            {
                Windows.Storage.StorageFile file = files[i];

                // Get top level file properties.
                fileName.AppendLine("File name: " + file.Name);
                //fileProperties.AppendLine("File type: " + file.FileType);
            }

            this.lblResult.Text = fileName.ToString();

        }
    }
}


เพิ่มเติม การอ่านไฟล์จาก Storage หรือ Folder มาแสดงในหน้าจอ Apps

แสดงไฟล์จาก Pictures library
// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

for (int i = 0; i < files.Count; i++)
{
	StorageFile file = files[i];

	StringBuilder fileProperties = new StringBuilder();

	// Get top level file properties.
	fileProperties.AppendLine("File name: " + file.Name);
	fileProperties.AppendLine("File type: " + file.FileType);
}

แสดงคุณสมบัติของไฟล์
// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

for (int i = 0; i < files.Count; i++)
{
	StorageFile file = files[i];

	StringBuilder fileProperties = new StringBuilder();

	// Get file's basic properties.
	BasicProperties basicProperties = await file.GetBasicPropertiesAsync();
	string fileSize = string.Format("{0:n0}", basicProperties.Size);
	fileProperties.AppendLine("File size: " +  fileSize + " bytes");
	fileProperties.AppendLine("Date modified: " + basicProperties.DateModified);
}

แสดงรายละเอียดอื่น ๆ ของไฟล์
readonly string dateAccessedProperty = "System.DateAccessed";
readonly string fileOwnerProperty = "System.FileOwner";

...

// Enumerate all files in the Pictures library.
var folder = KnownFolders.PicturesLibrary;
var query = folder.CreateFileQuery();
var files = await query.GetFilesAsync();

for (int i = 0; i < files.Count; i++)
{
	StorageFile file = files[i];

	StringBuilder fileProperties = new StringBuilder();

	// Define property names to be retrieved.
	List<string> propertiesName = new List<string>();
	propertiesName.Add(dateAccessedProperty);
	propertiesName.Add(fileOwnerProperty);

	// Get extended properties.
	IDictionary<string, object> extraProperties = await file.Properties.RetrievePropertiesAsync(propertiesName);

	// Get LastDateAccessed property.
	var propValue = extraProperties[dateAccessedProperty];
	if (propValue != null)
	{
		fileProperties.AppendLine("Date accessed: " + propValue);
	}
	
	// Get FileOwner property.
	propValue = extraProperties[fileOwnerProperty];
	if (propValue != null)
	{
		fileProperties.AppendLine("File owner: " + propValue);
	}
}



.

   
Share


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


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


   


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

 
Local app data จัดเก็บไฟล์ลง Storage บน Windows Store Apps (C#)
Rating :

 
Roaming app data จัดเก็บไฟล์ลง Storage บน Windows Store Apps (C#)
Rating :

 
Temporary app data จัดเก็บไฟล์ลง Storage บน Windows Store Apps (C#)
Rating :

 
File Dialog และ Save จัดเก็บไฟล์ลง Storage ของ Windows Store Apps (C#)
Rating :

 
ดาวน์โหลดจัดเก็บไฟล์ลงบน Storage ของ Windows Store Apps (C#)
Rating :

 
การสร้าง Text file และการจัดเก็บบน Storage ของ Windows Store Apps (C#)
Rating :

 
MySQL ตอนที่ 1 : Windows Store Apps ติดต่อกับ MySQL Database (C#)
Rating :

 
MySQL ตอนที่ 2 : Windows Store Apps ทำการ Insert , Update , Delete (C#)
Rating :

 
MySQL ตอนที่ 3 : Windows Store Apps กับ MySQL ข้าม Host หรือ Server (C#)
Rating :

 
Windows Store Apps DataBinding - ListView / Database Binding (C#)
Rating :

 
Windows Store Apps DataBinding - ListBox / Database Binding (C#)
Rating :

 
Windows Store Apps Databinding - Retrieve Data Master and Detail (C#)
Rating :

 
Windows Store Apps DataBinding - GridView / Database Binding (C#)
Rating :

 
SQLite : Windows Store Apps ติดต่อกลับ SQLite Database (C#)
Rating :

 
Windows Store Apps การอ่าน Text file และ CSV และการ Data Binding(C#)
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 04
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 อัตราราคา คลิกที่นี่