locked
windows app c# writing text to text file

    Question

  • Guys

    Having real problems with writing to a text file using following code

                    

    using HCP1.Common;
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using Windows.UI.Xaml.Media.Imaging;
    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 Windows.Storage;
    using System.Threading.Tasks;
    using System.Text;
    using System.Diagnostics;

    private void imageview_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
            {
                var point = e.GetPosition(imageview);
                var  p1 = (int)point.X;
                var p2 = (int)point.Y;
                string stringVal1;
                string stringVal2;   
                stringVal1 = System.Convert.ToString(p1);
                stringVal2 = System.Convert.ToString(p2);
                Text3.Text = stringVal1;
                Text4.Text = stringVal2;

                if (p2 > 210 && p2 < 235 && p1 > 339 && p1 < 367) { 

                    string dave = Environment.NewLine + Text3.Text + "," + Text4.Text;
                    string path = @"C:\myfile.txt";
                    StreamWriter sw = new StreamWriter(path);
                    string bufferOne = dave;
                    sw.Write(bufferOne);
                    sw.Close();
                    sw.Dispose();
                    };
                        }

    When a double tap is done it should save the contents of a textblock to a file but I have three errors

    The best overloaded method match for 'System.IO.StreamWriter.StreamWriter(System.IO.Stream)' has some invalid arguments (This is at the Streamwriter Line)

    cannot convert from 'string' to 'System.IO.Stream' (At the same line)

    'System.IO.StreamWriter' does not contain a definition for 'Close' and no extension method 'Close' accepting a first argument of type 'System.IO.StreamWriter' could be found (are you missing a using directive or an assembly reference?) (This is at the Close line)

    As you can see I have SYstem.io reference

    Any ideas where im going wrong?

    Any help appreciated

    Mark

    Saturday, January 31, 2015 10:17 PM

Answers

  • As the errors suggest, you are using methods which aren't available to Windows Runtime apps. Apps do not in general use file paths directly, they use StorageFile objects, and apps don't have general access to the file system and can't read or write outside of their app data and install directories without the StorageFile broker.

    For examples see Quickstart: Reading and writing files 

    Sunday, February 1, 2015 5:06 AM
    Owner

All replies

  • As the errors suggest, you are using methods which aren't available to Windows Runtime apps. Apps do not in general use file paths directly, they use StorageFile objects, and apps don't have general access to the file system and can't read or write outside of their app data and install directories without the StorageFile broker.

    For examples see Quickstart: Reading and writing files 

    Sunday, February 1, 2015 5:06 AM
    Owner
  • If you are developing App for Windows Phone (Silverlight) then code is this :

     IsolatedStorageFile localFile = IsolatedStorageFile.GetUserStoreForApplication();
    
                if (!localFile.DirectoryExists("ApplicationFolder"))
    
                    localFile.CreateDirectory("ApplicationFolder");
    
                using (var fileStreamCreator = new IsolatedStorageFileStream("ApplicationFolder\\myownfile.txt",System.IO.FileMode.OpenOrCreate,localFile))
                {
                    using(var fileStreamWriter = new StreamWriter(fileStreamCreator))
                    {
                        fileStreamWriter.WriteLine(txtRead.Text);
                    }
                }

    for Further Details please see this:

    click here 

    and 

    click here

    Please If found useful mark as answer.

    Muhammad Asad (MSP)


    Asad

    Sunday, February 1, 2015 8:36 AM
  • Hi Asad,

    You're in the wrong forum :) Silverlight is not supported for Windows Store apps. Mark's original problem was using API which aren't available to Windows Runtime apps. IsolatedStorage would compound that problem.

    --Rob

    Saturday, February 7, 2015 3:53 AM
    Owner