Reading files in xamarin forms as a src folder - c#

I'm trying to read a file.txt from a src folder inside my project. But when I write File.ReadAllText("PATH") I don't know the path for the file. The file is also inside a views folder.
string text = File.ReadAllText();
This is my project folder:
I'm trying to read from popuppage.xaml to src/.txt file

Xamarin.Forms code runs on multiple platforms - each of which has its own filesystem. Previously, this meant that reading and writing files was most easily performed using the native file APIs on each platform. Alternatively, embedded resources are a simpler solution to distribute data files with an app.
You can check this document to learn more about File Handing in Xamarin Forms https://learn.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/files?tabs=windows

Related

Access to an embeded resource file doesn't work on Xamarin but on Windows App

I added an excel File (template.xlsx) to the project resources as an embedded resource. In my code I want to save that file to disk.
On Windows it works well with:
File.WriteAllBytes("test.xlsx", Properties.Resources.template);
But in my Xamarin.Forms project exactly the same line does not work because "Properties" is unknown in the current context.
I searched half a day in the net but all hints I got don't work either or confuse me even more. It seems I am missing an essential piece of base knowledge here. Could somebody tell me if I can easily access an embeded File in Xamarin similar like I do on Windows?
The difference I can see in the two test projects is:
In the project explorer of the windows project I can see Properties->Resources.resx. However, the template file is not located there but in Resources->template.xlsx
In the project explorer of the Xamarin project there is no Resources.resx in the Properties folder.
My file is again located under Resources->template.xlsx
You have to read that file in your code and then use it to write that file to file storage (application sandbox) of the platform. You can save files to that location, although you may not easily be able to access that file outside the scope of the app (say from another application).

How to create text file in .NET project folder, not in bin/Debug folder where is by default

I have a very simple .NET console application in Visual Studio. I am trying to write some words into a text file.
using (StreamWriter file = File.AppendText("log1.txt"))
{
file.WriteLine("Hello from the text file");
}
If the file does not exist, the application creates it in the autogenerated folder bin/Debug.
Is there a way to create this file in the project's directory, where I have .csproj file?
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes, but this can only be done while you are working on your project. Once you are done developing it and try to publish it you won't have access to the location where you have .csproj file, because after publishing you can install it on any PC and it wont have the project you are working on.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug?
No, I assume by real-world applications in your context you mean a published project '.exe' that you can run on any PC. Windows provides you three Data folders that you should use when writing your program so that it works smoothly after publishing:
User Data
Roaming User Data
All User Data
You can acess the above folders in .NET application using the Environment.SpecialFolder:
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
As per your given code, try this :
var fileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"log1.txt");
using (StreamWriter file = File.AppendText(fileName))
{
file.WriteLine("Hello from the text file");
}
This way you will be able to publish your program and it will still work smoothly without hard-coding the path as you were doing previously.
That's why .NET creates them there firstly?
If you don't specify a complete path, and just the file name .NET looks into the working directory of the executable, which in this case is bin/Debug
Is there a way to create this file in the project's directory, where I have .csproj file?
Yes. As explained here (second answer) you can use the post-build event to write down the value of $(ProjectDir) in a text file (using command echo $(ProjectDir) > ..\..\projectdir.txt). This macro contains the directory of your .csproj. This command will create the file projectdir.txt with your project directory after a build process so you read this file contents in your code and use what is inside it to pass to File.AppendText as the base directory to create your file log1.txt.
And more important, in real-world applications, when you work with files, you keep them in bin/Debug? That's why .NET creates them there firstly?
That depends on what you want to do. In your case the code creates the file at bin/Debug because that is where your executable are being executed. When you omit the full path to File.AppendText and just pass "log1.txt" as argument, it will create the file in the same folder as the executable are at. If you want a different folder you should specify the folder here (e.g. File.AppendText("C:/log1.txt") will create the file at C:/.
You can create the text file in the root of your project and use copy always to have them in the same place as your executable. If this is just a readonly text file then it's OK because windows doesn't allow you to modify the files reside in Programs folder in OS drive.
If you want your code to modify these text file then you need to put them in appdata folder. In real world example I did this on many project. All the database work my winforms, WPF application need goes in AppData folder.

Accessing StreamingAssets resources from C++ DLL in Unity on Android

I have developed a C# application in Unity for Android. Additionally, I built a custom external C++ DLL which is properly loaded and accessible from the C# code. I have also added some .xml files into the StreamingAssets folder which works as expected as I can see those files loaded into the .apk.
Currently my problem is that I'm not able to access such xml files from my C++ library. On one hand I'm not sure how to do it correctly, on the other I tried multiple unsuccessful approaches including trying to understand in which cwd was my DLL loaded and then navigate to the files I needed (which didn't work as I constantly receive an access denied error).
EDIT:
Your replies actually put me on the right track and I solved the issue as follow:
During the OnStart() phase of my application, I use the UnityWebRequest class to extract from the JAR file the required files and then I save them in the Application.persistentDataPath
After that, I pass to my DLL the new path to each file which allows me to access them without any issue.
On Android the StreamingAssets are in a jar. That may well be the reason why you can't access them, but as you can see in the manual you can use the WWW class to retrieve files there.
On Android, the files are contained within a compressed .jar file
(which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file, you need to use additional software to see inside
the .jar archive and obtain the file.
(https://docs.unity3d.com/Manual/StreamingAssets.html)
On Android, the files are contained within a compressed .jar file
(which is essentially the same format as standard zip-compressed
files). This means that if you do not use Unity’s WWW class to
retrieve the file, you need to use additional software to see inside
the .jar archive and obtain the file.
A JAR is a package file format typically used to aggregate many Java class files and associated metadata and resources into one file for distribution. JAR files are archive files that include a Java-specific manifest file. They are built on the ZIP format and typically have a .jar file extension.
This means that you can work with jar files the same way as you do with zip files.
There is a fork for minizip with examples on how to use it.

Get file paths for files in Centennial app

When converting an app to a Windows Store app, we add the files as mentioned here. But now - how do we access them? What's their path?
They're not in the special folder created for the package.
You can create a folder structure in your project where you can include the files and then since you are already crossing the Centennial Bridge, you can start using the Windows.Storage.StorageFile WinRT API to interact with files.
For example, you can use: StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///folderUnderYourAppRoot/image.bmp")); to get the file which is under the "folderUnderYourAppRoot". This is the generic guidance: https://msdn.microsoft.com/en-us/windows/uwp/files/quickstart-reading-and-writing-files, so you can review it further...

Add Text file to assets folder after export apk file with C#

Just assume some persons's information exist in Asp.net developed application.
and also another application have been written to display the data of mentioned persons for android(by Eclipse).
How could make change in "APK" file after adding data for new Person by Asp to get the data of mentioned person from website.
How mentioned person ID could be placed as text file in assets folder while APK SIGN dont interfere it.
I want have personal apk for each person and personal apk created(or modified) by Asp website(This APK contain a text file with MyPersonId and it added to ASSETS folder with C#).
I was trying to do the same, and i'm almost there.
Basically i used MIT AppInventor to create an APK for Android. On it, i created a TXT file in the assets folder. The MIT AppInventor compiled the code to create the APK file for distribution.
I than placed the APK in a folder, owned by an ASP.net page. The goal would be to open the APK file (as a Zip, currently using the ZipSharpLib), locate the "info.txt" on the assets folder, and replace it for another.
My problem so far is on the "re-pack" ou "re-compress" the copy of the file; android is not recognizing it as a valid APK.
So, up to the "decompress" the APK, run every entry, locating the one that is the asset to replace i'm good (just use ZipSharpLib, classes ZipFile and ZipEntry - they show you how on their site). But on the "compress" a new changed file, i'm failing.
To do this kind of things you would need help of a decompilation and recompilation tool such as ApkTool. You can't just zip back the files and hope android recognize zip format as an apk format immediately. There's a signing process included to make it a "real apk" again

Categories

Resources