Question

This is my first post, but I have been scouring the stack for how I might use a text file or a SQLite database in Xamarin for Visual Studio. All the examples I have found end up in java, or in the old Mono android code.

The jist of the application is to read GPS data, display it, and save the latitude and longitude along with some identifying info such as room number. I have the display portion of the app working nicely, so now I need to find a way to save the information, so that the data can be passed along to a future class to finish creating a campus navigation app.

Here are some of the related portions:

path = GetDir("MCCMapping/",FileCreationMode.Append).ToString();

I am not sure this is correct to set the directory for the file creation.

String location = String.Format("{0}|{1}|{2}|{3}", roomNum.Text, campus.SelectedItem.ToString(), current.Latitude.ToString(),
                                             current.Longitude.ToString());
                using (data_file = new StreamWriter(path+"/MCC_ROOM_DATA.txt", true))
                {
                    data_file.WriteLine(location);
                }

This is not quite working. I need to be able to access the .txt file from outside the application, or even implement a SQLite database to store the same information. I have all the necessary manifest permissions. My device does not have external storage, so it will emulate it if that is the route that must be taken.

Anything you might have to help me along would be appreciated. Thanks.

Update 1:

folder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            conn = new SQLiteAsyncConnection(System.IO.Path.Combine(folder, "MCC_ROOMS.db"));
            conn.CreateTableAsync<Room>().ContinueWith(t => {});

and this

public class Room
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }
    public string Campus { get; set; }
    [Unique]
    public string Room_Num { get; set; }
    [Unique]
    public decimal Room_Latitude { get; set; }
    [Unique]
    public decimal Room_Longitude { get; set; }
}

I am not quite sure what to put in the continue with portion. Also, would this allow me to access the table from a different application later? Thanks.

Update 2: Thanks to Jason, I have a working application. I used the external storage directory that is built in, and can then access the file if I turn usb debugging off.

Was it helpful?

Solution

Xamarin provides enumerations that help you get the folder paths of the folders that you are allowed to access. You cannot just write to arbitrary file paths.

var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var filename = Path.Combine(path, "mydata.txt");

Once you have the path, you can use normal file IO operations to read and write.

using (data_file = new StreamWriter(filename, true))
{
  data_file.WriteLine(location);
}

Xamarin's SQLite.Net component has documentation that demonstrates how to create and utilize a db from within your app.

string folder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
var conn = new SQLiteConnection (System.IO.Path.Combine (folder, "stocks.db"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top