I'm using the MVVM structure and I wish to read a csv file, which I've added in the same folder as the viewmodel. I've tried to use the second answer in this question, but my variable var is null, so it doesn't seem to find the file. Beneath is an image of the folders and what I've done so far.
Can anyone help me get the destination of the file so I can use it as a stream?
You need to provide a path relative to your package, thus it should work if done like this:
var dest = App.GetResourceStream(new Uri(#"StartUpViewModels/" + destination, UriKind.Relative));
Here is also reference to answer to similar question.
Remember also to check if your resource file added to package has Build Action set to Content, otherwise if it's set to None, your file won't be added.
Related
I am at a complete loss, there seems to be a stark lack of up-to-date documentation about how to load a spritesheet from SpriteFactory with the Extended Content.Load<>() extension. I have tried following all of the other posts on here about how to do it but whenever I do:
var spriteSheet = Content.Load(“sheet.sf”, new JsonContentLoader());
MonoGame constantly tells me it could not find the file, yet I have added it to the /Content directory and I can see it get converted to an .xnb in the build folder, so I am totally confused how it could be not finding the file.
I have added the necessary headers for Content, Sprites, and Serialization so this line should work yet for some reason Content can’t find the file. I have also already made sure I added a reference to the MonoGame.Extended.Content.Pipeline.dll in the Content.mgcb file so everthing is already setup it’s just getting Content to actually find the file.
Does anyone have any idea why it may not be able to find the file? I am on day 3 and my wife is already packing her bags I’ve complained so much about this.
Thank you!
I have a script lets say in folder 'root'. I have a folder inside root called 'Images', and inside images an image called 'logo.png'.
root
script.cs
Images
logo.png
I have the following code to create a section:
Document document = new Document();
Section section = document.AddSection();
And I have tried this:
section.AddImage("../../Images/logo.png");
and this:
section.AddImage("Images/logo.png");
to try and add an image, but both return 'image not found'.
I've already tried updating MigraDoc to v1.5 but it didn't seem to work.
The "script" gets compiled and you get an assembly that is executed. You are using relative paths and by default the image will be searched relative to the working directory of the process which most likely is relative to the folder with the assembly. With ASP.NET things can be even more complicated.
Tip: The DocumentRenderer class has a WorkingDirectory property. If you set it then images with relative paths will be searched relative to this working directory. Set it to "root" and the second path "Images/logo.png" should work.
Forgive me because I realize the age of this thread before providing an answer, but I too had the same issue that the accepted answer was unable to resolve.
In C# you are able to use HostingEnvironment.MapPath to reference relative paths. In your case, it'd look like:
section.AddImage(HostingEnvironment.MapPath("~/Images/logo.png"));
I have a file I need to access at runtime, I've included it in my project and set it up as embedded resource (it's actually a source file, I changed the extension to .cs.txt to get around VS trying to compile it. That shouldn't matter, but I'm mentioning it anyway just in case).
When I try getting the file
var assembly = Assembly.GetExecutingAssembly();
Stream stream = assembly.GetManifestResourceStream(resourceName);
I get a null. I've made sure I'm using the Namespace.Folder.Filename notation, but that didn't help. It appears the file is actually not there, because when I call
assembly.GetManifestResourceNames();
I get an empty string array. Any idea what could be the case?
I appreciate this is an old thread but what I found this morning might be useful to others.
I had a resource where the filename had multiple dots in it...
example filename: data.txt.dat
var resources = asm.GetManifestResourceNames(); // None found (empty array)
renamed to data.txt (still just an embedded resource in the project configuration
var resources = asm.GetManifestResourceNames(); // Entry found ("Assembly.Namespace.data.txt")
So maybe there is some limitation around multiple . characters in the name
So I got around this by using the VS resource manager. Now I can access the file directly like this:
MyNamespace.Properties.Resources.MyFile
I'd recommend this approach to anyone, as it seems not only much cleaner, but safer as well. Thanks Hans Passant for the advice.
Based on this pull request (https://github.com/dotnet/msbuild/pull/5824) you can add WithCulture="false" in your csproj on your EmbeddedResource tag :
<EmbeddedResource Include="a.cs.b" WithCulture="false"/>
It is working for me
I am building a game which loads its map from a text file.
While creating the parts that handle maps, I simply kept the text file in the content folder and fetched it by its Windows filepath. This won't work for proper deployment (or even running the game from different drives) because it requires that the filepath be exactly the same.
I looked around for a way to include the text file the same way I would a Texture2D, but I cannot find any class that allows me to use it. Some answers to other questions suggested that I just use the text file from my content folder? How would I do that? My program's name is IslandQuest (placeholder; it doesn't even involve an island) so would I place the text file in the IslandQuestContent folder generated by XNA Studio? How would I access it from there such that its filepath doesn't depend on the drive configuration of a computer?
Thanks for any help.
This may not be the best way to do this but just looked back at what I did in my first year at university with XNA,
I added my txt file to the contents folder. Then in the properties for the file (select it in the solution explorer and view properties window) there should be "Copy to Output Directory", make sure this is copy if newer.
Then its just a case of
string.Format("Content/{0}.txt", filename)
I do think this can be improved perhaps by the following but it is untested
Path.Combine(Content,filename +".txt");
In my case I was reading XML file files from a data folder in my main project.
So under my project in Solution explore I had this set up:
WindowsPhoneGame1
...
data/
content.xml
Game1.cs
Program.cs
etc...
Where properties for content.xml were Build Action: Content and Copy to Output Directory: Copy always
In the class that read the file I used TitleContainer.OpenStream Method which according to the docs:
Returns a stream to an existing file in the default title storage
location. .... The stream returned is read-only. The file to be read must already exist, or this method will fail.
My example code
//open stream
Stream stream = TitleContainer.OpenStream("data/content.xml");
//do something with it...
Create a "Content" folder in your main project.
Put the files that cannot be put in the Content project in there.
Be sure to set all your content Build actions to Content and Copy Always.
The Content folder from your main project and the content in the content project will end up in the same folder when built.
The file path would still be "Content/file.ext" or whatever.
With Ziparchiv.CreateEntry I add files to a zipStream
But is there a way to create also folders?
I want to zip a folder tree to a zip. Currently I can't see a way to have the sametree in the zip file.
The only soultion I currently have , to not loose the information, is to create somekind of reference file which contrains the relative pathes.
Is there a way to have folders in WinRT ZipArchivs?
Just found the solution when writing the sample code.
Because i didn't find this here i consider adding the question also hoping that this will help someone.
If this one is a duplicate feel free to delete it or link it to the pre existing question :-)
When calling CreateEntry the path can be prefixed to the filename.
The following code sets foo as the folder in the zipfile in the archive
zipArchive.CreateEntry(#"foo\" + fileToCompress.Name,CompressionLevel.Optimal);