Hello.
I’m trying to load text file by
var filePath = Path.Combine(Content.RootDirectory, "l1.dat");
using (var stream = TitleContainer.OpenStream(filePath))
{
text = File.ReadAllText(filePath);
}
It’s just for test, but I don’t know where to put l1.dat, because whatever i try (with content directory or not), I get
System.IO.DirectoryNotFoundException: Could not find a part of the path “/Content/l1.dat”.
First time I do something for Android so I have no idea what should I do with this.
Edit:
The main question is:
how to add a existing file with data to a project and what path to it will be on device after compile.
I believe all the stuff in an Android build deploys as an APK file, I’m not sure if there’s an actual Content directory (I could be wrong here, I’m still learning :D).
Having said that, you do get access to the file system on Android, just in a different directory and you can get it via Android.OS.Environment.ExternalStorageDirectory.
There’s other storage areas on Android as well, but I’m not sure how to use them offhand. Google knows though… 
Here’s an example showing how I log crash dumps in my game.
private void Handle_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
try
{
string baseFile = "CrashLog";
string fileName = string.Format("{0}_{1}.txt", baseFile, DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss-fff"));
string dir = Path.Combine(_appStorageDir, "Log");
if (!Directory.Exists(dir))
Directory.CreateDirectory(dir);
string path = Path.Combine(dir, fileName);
File.WriteAllText(path, e.ExceptionObject.ToString());
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Unable to log dump file.");
System.Diagnostics.Debug.WriteLine(ex.ToString());
System.Diagnostics.Debug.WriteLine("Unhandled exception:");
System.Diagnostics.Debug.WriteLine(e.ToString());
}
}
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
_appStorageDir = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.ToString(), "ArbitraryPixel", "CodeLogic");
AppDomain.CurrentDomain.UnhandledException += Handle_UnhandledException;
// Other code
}
// This work on my case ^ _^ y
// E.G: string[] m_TextLines = ReadAllTextLinesFromFile( “Content/TextData/l1.dat” , true );
/// <summary>
/// Read all text lines in a text file and returns an array of strings.
/// </summary>
/// <param name="filePath">File path to read.</param>
/// <returns>Returns an array of strings.</returns>
public static string[] ReadAllTextLinesFromFile(string filePath, bool IsAndroid )
{
string[] m_Result = null;
if ( IsAndroid )
{
using (SIO.Stream m_FS = XF.TitleContainer.OpenStream(filePath))
{
using (SIO.StreamReader m_SR = new SIO.StreamReader(m_FS))
{
var m_TextList = new SCG.List<string>();
//
while (!m_SR.EndOfStream) m_TextList.Add(m_SR.ReadLine());
//
m_Result = m_TextList.ToArray();
//
m_TextList = null;
}
}
}
else// Windows
{
m_Result = SIO.File.ReadAllLines(filePath);
}
return m_Result;
}
@DexterZ @Trinith
Thank you for answer! It will be very usefull, but now I also understand, that I give a wrong qustion
My problem is that, I don’t know how to add to project a file with some data and after it, where to find it on device.
I edit my example and make it more generic without my device setting and make it as a parameter ^_^y the sample above works perfectly on my case…
I’m not sure if you can… I would be interested to see if other people know anything 
Just put it in the “Assets” folder in your solution. There’s even a “AboutAssets.txt” on how to load that file 
2 Likes
Hmmm, interesting, thanks!