How do I create a Resources file for a Console Application? - c#

I'm trying to use an embedded resource in a console application, but apparently console applications don't come with a resource file automatically. How do I create a resources file for my console app?

The project template for a console mode application doesn't have pre-cooked Resources. You simply add it with Project + Properties, Resources tab, click the "Click here to create one" link. Take an hour and click around some more. This is all very discoverable but you have to take a look.

Right click on the console application project node in the solution explorer. Add->New Item->Resources File.
edit: as has been pointed out, if you open the Properties of the console app project, it will also have a Resources tab, which will tell you that it doesn't have a resources file and provide you with a link to create one. This will do the same thing as the above, except that it will create the files under the Properties folder.

I added the answer in my answer to your previous question:
Right-Click on 'test' in Solution Explorer -> Add -> New Item -> Resources File
Then double-click on the created file (e.g. Resource1.resx), and take it from there.
Then use it:
string s = Resource1.String1;

Right click your project.
Select Properties.
In Resources click the link "This project does not contain a default resource file". Click here to add one.".

Understanding the fundamentals.
Just build your default
public class Lang {
public string Title = "my title";
}
then
public class Lang_en_US : Lang{
public Lang_en_US(){
Title = "my US Title";
}
}
then find your lang you want to case into with switch, if not match, just use the default
ASP.net compiles into classes like the global resource.
:)

Related

c# Winforms share resources between projects

I have a solution that contains 2 winforms app and a classlibrary referenced by the 2 apps... I added some images in resource
but when I go on the winform app and try to add the image into a button appears this:
Is there a way to do this?
thanks
You have to make resource items public instead internal.
To do this, in .resx file in solution explorer select Property Window (Alt+Enter) and in File Properties -> Custom Tool should be ResXFileCodeGenerator
Change this option to PublicResXFileCodeGenerator (if option don't show in dropdown field, don't worry aboit it, write/type PublicResXFileCodeGenerator in field)
After that resources member class will be generated with public modifier and you can access to it from any project that reference assembly.
I hope it can help you!

C# access files from another project

I am having a solution consisting on silverlight and web project. i want to access a file which is located inside a folder in web project from my silverlight project.how can this be done.
this file is a users manual document for which i have to provide the download functionality to the user..
Right click on the Silverlight project and select Add --> Existing item.
In the dialog, navigate to the location of the web project. Select the file(s) you need but instead of clicking on "Add", click on the dropdown next to the Add button and select "Add As Link".
In your Silverlight project:
Right-click References > Add Reference.
On the left tab, select Solution.
Select which project(s) you want to include a reference to.
In Silverlight project C# files, you may have to add a 'using' statement if you are trying to use a Class from a referenced project.
Got the solutions as below,
var uri = new Uri(Application.Current.Host.Source, "../DirectoryName/FileName.docx");
HtmlPage.Window.Navigate(uri);

"A project with an Output type of Class Library cannot be started directly"

I downloaded a C# project and I wish to debug the project to see how an algorithm implementation works.
The project has come in a Folder, inside this folder there are -
.sln file and
a folder which has source files and a .csproj file.
I installed Visual Studio and opened the .sln file present in the main folder. I built the project successfully, but when I try to debug the project I get this message:
A project with an Output type of Class Library cannot be started directly In order to debug this project, add an executable project to this solution which references the library project. Set the executable project as the startup project.
The strange part is that I don't see a main function anywhere.
What should I do to get round this hiccup?
The project you have downloaded compiles into a dll assembly and provide a set of classes with implemented functionality.
You should add to your solution a new project with Output Type of either Console Application or Windows Application (VS Add Project wizard will offer you different templates of Projects).
In the newly added project, you can implement logic to test your Class Library.
Output type of the project you can find and change by the following steps:
Right click on project in Solution Explorer -> Properties.
In opened tab with properties select Application and there will be ComboBox marked with Output Type label.
Just right click on the Project Solution
A window pops up.
Expand the common Properties.
Select Start Up Project
In there on right hand side Select radio button with Single Startup Project
Select your Project in there and apply.
That's it. Now save and build your project. Run the project to see the output.
This was the solution that worked for me since I couldn't find 'Common Properties' option.
Select your topmost level project in Solution Explorer.
Go to Project, and in contextual menu Set as StartUp Project.
See also: A project with an Output type of Class Library cannot be started directly
Just needs to go:
Solution Explorer-->Go to Properties --->change(Single Startup project) from.dll to .web
Then try to debug it.
Surely your problem will be solved.
The strange part is that I don't see a main function anywhere.
That is exactly your problem. The project merely creates a DLL. It has no executable to run.
You will need to add a second project, which is an executable which references the other project, and calls something in it.
1) Right Click on **Solution Explorer**
2) Go to the **Properties**
3) Expand **Common Properties**
4) Select **Start Up Project**
5) click the radio button (**Single Start_up Project**)
6) select your Project name
7) Then Debug Your project
Right Click on "Solution Explorer" -> "Properties"
Expand "Common Properties"
Select "Start Up Project"
click the radio button "Single Start_up Project"
select your Project name from the drop down list.
If still not working after the above steps, then try this.
Expand solutions explorer.
Right click on project name -> "Properties"
Go to "Application" tab
Select "Output type"
From the drop down list select the appropriate type according to your application.
"Windows application" or
"Console application"
Then save (ctrl + S)
Try debugging (F5)
You'll need some kind of app (Console Apps are my favorite for debugging, but a WinForm will do) which uses your Class Library. Just add a new project (in the same solution) of a Console Application or Windows Forms Application, and add a reference to your current project. Once you've done that, make any calls you need, set your break points, and go to town.
I had a similar issue when trying to use the Experimental Instance of Visual Studio 2013. This was for a vsix project (Creating Snippets).
Solution was:
Right Click Project in Solution Explorer > Properties > Debug
Setting the Start Action to "Start external program" and using the following path:
C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\devenv.exe
This option was set to "Start project" which won't work for the application output type Class Library, which caused that same error.
Note: Yours may differ depending on how you installed VS.
Error solutions is that you have already open your project but by mistake you have selected another class library .. that's reason this error is showing ... so what u need to do you u just select u r project then right click on u r project
after right click u can see the list box and select the "Set as start up project " option .
Accepted answer works if your solution has a project that compiles to an exe. If your solution does not have any projects that compile to an exe, then you have to use 'Start external program'.
VS2019 instructions:
right click -> properties on the main solution
debug, start external program, and add command line arguments
VS2022 instructions:
right click -> properties on the main solution
scroll down to Debug
Debug > General > Open debug launch profiles UI
left click the 'new' icon in the top left, select 'executable'
fill it out as per VS2019 (pick the exe and add command line arguments)
when clicking the start button, first select the profile you made
Suppose you have multiple project in the solution. Select the project that you want to view in browser and select 'Set as StartUp Project'. In your multiple project soln which was the main, the visual studio was unable to identify. this was the main problem.
You can right click the Class Library project and from the drop-down choose Initialize Interactive C# which will load your project context and you can work it in the interactive session.
In my case, the cause was that one of my projects in the solution wasn't loaded. The reason it couldn't load properly was that the file path length of one of the files was too long. Upon deleting this long file, I could reload the project, and build the solution.
If the question involves an Azure project, make sure you have the "Azure development" tool set installed, or when you go to run a solution you may get this same error.
Tools > Get Tools and Features... > Tick the box next to Azure development > Click install
None of the answers provided above helped me resolve this error, this is what resolved the issue for me.
Right click on the solution and select "Properties", which is in my case "Sintctech.Data".
Select the section called "Application".
Check what you have selected as your output type. If it is "Windows Application", change it to "Console Appication".
Rebuild and the problem should be fixed.

Add codebase as reference instead of copy Visual Studio

This may be a ridiculous question for you C# pros but here I go. I'm a Flash developer getting started in Silverlight and I'm trying to figure out how to create a "codebase" (a reusable set of classes) for animation. I'd like to store it in a single location and reuse it across a bunch of different projects. Normally in Flash I would add a "project path" reference and then start using the code. My question is, how do I add a folder to visual studio so that I can "use" those classes in my project. I tried "Add > Existing Item" but that copied the files into my project directory.
The easiest way would to create a new ClassLibrary project and build it. This will output a .dll file in a folder you can specify in the project settings menus, which you reference from every project that needs it.
Also, you can copy this .dll into the /bin/ folder of your project - this will do the same thing for this specific project, but when you start the next one you can change some details in the codebase library without breaking the first project.
The solution described by Tomas (adding a reference to a dll binary) is the correct solution to this problem; better than referencing the source code and compiling it into each project.
But just for extra information, if you ever do need to add a source code file to your Visual Studio project without having it make a copy of the file you can use the following steps:
Right click on your project in Solution Explorer and select Add -> Existing Item.
Navigate to the location of the source code file and select it.
On the "Add" button in the dialog window there is a drop down arrow. Click this and select "Add as Link".
This will allow you to use this source code file in your project without having VS make a copy of the file.
In Solution Explorer, right-click on the project node and click Add Reference.
In the Add Reference dialog box, select the tab indicating the type of component you want to reference. (for instance for a class library a dll)
Select the components you want to reference, then click OK.

How do I access the Properties namespace from within a console app?

I am trying to store/retrieve a value that is stored in the Application Settings. From within my console application I can not seem to access the Properties.Setting namespace. My web surfing has revealed that a little more work might be needed to do this from within a Console application. How does one do this?
string test = Properties.Settings.Default.MyString;
Thanks!
By default there is no Settings file in a Console application. However, you can add one simply by right-clicking your project in the solution explorer, choosing "Properties" and then in the resulting window, click the "Settings" tab.
There should be a link saying "Click here to create a default settings file". Once that's created, you're off to the races.
Ensure the namespace of the class you're using is the default namespace of your project.
2.then you cna use
string s = Properties.Settings.Default.THENAMEINSETTINGS;
.
So.. Once I created my Settings.settings file in the project that is saving the property I ran into the issue of how to access these properties from another project in the same solution. The settings object is sealed so you have to use a little trickery to gain access to the property values in another project. I found my solution here:
http://blog.decarufel.net/2007/10/getting-access-to-settings-in-another.html
Basically you create a link file to the Settings.Designer.cs file in the project where you are trying to retrieve the values.
I hope this helps someone with a similar issue.
-Nick

Categories