locked
Open and read from text file into a text box for Windows Store

    Question

  • I wish to open and read from a text file into a text box in C# for the Windows Store using VS Express 2012 for Windows 8.

    Can anyone point me to sample code and tutorials specifically for Windows Store using C#.

    Is it possible to add a Text file in Windows Store. This option only seems to be available in Visual C#.

    Thanks

    Wendel


    Wednesday, January 21, 2015 4:31 AM

Answers

  • Is there a reason you're not using VS2013 Express for Windows 8.1?

    Link to Windows 8.1 samples:
    https://code.msdn.microsoft.com/windowsapps/site/search?query=windows%208.1%20read%20file&f%5B0%5D.Value=windows%208.1%20read%20file&f%5B0%5D.Type=SearchText&ac=5


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by My2Wanzen Friday, January 23, 2015 11:31 PM
    Wednesday, January 21, 2015 1:20 PM
    Moderator

All replies

  • Is there a reason you're not using VS2013 Express for Windows 8.1?

    Link to Windows 8.1 samples:
    https://code.msdn.microsoft.com/windowsapps/site/search?query=windows%208.1%20read%20file&f%5B0%5D.Value=windows%208.1%20read%20file&f%5B0%5D.Type=SearchText&ac=5


    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    • Marked as answer by My2Wanzen Friday, January 23, 2015 11:31 PM
    Wednesday, January 21, 2015 1:20 PM
    Moderator
  • This is a simple sample for Read/Load Text file from IsolateStorage and Read file from InstalledLocation (this folder only can be read)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Windows.Storage;
    using System.IO;
    
    namespace TextFileDemo
    {
        public class TextFileHelper
        {
            async public static Task<bool> SaveTextFileToIsolateStorageAsync(string filename, string data)
            {
                byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(data);
                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
                var file = await local.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);
                try
                {
                    using (var s = await file.OpenStreamForWriteAsync())
                    {
                        s.Write(fileBytes, 0, fileBytes.Length);
                    }
                    return true;
                }
                catch
                {
                    return false;
                }
            }
    
            async public static Task<string> LoadTextFileFormIsolateStorageAsync(string filename)
            {
                StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
    
                string returnvalue = string.Empty;
                try
                {
                    var file = await local.OpenStreamForReadAsync(filename);
                    using (StreamReader streamReader = new StreamReader(file))
                    {
                        returnvalue = streamReader.ReadToEnd();
                    }
    
                }
                catch (Exception ex)
                {
    
                    // do somthing when exception
                }
                return returnvalue;
            }
    
    
            async public static Task<string> LoadTextFileFormInstalledLocationAsync(string filename)
            {
                StorageFolder local = Windows.ApplicationModel.Package.Current.InstalledLocation;
                    
                string returnvalue = string.Empty;
                try
                {
                    var file = await local.OpenStreamForReadAsync(filename);
                    using (StreamReader streamReader = new StreamReader(file))
                    {
                        returnvalue = streamReader.ReadToEnd();
                    }
    
                }
                catch (Exception ex)
                {
    
                    // do somthing when exception
                }
                return returnvalue;
            }
        }
    }

    show how to use it as below
           async private void Button_Click(object sender, RoutedEventArgs e)
            {
                string txt =await  TextFileHelper.LoadTextFileFormInstalledLocationAsync("TextFile1.txt");
                Debug.WriteLine(txt);
            }


    在現實生活中,你和誰在一起的確很重要,甚至能改變你的成長軌跡,決定你的人生成敗。 和什麼樣的人在一起,就會有什麼樣的人生。 和勤奮的人在一起,你不會懶惰; 和積極的人在一起,你不會消沈; 與智者同行,你會不同凡響; 與高人為伍,你能登上巔峰。

    Wednesday, January 21, 2015 2:00 PM
  • Hi Matt

    I developed my first app using VS Express 2013 and was quite happy with my achievement. Unfortunately most of the users are still using Windows 7, with a few on Windows 8.0. Only a handful on 8.1. I believe Windows 10 will be released April + this year with free upgrade for Windows 7 and 8.0.

    I think I’ll take your advice and stick to VS Express 2013. Only a few months to go before users will be able to use my app.

    Thanks for the code, much appreciated.

    Wendel

    Friday, January 23, 2015 11:32 PM