I'm creating SpecFlow tests for an application that uses an xml settings file (example, C:\ssis\mySettingsFile.xml) to run. In one test, I want to save the file to disk, and then add that file to my project resources and clean up the disk location. Then, another test will unpack the resource to a temporary directory and use it from there.
I'm clear about the unpacking part, but is there a way to programatically pack the file into a project resource rather than manually adding it to the project using the VS GUI and marking it as an embedded resource?
I know this is wrong, but I'm thinking something along the lines of:
string myPath = "C:\ssis\mySettingsFile.xml";
TestHelper.ResourceDirectory = "$\...\...\Project.Folder.Resources";
myResource = TestHelper.PackResource(myPath);
myResource.IsEmbeddedResource = true;
...where PackResource method saves the file to the project resources.
Thanks in advance!
you can create a Resource Folder and add your xml to it.
When you click on Properties of the project, there will be a Resources tab wherein you can see your file.
To access the file, you can use ProjectNamespace.Properties.Resources.yourfilename.
Related
I want to create a resource file programmatically. I managed to do so following this link: https://msdn.microsoft.com/en-us/library/gg418542(v=vs.110).aspx
And I got this code:
ResXResourceWriter resx = new ResXResourceWriter(#".\Resources\resources.en.resx");
resx.Close();
It's working but it wants to create the file in the "Resources" folder in "C:\Program Files (x86)\IIS Express". But I want it to create the file in the dedicated "Resources" folder in my asp.net project, how to I point to that map relatively?
If you want it to write to a specific location I would suggest something like this
string resFilePath = ConfigurationManager.AppSettings["ResourceFilePath"];
ResXResourceWriter resx = new ResXResourceWriter(Path.Combine(resFilePath, "\Resources\resources.en.resx"));
resx.Close();
Putting the base path in the config allows you to change it when you deploy your application.
Remember that the code runs under the context of the running application though, so whatever account it runs as will require write access to your resource directory.
Create the resource file first, in the location of your choice.
Add the file to the project.
Depending on where the file/folder exists, VS will make either a relative or absolute reference to it
I write you all for a problem with my c# library. It needs an XML file that uses as a dictionary. Since the files will not change I would like the dll already contained the file and requires no memory references to it.
System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(#G:\Project\dictionary.xml");
So I put the XML file in the "Resources" folder of visual studio, but now I do not know how to get that file. If you always use the memory reference to use the library without also enter the file, it does not work. How can I do? If I open the dll with a notepad I see that the XML file is in.
This should give you an idea:
add a *.resx file
drag and drop you XML File onto it.
Use it!
Simple and plain question as this is the first time for me working with resources. To my console application/C# I have an xml file that contains some data, then I chose to add this xml file as a resource to my project, the question is, if I once need to change the data in the xml file, does the resource file update accordingly?
Thanks!
Yes, files added as resource stay in they original format and can be changed via normal editors for that file format. Obviously you will need to recompile project to see the change in resources.
Note that by default source files will be copied into project (so you need to modify copy in the project folder), but you can explicitly add "as link" to refer to some other file on your local disk.
I have a solution containing two projects. One project is just for doing all data stuff and the other one, the startup project, do all the web stuff.
Now I want to get the TasksDataBase.xml from the TaskManagerHelpers class by first getting the projects root directory. But all I get is the TaskManager.Web root directory. (I call the method inside TaskManagerHelpers.cs from a controller inside TaskManager.Web)
How do I get the TaskManager.Data root directory when I'm in a class in the same project?
I've tried with theese methodes and similar ones.
HttpContext.Current.Request.PhysicalApplicationPath;
System.IO.Path.GetFullPath();
AppDomain.CurrentDomain.BaseDirectory;
Thanks in advance!
One possibility is to embed the XML file into the assembly of the class library and then read it as resource in your web application. Remember that when you publish your web application to a web server all that will get into the package will be the files of this web application. There's no physical relation to some projects that might have lived into the Visual Studio solution that this web application was part of.
You may take a look at the GetManifestResourceStream method which will allow you to read the embedded XML from the referenced assembly.
Here's an example:
// you could use any type from the assembly here
var assembly = typeof(TaskManagerHelper).Assembly;
using (var stream = assembly.GetManifestResourceStream("TaskManager.Data.DataBase.TasksDataBase.xml"))
using (var xmlReader = XmlReader.Create(stream))
{
// ... do something with the XML here
}
Bear in mind though that since the file is embedded into the assembly you will not be able to modify it. It is readonly. If you need to modify it then an alternative approach would consist into copying this file to your web application. For example a good place is the App_Data special folder. You could even setup a post compilation step that will copy the XML file in this location.
And then you can reference it easily:
string xmlFile = HostingEnvironment.MapPath("~/App_Data/TasksDataBase.xml");
using (var xmlReader = XmlReader.Create(xmlFile))
{
// ... do something with the XML here
}
In this case since the XML file is now physically part of the web application and lives on the hard drive you could also modify it.
Just because the two projects are located in the same folder tree during development, says nothing about where they'll be located at run time. It's entirely possible that that could be on different machines.
"No," you say. They'll will definitely be on the same machine in the same c:\inetpub tree. That may be true, but that's your policy, not a requirement.
If you are going to establish a hard policy about where they are located, then you can hard-code that into you code.
Right-click the XML file and select properties, then change the Copy to Output Director to one of the other settings than "Do Not Copy". That will place the file into your \bin\ folder alongside the other project output. You can then use AppDomain.CurrentDomain.BaseDirectory as your base path
IF you are running a web project, all the referenced dll files are copied to the bin directory (unless they are in the GAC) and used from there, no matter if you add a reference to another project, Visual Studio first compile it and then copies it to the bin folder of the web project. You can mark your xml file as Content (Compilation Action) and with the copy always option so it always copy it to the bin directory .... the problem is that it sometime look for this files outside of the bin folder but I think that you can handle this.
I'm planning to build my winform into a .exe file. I'm just wondering what to do with the XML files that my application needs?
I did some research and found out that I can add the XML files in the Resource folder before creating a .exe file.
Or I need to create a setup file? When the user runs the setup file, the XML files will be installed into their pc.Now I wonder which one is the best way to go for,
Note: XML files might get modified by the user.
If you want to ship the XML files as seperate to the .EXE then you can set the Copy to Output Directory to Copy if newer. (click on file and then go to properties).
OR if you want it as part of the .EXE I think you can change the Build Action to Embedded Resource.
I personally would create a Setup as per your edit and include the XML files. I usually just add everthing from the bin/release folder that is needed when I create a setup file.
You could either deploy the necessary files along with the executable in the same folder or embed them as resources (if they are read-only). If you need to modify them do not embed them as resources into the executable.
The correct way depends on how you intend to use the files. If the files always are deployed together with your application, the application never writes to them and they are never upgraded without upgrading the application, you can go with them embedded as resources.
If you need to update them separately from the application, you need to have them as physical files.
You don't necessarely need a installation package, unless you need to apply some logic during setup, such as updating the content of the setup based on user input or to check preconditions. The application can just be copied into place regardless of if you have embedded the files or not.