Add a text file as a resource in Visual Studio 2022 - visual-studio

I am creating a WebView2 app (WPF using C#) and I want to have a index.html file as a resource.
The idea is that when I create a project, a new folder is created.
This folder will contain an index.html file for the WebView control to load.
The index.html file then points to project specific data as relative paths.
I would like to have a file embedded in my resources that I can use at runtime like this
`void newProject()
{
string strHTML = /** code to read from resource file **/
createFile(projDir + "\\index.html") /** code to create a new file **/
/** code to write strHTML string to new index.html file **/
}
`
I know I can create a static string to do this, but editing is easier if I have the resource as a file.
Thanks in advance.
D
Update:
I put the file into the resources. Then, I added this code
var assem = Assembly.GetExecutingAssembly();
String[] resources = assem.GetManifestResourceNames();
html = assem.GetManifestResourceStream("index.html")
The resources string array always has two elements regardless of what I have in my resources file.
"WebView2Testing.g.resources"
"WebView2Testing.Properties.Resources.resources"
which has nothing to do with my file.
GetManifestResourceStream("index.html") returns null.
FYI I am using .net6 and WPF Windows app.
Any more ideas?

Related

Problems automatically converting to .txt when downloading xlsx files (excel files) within a Java (SpringBoot) server

When downloading a .xlsx file on a server, it automatically converts to .txt when asked for a save path.
You are currently working with SpringBoot, and if you click the download link through the tag in the view, click the .xlsx file.
The method of loading from the specified path.
The problem is that if you specify the href path for tag a as resource/static within the project, the .xlsx file will be downloaded without any problems.
However, if you route to a folder outside the project, the .xls file is downloaded normally, but. The xlsx file is automatically converted to .txt.
The path imported from the external folder is in the form of a request to the controller to return the actual file.
Below is the code that returns the file from the controller.
I'd like to ask those who know about this problem.
#GetMapping
public byte[] file(HttpServletRequest request) throws Exception {
FileInputStream in = new FileInputStream(new File().getPath() + "/filemanager/"
+ request.getRequestURI().split(request.getContextPath() + "/filemanager/")[1]);
byte[] image = IOUtils.toByteArray(in);
in close();
return image;
}

How to access file stored in shared project in Xamarin Forms?

I have a folder called Documentation inside the shared project, named App2 in this case. How do I access the files stored inside the Documentation folder? Attached image below shows the project structure.
Visual Studio Solution Page
I have tried out following commands but they aren't working :
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
AppDomain.CurrentDomain.BaseDirectory
System.Reflection.Assembly.GetExecutingAssembly().CodeBase;
If it's troublesome to access the file in that folder, I'm open to hearing other alternatives.
This is how I have done it for JSON files in my shared project (using PCL). As Jason pointed out in the comments, if you are using .NET Standard, you can simply define the GetSharedFile method in your shared project instead of creating platform specific references.
Add the file to the shared project and set it as Embedded Resource
Create an IFileHelper interface in your shared project
public interface IFileHelper {
Stream GetSharedFile(string fileResourceName);
}
Create a new FileHelper class in each project (Android and iOS) with the following
public class FileHelper : IFileHelper {
public Stream GetSharedFile(string fileResourceName) {
Type type = typeof(IFileHelper); // We could use any type here, just needs to be something in your shared project so we get the correct Assembly below
return type.Assembly.GetManifestResourceStream(fileResourceName);
}
}
Add a documentation handler class in your shared project. Something like below (make sure to change the App namespace to match yours):
public class Documentation {
private const string ResourcePath = "App.Documentation.index.html"; // App would be your application's namespace, you may need to play with the Documentation path part to get it working
public string GetDocs() {
IFileHelper helper = DependencyService.Get<IFileHelper>(); // Your platform specific helper will be filled in here
Stream stream = helper.GetSharedFile(ResourcePath);
using (stream)
using (StreamReader reader = new StreamReader(stream)) {
return reader.ReadToEnd(); // This should be the file contents, you could serialize/process it further
}
}
}
I wrote this mostly by hand so let me know if something is not working. If you cannot load your file, I suggest trying to put it into the root of your shared project and then changing ResourcePath in the code above to the following (again using your app's namespace instead of App):
private const string ResourcePath = "App.index.html";

Not a valid resource name of assembly

I'm using Xamarin Studio and GTK#(.NET). I have the following problem:
I've made an app, and I need to(dinamically) download web images(favicons) and display them in a Gtk.Image widget. When I try to do this, I get the following error:
System.ArgumentException: 'myfile.ico' is not a valid resource name of assembly 'myApp, Version=1.0.6742.24722, Culture=neutral, PublicKeyToken=null'.
at at Gdk.PixbufLoader.InitFromAssemblyResource(Assembly assembly, String resource)
at at Gdk.PixbufLoader..ctor(Assembly assembly, String resource)
at at Gdk.Pixbuf..ctor(Assembly assembly, String resource)
at at Gdk.Pixbuf.LoadFromResource(String resource)
It works if i add the favicon, after download,in the resource folder, BUT I need to make this dinamically(if I add a new site, I want to see it's icon there..).
The code:
using (WebClient client = new WebClient())
{
client.DownloadFile(new Uri("https://google.com/favicon.ico",
Directory.GetCurrentDirectory() + "\..\..\Res\google.ico");
}
faviconImage.Pixbuf = Gdk.Pixbuf.LoadFromResource("myApp.Res.myfile.ico");
The image is in the folder(i can see it, it's everything ok) but i cannot use it if i don't include it(manually) in the resource folder....
Please help me :'(
Pixbuf.LoadFromResource only loads image data from an embedded assembly resource. You can not add resources to assemblies after they are created.
To create a Pixbuf from a file, use one the its .ator that supports either a file path to the image or one that uses a file stream.
Something like:
var filePath = Path.Combine(Directory.GetCurrentDirectory(), "myDownloadIcon.ico");
var newPixBuf = new PixBuf(filePath):
faviconImage.Pixbuf = newPixBuf;

Handling dynamic created files in play 2

I wrote a small app that creates downloadable pdf files with play 2.0
I want to serve them to the public. On my development environment I created a folder in the
/assets/ folder and everything worked nice.
Now, when switching to production, I figured out that play always deployed those files behind my back.
Do I really have to write a own controller to serve those files or what is the way here ?
I've also had problems trying to serve files created dynamically with the assets controller. I don't know if it's for some kind of caching but I ended up writing my own controller, and now it woks perfectly.
I mean, I use the Assets controller for regular public files and for my dynamic generated files I use this one:
public class FileService extends Controller {
static String path = "/public/dynamicfiles/";
public static Result getFile(String file){
File myfile = new File (System.getenv("PWD")+path+file);
return ok(myfile);
}
}
And the routes would be like this:
GET /files/:file controllers.FileService.getFile(file: String)
GET /assets/*file controllers.Assets.at(path="/public", file)
It works great for me

How to read Asset Files using VS & Monodroid

I've been trying to implement this example using C# and Monodroid, but am having difficulties reading and writing an Asset file:
http://docs.xamarin.com/android/advanced_topics/using_android_assets
I am using the emulator, not a device.
First of all, I am having trouble finding the namespace for Assets.Open. What I ultimately found was
const string lfn = MyAssetFile.txt;
System.IO.StreamReader(Android.Content.Res.Resources.System.Assets.Open(lfn);
Is this the correct namespace?
Second of all, my Asset file is marked as AndroidAsset and "Copy Always" in the VS "Properties" pane, but my attempts to read the file always fail (File Not Found) using this statement:
string settings = "";
using (StreamReader sr = new System.IO.StreamReader (Android.Content.Res.Resources.System.Assets.Open(lfn))) settings = sr.ReadToEnd();
Do I have my VS settings wrong so that the asset file not being copied onto the emulator, or is it being copied OK but my code to open/read it is wrong?
You must use:
const string lfn = "MyAssetFile.txt";
string settings = string.Empty;
// context could be ApplicationContext, Activity or
// any other object of type Context
using (var input = context.Assets.Open(lfn))
using (StreamReader sr = new System.IO.StreamReader(input))
{
settings = sr.ReadToEnd();
}
If I remember correctly, as I haven't got an IDE at the moment, Android.Content.Res.Resources.System.Assets references the Android assets not your project assets. You want to use your projects assets so you need to use the AssetManager from your Activities or Contexts.
For example: the Activity class has a property called Assets. This is what you use. OR you use a View's Context.Assets property.
Or just simply add at the top:
using System.IO;
and it will work.
Xamarin team forget sometimes to mention using stuff.

Resources