Visual Studio: cannot access resources from code - c#

I added several files (HTML, CSS, etc.) as resources to my Visual Studio project. According to MSDN, I should be able to access these resources via
MyProjectName.Properties.Resources.Filename
Unfortunately, IntelliSense doesn't even know the Properties property behind MyProjectName. If I type "MyProjectName.", IntelliSense offers me several things but no properties. I already built the project, but nothing changed.
If this is important: that project is not the main/startup project of my Visual Studio solution.

You need to add them here:
Project menu
MyProjectName Properties
Resources tab
"Click here to create resources" (if necessary)
Select Files menu
Add Resource
Now you can access them using MyProjectName.Properties.Resources.Filename

Make the file as a resource from its properties as:
BuildAction=Compile

Take a look at this MSDN Forum link. What I take from it is make sure your resources are added as resources and that the namespace's match.

Are you sure they're marked as resources? Right click and check their properties.

It is a little confusing that there is a folder in Visual Studio called Propertieson the same level as Resourcesbut which is bypassed altogether.
So while the path in your code may go via Solution_name.Properties.Resources.yourFile.png this does not reflect in the folders used in Solution Explorer of Visual Studio, as there you find the image then directly under Solution_name\Resources\yourFile.png.
And just adding a file and moving the resource with the mouse won't make Visual Studio find this file as Resource in your code. So you must go through menu Project, Properties (lowest entry) ? or click Alt + F7, Highlight Resources.
Now click on the Drill-down-Arrow of Add Resource rather than just on the main area of the button. Doing that would open the dialog window for Add New Resource, but actually you want to click Add Existing File. That's it.

I know this is contrary what answer said but on VS2013, I had to set BuildAction=Resource

For anyone else perhaps check the Resources.Designer file?
I found a discrepancy on this line
System.Resources.ResourceManager("DemoApp.Properties.Resources", ...
I had changed the project name to Demo.

I am not sure will it help or not but all I had to do is creating a new form and adding an icon to picturebox. Somehow that solved the problem.

Related

vs 2017 intellisense not working C# and XAML

I have a Strange issue that I can't seem to fix, my Intellisense for XAML is no longer showing up and the code behind is only showing the premade Members. The Classes and Methods I have made are not showing up. Also Visual Studios is not recognizing other pages and wont recognize Navigation either.
I have tried going to Tools>Text Editor>C#>Intellisense -Statement completion and checking the boxes Auto list members and Parameter information. I also tried to clear out the cache.
From what I have researched it seems nobody else is having the same problems.
Close all open tabs in the project and quit VS, reopen the solution in VS and right click the XAML file in the Solution Explorer and then select Open With….> Source Code (Text) Editor.
Delete obj folder and clean project
Right click the XAML page>Properties>Build Action>change it to something else and back
Add a new content page under this specific project and check it works or not.
I would like to add that this worked for my .cs files only.
To get the XAML files Intellisense to work try to repeat the above steps on your .cs files or wait for the XAML files to gain Intellisense.
For ReSharper users: I found that having ReSharper's IntelliSense enabled for all languages can break Intellisense unexpectedly (especially for XAML files). Here's how I was able to fix the same problem that the asker had in Visual Studio 2017 with ReSharper installed:
In Visual Studio, go to the ReSharper menu and click on Options.
On the left side go to Environment > IntelliSense > General.
Select the Custom IntelliSense radio button.
Change any languages that have broken IntelliSense to Visual Studio (like XAML).
Click the Save button.
Just Exclude and Include Xaml Pages and It works again.
I solve that by changing the default editor in visual studio :
In Visual : File > Open
In the open file box : Select a .xaml file (don't open it)
Select "Open with" in the button arrow
Select "Source Code (Text) Editor and Set as Default
Click OK
I tried most of the above without much luck, but noticed if I created a new page, then intellisense worked as expected. For the properties section of the xaml file I noticed that the new page had a Custom Tool assigned (MSBuild:UpdateDesignTimeXaml) do I tried to cut-and-paste this into the existing forms without luck.
Looking at the .cs page for the new page that worked I notice that there is some extra info above the partial class [XamlCompilation(XamlCompilationOptions.Compile)] so I added this along with a using Xamarin.Forms.Xaml statement.
Finally I went back to the xaml properties page and selected Reset to Default for the Custom tool. Voila, for me everything started behaving itself.
Please add the following Nuget Package from Nuget Console.
Install-Package MobileEssentials.FormsIntellisense -Version 0.1.1-pre
You can download the latest update from the following link.
https://www.nuget.org/packages/MobileEssentials.FormsIntellisense/0.1.1-pre
After installing the package please restart the project and wait for sometime and check it.
Just delete .vs directory. this directory is hidden. so
Just Exclude and Include Xaml Pages worked for me too. The difference in .csproj file was:
MSBuild:Compile
now:
XamlIntelliSenseFileGenerator
I could fix the issue in VS2019 by launching the VS installer to modify it, un-check and check the workload ‘.NET desktop development’ to re-install it.

Cannot get Reharper 9.1 Move to Resource refactoring to work

I get this error message when I try to apply the Move to Reource refactoring to a string in a razor file:
"This project does not contain an available resource file"
I have tried added ressource files by adding new item, and also by adding in the project settings dialog. and building etc. All to no help. What is the problem?
I am using JetBrains ReSharper Ultimate 2015.1.1 Build 102.0.20150521.123255
ReSharper 9.1.20150521.134223
in Visual Studio 2015 rc in an ASp.NET MVC 5 project.
Any suggestions how to make this refactoring to work?
Open the .resx file with your "Managed Resource Editor" (the default), look at the top of the screen. There's a dropdown called "Access Modifier". Set it to public. Done.
Bart Van Meerbeeck's answer didn't work for me for the identical issue, though it definitely is the best answer. In MY case, however, the problem was me.
I was working in a large solution, and the file, in which I was attempting to do this refactoring, was located within a completely different project than where my Solution Explorer was scrolled to at the time. Upon realizing this, I created the resources file in the respective project, and all was well.
So, to others who may run into this, simply click your "Sync with Active Document" button in Solution Explorer, to jump to the project for which file you're editing, and you won't feel dumb like I do. :)

"Add existing item" in Visual Studio: is it possible to make "Add as link" default?

As pointed out in this SO answer, the Add > Existing item dialog in Visual Studio by default displays the Add button (meaning that the selected items will be physically copied to the new location), whereas the desirable action is often (always?) to Add as Link.
Is it possible to configure Visual Studio so that Add as link is selected by default when opening the Add > Existing item dialog?
I have thoroughly searched the Options dialog in Visual Studio and examined the MSDN documentation for an answer to this, but so far to no avail.
Project Linker would be a good option for automated linking when two new projects are to share the same code base. However, when linking a large number of files from an existing to a new project, one seems to be confined to the Add > Existing item approach, and this work could be much more convenient and less prone to error if Add as link would be the default action.
Unfortunately, there is no option for that. But there is silver lining. You can drag files to a project and drop them to desired folder. Files are copied to your project. If you press Alt key doing drag and drop operation, files are not copied but linked instead.
You can also use VSCommands extension which adds 'Copy as Link' on folders and files in Solution Explorer. After that you can do 'Paste Link' in desired location. It also allows you to navigate to source item from linked item via 'Locate Source File' from cotnext menu. Works with vs2010 and vs2012.
It appears that when you "Add Existing" to a SOLUTION FOLDER(as opposed to an actual folder), then you are always adding these items as a link.
If you add existing items to an actual folder, then you get the button that gives the option for "Add as Link".
Simply Edit project file and change the path of the file.
You can relatively move folder up using ../
It starts in the project folder.
I found an nice tip here:
If you want to add an existing item as link in VS C# Express 2010 in the "Add existing item" dialog you have to explicitly choose Add As Link from the dropdown on the Add button.

Unable to access 'Build Action' property for .cs files

i got a problem with my site with my app_code files and a lot of question ive read people say you need to change the proprties of the .cs files to Build Action.
by right clicking the .cs files and press proprties.
but...
when i right clicking the files i cant see any option called proprties so i press F4 and it open a proprties window and i just click on the .cs file and still cant see any option i can change somthing to Build Action.
any idea why i cant see this options?
EDIT:
my problem is that when i get in the file proprties i have only 2 options...
thay are called:
1.Full Name
2.Full Path
EDIT2:
here what i see in visual studio 2010 when i try access the protrtie menu/window
http://img338.imageshack.us/img338/2359/blac.png
As Microsoft points out in the article "Web Application Projects versus Web Site Projects":
Web application projects use Visual Studio project files (.csproj or
.vbproj) to keep track of information about the project. Among other
tasks, this makes it possible to specify which files are included in
or excluded from the project, and therefore which files are compiled
during a build.
An answer to a similiar question at CodeProject's forums reveals a hint. Abstract:
[...] Looks like you are working on a web application that is actually a
Website as per Visual Studio. You would need to create a new Web Application
and probably copy over the source files there. [...]
http://www.codeproject.com/Questions/173637/Setting-Build-Action-for-Files-in-App_Data
Have you tried this:
You say you haven't got the option being suggested by other posters.
If this is the case, than it is quite possible that your Visual Studio settings are corrupt; this can give rise to all sorts of odd behaviour.
I would suggest you reset your settings, but please be aware you will lose any custom IDE settings that you've previously applied.
Try this:
In Visual Studio, go to Tools->Import and Export Settings
Choose "Reset All Settings" and click Next
Choose to save your current settings if you wish, or select "no" and then click next.
Choose the collection of settings( he IDE preset) you want, probably "Visual C# Development Settings"
Visual Studio will now revert all settings. Hopefully this will make the Build Action reappear.
[EDIT]
It might be worth trying safe mode too.
To do this, start up a "Visual Studio Command Prompt" from your start menu/programs list in Windows, and start Visual Studio with
devenv.exe /SafeMode
Does this make the options appear?
You can copy the file from Windows Explorer and paste it in the Solution Explorer. It will replace (or do nothing but incorporating it in the proj file) the file and recognize it as C#.
Normally you should see a Property named "Build Action" in the first line of the Property Window. This property should be set to "Compile".
Please select file and right click on it so that you will get following screen
Than click on the Properties you will get following screen
You can find build option in as a first option.

adding images in my project

I'm trying to add/change images in my project, using Microsoft Visual Studio 2008 C#. Along with it, Devexpress components are also included.
What I did, I copied an image (.png file) and paste it it in my "PrintRibbonControllerResources.resx" and then after that I have to open again the MainForm.cs right click on the form and click on the Run Designer. It will open the Ribbon Control Designer. From there, I can add the image.
Do you think its ok? It's my first time to do this, and I don't have any experience & I'm learning it by doing.
Thanks
===
it seems I don't see the add function under resource tab
I generally add an image to the project itself. (Add/Existing item) I do this so I can use SourceControl to check in/out the image file. The resx then links to these files.
Basically, my philosophy is: if it works, don't fix it.
If this solution works for you, it's fine. At least, until you discover a situation where it fails.
If you go in the properties of your project, and select the resources tab, you can add it directly from there and it will be accessible in the default resource file of your project, which might be more convenient.
I like to add it to a solution folder (or folder in the project) and like that to the resx via "Add Exising File".
I get all of the advantages of having the resource file, but also the advantages of having a physical file (editing, quick view, etc.)

Categories

Resources