I've execute the instruction which locates there
I can get, execute and use .exe file as it shown there.
But when I trying debug this code
using System;
using System.Reflection;
using System.Resources;
namespace Example
{
public class Example
{
public static void Main()
{
ResourceManager rm = new ResourceManager("GreetingResources",
typeof(Example).Assembly);
Console.Write(rm.GetString("prompt"));
string name = Console.ReadLine();
Console.WriteLine(rm.GetString("greeting"), name);
Console.ReadLine();
}
}
}
with my GreetingResources.resources file, I get MissingManifestResourceException.
I also choose "Embedded resource" option in GreetingResources.resources file properties, but it doesn't solve my issue.
Go to project properties in VS, and see what the 'default namespace' is for the project. Then put that before the resource name, eg.
ResourceManager rm = new ResourceManager("<default namespace>.GreetingResources",
typeof(Example).Assembly);
If the resource file is also under a folder, include that, eg.
ResourceManager rm = new ResourceManager("<default namespace>.<my resource folder>.GreetingResources",
typeof(Example).Assembly);
Related
I need to get the path where the solution file (.sln) is located. I try with this lines:
string startupPath = Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName,"abc.txt");
// Read the file as one string.
string text = System.IO.File.ReadAllText(startupPath);
When I run it, I get an "Unauthorized Access Exception". When I put the path "manually": C:\Users\yabej\source\repos\tutoriales\Proyecto 0.1 it works!
However I need an "automatic" solution to be sure that the path will be correct when I run my project on different computers
The proper way to get the right solution path worked for me in Visual Studio 2019 with the EnvDTE100 NuGet Package.
Clarification: Only works with a single VS Instance Open (credits #junliantolovich)
Be sure use the correct ProgId for your Visual Studio:
using System;
using EnvDTE80;
using System.IO;
using System.Runtime.InteropServices;
namespace Test
{
public class Program
{
public static void Main()
{
var di = GetSolutionDirInfo();
Console.WriteLine(di.FullName);
}
public static DirectoryInfo GetSolutionDirInfo()
{
// Use here the ProgId of your Visual Studio
// VS 2010 -> "VisualStudio.DTE.10.0"
// VS 2012 -> "VisualStudio.DTE.11.0"
// VS 2013 -> "VisualStudio.DTE.12.0"
// VS 2015 -> "VisualStudio.DTE.14.0"
// VS 2017 -> "VisualStudio.DTE.15.0"
// VS 2019 -> "VisualStudio.DTE.16.0"
var progId = "VisualStudio.DTE.16.0";
var dte2 = (DTE2)Marshal.GetActiveObject(progId);
return Directory.GetParent(dte2.Solution.FullName);
}
}
}
If you add your file (abc.txt) to your solution, then in the properties for the file, you can set 'Copy to output directory' to 'Copy always'. Make sure the build action is set to 'None'. Then during the build process, it'll be copied to the same location as your executable and you can get the path using
var path = Path.Combine(Directory.GetCurrentDirectory(), "\\abc.txt");
I am having a issue where every time i change my resource files, the ResourceManager is set with a invalid path.
Example
namespace bundlrs_MVC6.Resources {
...
global::System.Resources.ResourceManager temp = new global::System.ResourceManager("bundlrs_MVC6.HomeIndexViewResources", typeof(HomeIndexViewResources).GetTypeInfo().Assembly);
Should be: (Note the extra Resources)
namespace bundlrs_MVC6.Resources {
...
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("bundlrs_MVC6.Resources.HomeIndexViewResources", typeof(HomeIndexViewResources).GetTypeInfo().Assembly);
Folder structure:
I am using Visual Studio 2015
Should i move all resource files to: namespace bundlrs_MVC6 instead of namespace bundlrs_MVC6.Resources if so, why?
This issue was fixed with the CoreCLR 1.0.0 RTM release
I have an ASP.Net website that references a class library. In the class library I need to read a file into memory.
At the top level of my class library there is a folder called EmailTemplateHtml containing the file MailTemplate.html that I want to read in.
How can I do this?
In Visual Studio, you can configure your library such that the file is copied into the build directory of any project that depends upon it. Then you can get the path to the build directory at runtime in order to read your file.
Step by step instructions, starting from a fresh solution:
Create your application project and your class library project.
Add a reference to the class library project from the application project via Properties->Add->Reference from the application's context menu in Solution Explorer:
Create the file in your class library project that you need to read, then set its Copy to Output Directory property to either Copy always or Copy if newer via the Properties pane in Solution Explorer:
From within either the class library project or your application (either will work with exactly the same code), reference your file relative to Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location). For example:
using System.Reflection;
using System.IO;
namespace MyLibrary
{
public class MyClass
{
public static string ReadFoo()
{
var buildDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filePath = buildDir + #"\foo.txt";
return File.ReadAllText(filePath);
}
}
}
(Note that back before .NET Core, you could use a file path relative to System.IO.Directory.GetCurrentDirectory() instead, but this doesn't work in a .NET Core application because the initial working directory for .NET Core apps is the source directory instead of the build directory, apparently because this was needed by ASP.NET Core.)
Go ahead and call your library code from your application code, and everything will work fine. e.g.:
using Microsoft.AspNetCore.Mvc;
using MyLibrary;
namespace AspCoreAppWithLib.Controllers
{
public class HelloWorldController : Controller
{
[HttpGet("/read-file")]
public string ReadFileFromLibrary()
{
return MyClass.ReadFoo();
}
}
}
using System.IO;
using System.Reflection;
public static string ExecutionDirectoryPathName()
{
var dirPath = Assembly.GetExecutingAssembly().Location;
dirPath = Path.GetDirectoryName(dirPath);
return Path.GetFullPath(Path.Combine(dirPath, "\EmailTemplateHtml\MailTemplate.html"));
}
If you want to find the path where the assembly is located; from within the assembly then use the following code:
public static string ExecutionDirectoryPathName
{
get
{
var dirPath = Assembly.GetExecutingAssembly().Location;
dirPath = Path.GetDirectoryName(dirPath);
return dirPath + #"\";
}
}
I am not sure what you mean by a folder in the class library but you can use the current working directory if you wish to build a path as follows:
System.IO.Directory.GetCurrentDirectory()
You can then use the Path.Combine() method to build file paths.
You can add a Static class in your class library with some static methods/properties to set. Set the values from Global.ascx.cs on start app method. Now you can get the values of class library.
Hope this makes clear.
Happy coding
In C#.Net when we create a resource file .... Visual Studio generates the designer code for the file automatically. By default the ResourceManager class gets instantiated using the default namespace of the project.
Example
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("ISC.Core.UI.DeployResources", typeof(DeployResources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
I want this resource file to be under a custom namespace (say Custom.Default.DeployResources) and not in the default namespace of the project (which in this case is "ISC.Core.UI.DeployResources")
Is there a way to achieve this ?
Right click on your resx, and type your namespace in Custom Tool Namespace.
You can also change the access modifier from internal to public(at the top of your resx when opened in designer) and put your resources in an external assembly.
Could you create a separate project with that default namespace, specifically for resources?
I have created an assembly and later renamed it.
Then I started getting runtime errors when calling:
toolsMenuName = resourceManager.GetString(resourceName);
The resourceName variable is "enTools" at runtime.
Could not find any resources
appropriate for the specified culture
or the neutral culture. Make sure
"Jfc.TFSAddIn.CommandBar.resources"
was correctly embedded or linked into
assembly "Jfc.TFSAddIn" at compile
time, or that all the satellite
assemblies required are loadable and
fully signed.
The code:
string resourceName;
ResourceManager resourceManager = new ResourceManager("Jfc.TFSAddIn.CommandBar", Assembly.GetExecutingAssembly());
CultureInfo cultureInfo = new CultureInfo(_applicationObject.LocaleID);
if(cultureInfo.TwoLetterISOLanguageName == "zh")
{
CultureInfo parentCultureInfo = cultureInfo.Parent;
resourceName = String.Concat(parentCultureInfo.Name, "Tools");
}
else
{
resourceName = String.Concat(cultureInfo.TwoLetterISOLanguageName, "Tools");
}
toolsMenuName = resourceManager.GetString(resourceName); // EXCEPTION IS HERE
I can see the file CommandBar.resx included in the project, I can open it and can see the "enTools" string there. It seems that either resources are not included into assembly or resource are included but .NET cannot resolve the name.
I think simpler solution would be to create separate resources file for each language.
As far as this case is concerned check if the assembly containing resources has the default namespace set to the same text (Project->Properties->Default namespace; in VS)
Check as well if the resx file has a property BuildAction set to "Embedded resource"
Sounds similar to an issue we had. The namespace was incorrect in the resource file's designer. I fixed it by manually re-running the custom-tool on the resx file.
Right click your.resx, and click Run Custom Tool.
I'm sure you've already got the answer, but just in case:
You can view your ManifestResourceName by calling
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames()
Check that Manifest name and your name in GetString() calling are identical.
Also, be sure you have correct namespace in designer.resx file:
namespace Jfc.TFSAddIn {
...
global::System.Resources.ResourceManager temp =
new global::System.Resources.ResourceManager(
"Jfc.TFSAddIn.CommandBar", typeof(CommandBar).Assembly);
...
}
Open resx file properties: "Build Action" should be "Embedded Resource"
For me, the source of the problem was naming the rex files starting with a number:
20160216_tranlation.resx
I had to add an underscore _ before the resx file name when calling GetGlobalResourceObject:
public static string getResource(string key)
{
return HttpContext.GetGlobalResourceObject("_20160216_tranlation", key).ToString();
}
I corrected the namespace in designer file (Resources.Designer.cs) in ResourceManager static property & it worked for me.
See the code below:
[global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
internal static global::System.Resources.ResourceManager ResourceManager {
get {
if (object.ReferenceEquals(resourceMan, null)) {
global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("XYZAssembly.Properties.Resources", typeof(Resources).Assembly);
resourceMan = temp;
}
return resourceMan;
}
}
I added a temporary class within my Form.cs while (testing || debugging) that caused this exception to be thrown. The Form.resx file (Name || Resource ID) was modified to the temporary class name instead of the Form class name. This caused the issue for me. I (corrected || alleviated) this by creating a separate file for my temporary class in the project.
One Solution is to change the property of resx file from content to Embedded Resource and Build it.Sure this time u vil get
I have encountered this issue in Xamarin.Forms, when I tried to the rename the project, the resources could not be loaded anymore with the same stated error text.
To fix the problem I had to modify the .csproj by a text editor, and change the logical name of the embedded resource.
<EmbeddedResource Include="Localization\TextResources.resx">
<Generator>ResXFileCodeGenerator</Generator>
<LastGenOutput>TextResources.Designer.cs</LastGenOutput>
<LogicalName>YourNewNamespaceName.TextResources.resources</LogicalName>
<SubType>Designer</SubType>
</EmbeddedResource>
Also watch out for the autogenerated class when you rebuild it, the namespace stated in there might change.
Hope it helps someone that went into the same situation.
Got this error when I added a class ABOVE the partial form class in my Windows forms app.
It went away when I moved the class BELOW the partial form class.
This answer solved the problem for me! GetGlobalResourceObject