Reputation: 2469
I have a Xamarin Forms application in which I want to read a txt file. In my android project the file is placed in the assets folder with these properties :
Build action : Android Asset
Copy options : Allways copy.
And I am capabale to read the file with this code :
public string GetAppSetting(string name)
{
string retVal = "";
try
{
using (StreamReader sr = new StreamReader(AndroidHelper.Assets.Open("AppSettings.txt")))
{
var result = sr.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
foreach (var line in result)
{
if (line.StartsWith(name + ":"))
return line.Split(':')[1];
}
}
}
catch (Exception ex)
{
ParseError(ex, "GetConnectionString");
}
return retVal;
}
On the other hand, in the uwp project, I have allways the exception File Not Found!
I put the file in the root of the project and tried to put in the assets folder too. It doesn't change the result. File Not Found!
Build action : Content (Tried other options too).
Copy options : Allways copy.
Here is my code to read the file :
private async Task<string> ReadFileAsync(string name)
{
string retVal = "parameter not found";
try
{
Windows.Storage.StorageFolder storageFolder = Windows.Storage.ApplicationData.Current.LocalFolder;
Windows.Storage.StorageFile sampleFile = await storageFolder.GetFileAsync("AppSettings.txt");
string str = await Windows.Storage.FileIO.ReadTextAsync(sampleFile);
foreach (var line in str.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries))
{
if (line.StartsWith(name + ":"))
return line.Split(':')[1];
}
}
catch (Exception ex)
{
MainHelper.ParseError(ex, "UWP readFileAsync");
}
return retVal;
}
What is wrong wiht my code ? Or where should I place the AppSettings.txt ?
Upvotes: 0
Views: 184
Reputation: 131
In the UWP MainPage Xaml.cs change the LoadApplication:
namespace MyApp.UWP
{
public sealed partial class MainPage
{
public MainPage()
{
this.InitializeComponent();
LoadApplication(new MyApp.App(Windows.Storage.ApplicationData.Current.LocalFolder.Path));
}
}
}
Then in the app.Xaml.cs add:
public static string path;
public App(string paTh)
{
InitializeComponent();
path = paTh.ToString();
MainPage = new ContentPage();
}
Then use App.path as the Windows storage path.
Upvotes: 0
Reputation: 530
For UWP, I suggest you to set the file as an "Embedded Resource" (Build Action)
To ensure your file is loaded as an embedded resource at runtime, you can enumerate all your assembly's resources like this:
var resourceNames = anotherSameResAssemblyInstance.GetType()
.GetTypeInfo().Assembly
.GetManifestResourceNames();
Then, you can open the file as a stream like this:
string myFileResourceStream = "{YourAppNamespace}.AppSettings.txt";
var myFileResourceStream = someAssembly.GetManifestResourceStream(name);
Where 'YourAppNamespace' is the namespace in your app where is embedded the file. To get the correct full name, just check all values returned by GetManifestResourceNames()
method.
Example:
var myFile = resourceNames.Where(x => x.Contains("AppSettings.txt")).FirstOrDefault();
if (myFile != null)
{
var str = this.GetType().GetTypeInfo().Assembly.GetManifestResourceStream(myFile);
}
It should now work.
Upvotes: 1