0

I'm making an app that need to write to a txt file for remember some settings in c#. I used that type of code:

class Preferiti
{
    public static string[] Read()
    {
        string path = "Assets/mytxtfile.txt";
        using (StreamReader sr = new StreamReader(path))
        {
            string[] values = sr.ReadToEnd().Split(Environment.NewLine.ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
            return values;
        }
    }

    public async static void Write(string mod)
    {
        string[] try = Read();
        bool write = true;
        for (var i = 0; i < try.Length; i++)
        {
            if (try[i] == mod)
            {
                write = false;
                break;
            }
        }
        if (write)
        {
            string path = "Assets/mytxtfile.txt";
            using (StreamWriter outfile = new StreamWriter(path, true))
            {
                await outfile.WriteLineAsync("" + mod);
                MessageBox.Show("Added to list");
            }
        }
        else
        {
            MessageBox.Show("It exist in the list");
        }
    }
}

When i try it on debug on my device, it works without any problems, but if i publish it on the store it read with any problems the file, but on the moment of write on it crash and exit form the app.

Can you help me to resolve that type of error?

Thank you very much

I post the image of the correction that you suggest to me... why do that to me?

Problem

Roberto
  • 135
  • 2
  • 14
  • What error does it throw? – MattR Feb 13 '14 at 12:17
  • On a related note, When using streamReader you should always check if your file exists before trying to read it – Svexo Feb 13 '14 at 12:42
  • I really don't know what is the error because it's come on the published version. On debug version all works without problems. The reading proces it's working without problem... I have problems when i try to write on the txt file. – Roberto Feb 13 '14 at 13:11

1 Answers1

0

I guess you are not copying the original file when publish the release version.

In Visual Studio, double-click mytxtfile.txt to open the Properties window. In the Properties window, set attribute "Copy to Output Directory" to "Copy always".

etaiso
  • 2,736
  • 3
  • 26
  • 38
  • So now make sure you have the `Assets` folder and the `mytxtfile.txt` in the application folder (where your `.exe` resides). If that's the case, I would add MsgBoxes to find the exact line where it fails. – etaiso Feb 13 '14 at 13:17
  • I think thats the problem is because the file is in ReadOnly, but i can't find the proprety for remove that... – Roberto Feb 13 '14 at 13:20
  • Look in [this](http://stackoverflow.com/questions/7399611/how-to-remove-a-single-attribute-e-g-readonly-from-a-file). – etaiso Feb 13 '14 at 13:24
  • Yep, but where to had that code to my code? And it's talk about File.GetAttributes(path) and say me taht this dosn't exist! – Roberto Feb 13 '14 at 14:04
  • Add `using System.IO;` – etaiso Feb 13 '14 at 14:12
  • There is... but still doesn't exist – Roberto Feb 13 '14 at 14:14
  • @Princess what error do you receive? is the path incorrect ? – etaiso Feb 13 '14 at 14:21
  • Path is correct because from the same path read a pre input string... After publish the app i can't see whats the error recived because it's working on device... i only know that if i perform the read function work and do the the stuff should do... when i try to write on this txt file the app crash and go out. I can't view the error message! But i'm sure that the path it's correct (in debug mode all work perfectly!) @etasio – Roberto Feb 13 '14 at 14:42