Using Visual Studio 2012, I am debugging a program to find out why loading a resource file using System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream isn't working. The file is being copied to the project's bin folder correctly - but invoking System.Reflection.Assembly.GetExecutingAssembly().Location is reporting that the program is not running from there - it is running from C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\root\c37f9465\2be54367\assembly\dl3\c92e18ea\e852a1ef_ce6ad001.
The program is a web service, and the error occurs in a DLL called ConnectSystem.DLL (another project in the same solution), and it is this file that resides in the Microsoft.NET folder.
If this is because of Dynamic ASP.NET Compilation, which looks like the best explanation, can anyone think of a way I can incorporate my configuration file please?
The resources you are trying to load shouldn't be next to the assembly, it should be compiled into it. GetManifestResourceStream only reads resources from the assemblies themselves.
You can try to set the build action of the resource to Embedded Resource, and the file should end up in your assembly. The name may vary, usually it has the namespace as prefix. You could use Reflector to see what the actual name is.
Related
I use xsltc.exe in developer console (ms visual studio). I try to generate a DLL that could be used in a .NET project.
xsltc /settings:dtd /settings:document /settings:script /c:Bk24.Specs specs.xsl /out:Bk24Specs.dll
I have successfully generated many of such DLLs before, but now I have a big problem. The current XSL contains a reference to an XML
<xsl:variable name="spcodes" select="document('specialCodes.xml')/list/data" />
I successfully generated the DLL for this template. I added a reference to this DLL in the .NET project. But then, the application throws an exception
File not found (c:\projects\bk24\specialCodes.xml)
in the real production environment. Of course, on the client's workstation there is no such directory C:\projects\bk24\. It's my directory on my dev machine, but when I ran xsltc.exe, I put specialCodes.xml into the same directory where the XSL is placed. I hoped that xsltc.exe would look at the directory, found that XSLs and XMLs are in the same directory, so, the generator will embed the XMLs into this DLL, but, it seems, it's not...
How can I resolve this issue?
If it is only a read only file, then open the file in a text editor and copy it. Then embed it as a string in any cs class and read with XElement.Parse(that_string)
I'm using WinSCPnet.dll in my project and to run correctly it needs WinSCP.exe in launch directory e.g. project_path/obj/Debug. But if I want to use my program, it needs to have this .exe file all the time in same folder otherwise the program will crash.
Is it possible to place WinSCP.exe into project resources so I don't have to place it in same directory everytime I move it?
(Sorry for my clunky English, don't downvote me for that :/ If you need some additional info I'll gladly post it)
The winscp.exe has to exist as a real file, at the moment you call the Session.Open.
So while you can store the winscp.exe to your application resources, you have to extract it somewhere (e.g. to a temporary folder), and set the Session.ExecutablePath accordingly, before you call the Session.Open.
Inspired by your question, I have added instructions for Embedding WinSCP executable as resource to the assembly documentation.
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 am working with developing Windows Service using C#. I want to load CLIPSLIB.dll with Windows Service. First I tried write service as console app in vs2010 placing CLIPSLIB.dll in the project debug folder. It worked fine.
But when I install it and run as Windows Service it throws this exception.
Could not load file or assembly 'Mommosoft.ExpertSystem, Version=0.3.0.2, Culture=neutral, PublicKeyToken=20382083c6694bdc' or one of its dependencies. The system cannot find the file specified.
As I figured out this is happening because Windows Service is unable to load relevant dlls. So whare should I put external dlls to load with Windows Service?
Scott Hanselman has an article that describes how to debug assembly loading errors. the downside to this method is that it requires a registry key change and a reboot in order to emable the fusion loader logging.
That article can be found here:
http://www.hanselman.com/blog/BackToBasicsUsingFusionLogViewerToDebugObscureLoaderErrors.aspx
since you are writing a windows service check that the reference for the assembly refers to the bin directory and not some other path. Otherwise the fusion loader logs will identify where it is trying to load the assembly from.
as to the .dll files add a folder to your project and add the .dll files there, change the build properties from do not copy to copy if newer in order that the .dll files will end up in your build output bin path.
You must rebuild the CLIPSLib solution,
Download all files in this link :
Link: https://svn.code.sf.net/p/clipsnet/code/
2.Open CLIPSLib.sln in CLIPSLib folder
3.Right-click on the solution and select rebuild solution.
4.Go to the CLIPSLib folder your CLIPSLib.dll and CLIPSNet.dll is in ..\ ..\mlplatform\bin floder.
hope this help.
I have a sound.wav file on my resources folder in my C# project. I found that I can use the file using:
Properties.Resources.soundImage
But the code above gives me a stream. What I need is the name and if it is possible, the path to the file. Is there any way to get them?
Since we see that it is part of Properties.Resources, we can tell that it is actually a compiled resource. This means it is embedded in the .NET assembly (.exe or .dll).
While it starts out as a file in your project, the compiler actually plops it into your output file. So when your application is running, there is no filename for you to get.