I can't find the System.Media namespace in Visual Studio 2019 - c#

Edit - ANSWER: I found the solution thanks to #LexLi and #Amy; I accidentally created a .NET Core console application instead of .NET Framework. Once I remade it as a .NET Framework app it had "System.Media" available.
I'm relatively new (only a few weeks in) to learning C#, but I'm trying to make a console application text game, and I have a typewriter method I'm using to print messages to the display.
public static void typewriter(string s)
{
// This method types strings out slowly.
for (int i = 0; i < s.Length; i++)
{
Console.Write(s[i]);
Thread.Sleep(50);
}
}
I want to add a sound when each character is typed (I have the typewriter sound already). I saw some people recommend the SoundPlayer class located in System.Media. It would look something like this:
SoundPlayer typewriter = new SoundPlayer();
typewriter.SoundLocation = Environment.CurrentDirectory + "/typewriter.wav";
My problem is, I don't seem to have access to System.Media
If I type using System.(et cetera), Intellisense does not show Media as an available option. I am using Visual Studio 2019 and have .Net Framework Version 4.8 something
How can I get access to System.Media?
Here's a photo with some info to show my .Net framework and that Visual studio doesn't recognize SoundPlayer();

Check MSDN for documentation on that namespace. Identify which assembly its located in. Then add a reference to that assembly.
Google "MSDN system.media".. Pick a class from that namespace. I'll use SoundPlayer.
The documentation says that class is located in Assemblies:
System.dll, System.Windows.Extensions.dll.
So add a reference to one of those assemblies.

Related

Use the dynamic keyword/.NET 4.6 feature in Unity

I am trying to implement GraphQL into Unity3D (version 2017.1.0f3 Personal). I am using .NET 4.6 (Experimental), but despite this, Unity does not support dynamic keyword. Which is odd, since .NET 4.0 it is the part of .NET. Except in Unity. I was googling for some solution how to get it work, but no solutions to the dynamic keyword. The error is this:
Severity Code Description Project File Line Suppression State
Error CS1980 Cannot define a class or member that utilizes 'dynamic'
because the compiler required type
'System.Runtime.CompilerServices.DynamicAttribute' cannot be found. Are you
missing a reference? skiing-prototype (1) D:\skiing-prototype
(1)\Assets\Scripts\GraphQL.cs 62 Active
That is the only caveat of using GraphQL C# client. Has anyone tried it yet to get it work? I hadn't found any greater efforts to get it up and running yet.
EDIT:
I am using this client here: https://github.com/bkniffler/graphql-net-client
Also this is an error from visual studio, but in Unity console it shows errors too, will update what exactly momentarily
Assets/Scripts/GraphQL.cs(80,16): error CS1980: Dynamic keyword requires
`System.Runtime.CompilerServices.DynamicAttribute' to be defined. Are you
missing System.Core.dll assembly reference?
this is the unity editor error, which seems to be the same that in visual studio
The first step is to check if Unity recognizes these 2 basic C# 6 features from MS site.
1.Try "Index Initializers" feature:
private Dictionary<int, string> webErrors = new Dictionary<int, string>
{
[404] = "Page not Found",
[302] = "Page moved, but left a forwarding address.",
[500] = "The web server can't come out to play today."
};
2. then "String Interpolation" feature:
private string FirstName = "";
private string LastName = "";
public string FullName => $"{FirstName} {LastName}";
If they give you error then the problem is not just the dynamic keyword but a problem that Visual Studio cannot recognize the .NET version being set by Unity.
From the comment section your Unity failed to compile the first example.
Go through the steps one by one for a possible fix. Do not skip of them.
1.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> Experimental (.Net 4.6 Equivalent).
2.Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Api Compatibility Level --> .NET 4.6
3.Restart Unity Editor and Visual Studio. You must restart both.
Test both C# features above. If they work then the dynamic keyword should as-well. If they don't then move on to #4.
4.Update Visual Studio. This is very important. Update the visual Studio to the latest version/patch.
5.If you can't still get both C#6 features above to compile then Re-install both Visual Studio and Unity then perform step #1 and #2 again as some files are missing.
6.Finally, if you get both C#6 features working but the dynamic keyword is still not working then update from Unity 2017.1 to Unity 2017.2. This version fixed many .NET issues.
Note that I am using Unity 2017.2 with the dynamic keyword without any issue. Also, GraphQL is working fine.
I seem to have found a solution
Navigate to Edit > Project Settings > Player > Other Settings > Configuration > API Compatibility Level and change from .NET Standard 2.0 to .NET 4.x
This immediately removed the compiler error and allowed me to run code using the dynamic keyword.
Let me know if that was useful

Visual Studio Autocomplete Failing with Source Files which are not Part of a Solution

I have two source files, game.cs and textEngine.cs, both in the same namespace, but not part of a Visual Studio project or solution.
textEngine.cs contains a class called Engine, but when I try to reference any of the members of Engine from game.cs intellisense doesn't autocomplete them.
Additionally, the 'go to definition' feature doesn't work for these members.
The files compile and run cleanly, so this is only a small problem, but an annoyance nonetheless.
I am using Visual Studio 2015.
Method definition in public class Engine:
// start playing the currently loaded sequence
public static void StartSequence()
{
mainTimer.AutoReset = true;
mainTimer.Elapsed += PlaySequence;
mainTimer.Start();
isPlaying = true;
}
Method call in game.cs:
Engine.StartSequence();
Note that Engine is not properly highlighted and StartSequence was not autocompleted.
Source files that are not part of a solution apparently cannot use visual studio's auto complete.
"Visual Studio needs the project system to track which version of the .NET Framework you're targeting and which DLLs you have referenced. There is no way outside the project structure to do this."
Source: https://superuser.com/questions/981816/enable-intellisense-for-single-cs-file-in-vs2015

Installed InputSimulator via NuGet, no members accessible

In Visual Studio 2013, I installed a C# package called "InputSimulator." After doing so, I see a new reference get added to my project called "WindowsInput." (i.e., WindowsInput.dll)
The problem is that none of the methods that the codeplex site talks about are accessible. If I try:
InputSimulator.SimulateTextEntry("Say hello!");
I get the error:
Error 14 'WindowsInput.InputSimulator' does not contain a definition for
'SimulateTextEntry' Blah.cs 33 32 ALibrary
I do have using WindowsInput as a directive.
Does anyone know why the methods are not accessible? Methods like SimulateKeyPress() are also not available. These are the key API. Am I missing something about adding this library and referencing it?
So I just tried this lib.
Actually their documentation is just a little off. You need to create an instance of InputSimulator first, like this:
InputSimulator s = new InputSimulator();
s.Keyboard.TextEntry("Hello sim !");

Dealing with C# namespaces

I am working on a project that will need to ability to download youtube videos. I found this project on github:
https://github.com/flagbug/YoutubeExtractor
My project already has a namespace. How would I import the YoutubeExtractor into my project? Do I need to change the namespace for it before (or after) importing it? Or is it up to me, in which case, what are the advantages and disadvantages to changing the namespace vs. not changing it? I am using VS Express 2012, if that matters.
My project already has a namespace. How would I import the YoutubeExtractor into my project?
You add reference to the external assembly (in this particular case you install the NuGet) and then add using statement with the correct namespace (YoutubeExtractor) in which the classes are defined.
So just follow the steps described on the home page:
Install-Package YoutubeExtractor
and then:
using YoutubeExtractor;
and finally:
// Our test youtube link
string link = "insert youtube link";
/*
* Get the available video formats.
* We'll work with them in the video and audio download examples.
*/
IEnumerable<VideoInfo> videoInfos = DownloadUrlResolver.GetDownloadUrls(link);
Open the project that exists for the YouTubeExtractor and build it.
Move the outputted assembly to a location inside your project structure.
Add a reference to that assembly.
Add a using {namespace} to the files you want to use the extractor in.
Where {namespace} is the namespace it uses.
Further, it appears that there is a nuget package for it (you see that YoutubeExtractor.nuspec file in root). I would recommend installing nuget into Visual Studio and then searching nuget for the YouTubeExtractor. It's a lot easier, and you get updates with it easier as well.

How to use XDocument class in Mono

I'm trying to use XDocument class inside a Unity3D project on Windows 7.
I did the following:
added the reference System.Xml.Linq to the Mono project.
included the namespace:
using System.Xml.Linq;
set the target framework to: Mono/.NET 3.5
clean and rebuild the project
But still Unity3D complains about it. Here's the error output in the console:
Assets/Scripts/Editor/RoadManager/RoadManager.cs(3,18): error CS0234:
The type or namespace name `Linq' does not exist in the namespace
`System.Xml'. Are you missing an assembly reference?
Any idea?
This has been discussed many times before, but few of these answers are complete.
As has been said before, Unity3d only supports up to .NET version 2.0, and it seems System.Xml.Linq was introduced in .NET 3.5, besides the fact that it is not listed on the Unity3d compatibility list anywhere.
The only things to try are to set the Mono API compatibility level to 2.0 (Menu: Edit > Project Settings > Player and look in the Other Settings panel), but it seems that that was a mistaken solution for Linq2SQL.
Another possible solution is to add the DLL yourself into the Unity Editor as shown:
Try dragging the C:\Program Files
(x86)\Unity\Editor\Data\Mono\lib\mono\2.0\System.Xml.Linq.dll file
into the unity project window like you would a texture or other game
asset.
If none of these yield ANY luck for you, then I'm afraid you are out of luck.
Unity3D supports .Net 2.0 only, so setting compatibility to .net 3.5 in MonoDevelop/Visual Studio will not work. You will have to make do without the class. This shows what library classes are available:
http://docs.unity3d.com/Documentation/ScriptReference/MonoCompatibility.html
See also this post in UnityAnswers: http://answers.unity3d.com/questions/46039/can-not-reference-systemxmllinq.html

Categories

Resources