1

I am currently creating a CSV file and trying to write the contents of my SQLite database to it. Unfortunately I keep getting the "Sharing Violation on path" error. It errors on the line where I try to WriteAllText.

Below is my current code:

var fileName = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "pullList" + pullMonth + ".csv";

using (var fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None))
{
    //Write your file here
    //fs.WriteLine("Hello");
    foreach (var listing in allTables)
    {
        File.WriteAllText(Path.Combine(fileName), string.Format("{0}," + System.Environment.NewLine, listing.ComicTitle));
    }
}
Freiza1991
  • 71
  • 1
  • 2
  • 9

1 Answers1

1

you are creating a stream, but not using it to write. Since the stream is creating the file with FileShare.None, when you try to write using a different file handle, it throws the sharing exception

try this instead

var filename = Path.Combine(Android.OS.Environment.ExternalStorageDirectory, "pullList" + pullMonth + ".csv");

StringBuilder s = new StringBuilder();

// build the data in memory
foreach (var listing in allTables)
{
    s = s.AppendLine(listing.ComicTitle);
}

// write the data all at once
File.WriteAllText(path,s);
Jason
  • 86,222
  • 15
  • 131
  • 146
  • I tried this and i get "Argument 2 cannot convert from System.Text.StringBuilder to string File.WriteAllText(fileName,s); – Freiza1991 Aug 04 '17 at 04:03
  • Okay i just converted it to a String and it worked! Thank you so much. Something that was this simple took me forever haha. No violation error and all titles are written in separate lines in CSV. – Freiza1991 Aug 04 '17 at 04:07