Browser detection in C# (.Net) - c#

I'm in C# land today. I'm trying to write a function which accepts a user agent string and returns an object that gives me at least the browser name and version. So I tried this answer, but apparently I don't have access to HttpBrowserCapabilities. It tells me the type or namespace name could not be found (yes, even if I add using System.Web, it still doesn't show up, or when I type using System.Web. it doesn't pop up, so it's obviously not there).
I'm using .net 3.5, but the documentation for that class shows it existed even in 3.5, so I'm not sure what's going on. I have access to the browscap files - ini or xml. Am I going to have to write this from scratch?
Edit: I've fixed the reference problem. But Chrome is being reported as AppleMAC-Safari 5.0. I'm wondering if I'm going to need a completely different approach. PHP figures it out with the same ini file fine.

Adding a using block does not automatically import the DLL. All a using does is allow you to not write:
System.Web.HttpClient //Or whatever
All over the place, and use HttpClient instead.
You need to add a reference to System.Web to the project before any of its classes will be available to you.

Did you have a using System.Web; statement in your source file?
Here's a tip: if you're using Visual Studio, and you have a reference to the System.Web.dll in your project, if you type the name of a type and press Ctrl-. it will give you a popup menu to add the namespace reference to your source file.

Do you see it in the ObjectBrowser (assuming you are using Visual Studio)? I found the namespace this morning (granted I'm on 4.5 - but documentation shows it has been around since 3.5 and earlier)

Related

How to know what assembly to add C#

I've ran into this issue a couple times and I'm wondering if anyone has a better solution than trial and error or searching stack overflow.
Lets say we are using some .net class Foo
Foo resides in the Bar.Baz namespace
The following statement
using Bar.Baz;
is not sufficient to compile the program, we are missing an assembly reference. So add a reference to System.Bar.Baz It still doesn't work so after searching the internet I find that I actually have to add a reference to Some.Other.dll and now it compiles.
My question is how do I know what namespace maps to what reference when the usual one doesn't work?
Most recent problem was
The type or namespace name 'DbContext' could not be found Instead of adding a reference to System.Data.Entity I had to install through Nuget.
If it is a .NET framework function, you can just search it on MSDN, and it will tell you in which assembly the class/function exists.
You can also use ReSharper which is a very nice plugin to Visual Studio, and it can help you add assemblies automatically.
If you're using Visual Studio 2013 or higher, one easy way to discover which namespace a class belongs to is using the Peek definition feature. You can easily find it in the right-click context menu.
In the screen below, I used it with KeyValuePair:
Also, take a look at the documentation.

Error while attempting to use "using com.google.zxing;" to decode Data Matrix

I started off from the thread to [decode data matrix in C#] (How To Use ZXing C# Port)! but I am encountering an error that prevents me from using zxing.
error states: "The type or namespace name 'com' could not be found (are you missing a using directive or an assembly reference?)"
In my code i am (trying to) use:
using com.google.zxing;
Please let me know what I am doing wrong.
It'd be nice if C# could automatically detect the references you're using and download them, but this is not the case. You need to download the ZXing SDK and reference it before using it.
The easiest way to do so is via NuGet, which is built in Visual Studio from 2012 onwards, and available for VS 2010. Right-click on your project, click on "Manage NuGet Packages", then select "Online" from the left-hand menu and search with the search box in the top right.
There are two ZXing ports available on NuGet, so you'll have to look at them both and pick the one that seems right for you.
You can also find some ZXing DLL on the Internet and reference it from your project: right-click on your project's "References", click on "Add reference..." then on the "Browse" button.
However, that is not as convenient as NuGet because you need to manually check for updates to the library, and update the DLL, whereas NuGet will tell you when an update is available (it won't force you to update).
Just by looking at your code sample, I wouldn't suggest using a port that uses Java-like namespaces (a more C#-y version would be e.g. "Google.ZXing"), since it sounds like a "dumb" port that simply converted the Java syntax to C# without thinking about whether some C# features are better at doing whatever the original library wanted to do.
I've used ZXing.Net v0.14 in a Windows Phone app and it works well.
You have to replace
using com.google.zxing;
with
using ZXing;
if you are referencing and using the ZXing.Net assemblies.
I replaced the Java-stylish namespaces with a more .Net like version.
Old sample around the web doesn't work with ZXing.Net.
In the source code repository and the download section of ZXing.Net you will
find some more up-to-date samples. You should really start with the newer samples because I wrote some simplifications on top of the port.

System references not being recognized?

I am not entirely sure how to better word this question, as I am not entirely sure what the central cause is to these various reference problems, so I apologize in advance for the vague title.
I am writing apps in VS 2012 Express for Windows 8, targeting a winRT (ARM) surface tablet.
I am having multiple problems with references apparently being recognized in their entirety. For simplicity, let me illuminate with some examples.
I am writing some code involving SOAP xml.
First, if I reference "System.Xml" (as well as System.Xml.Linq) by explicitly including a "using.." line, it doesn't seem to actually do anything.
using System.Xml.Linq;
using System.Xml;
...
XmlDocument xmldoc = new XmlDocument();
throws "The type or namespace name 'XmlDocument' could not be found"
Likewise if I am more explicit:
using System.Xml.Linq;
using System.Xml;
....
System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument();
throws "The type or namespace name 'XmlDocument' does not exist in the namespace 'System.Xml'" as well as the original error message.
Ok. I'm clearly missing something, but there's tons of google results for things like this.
I am running VS2012 on Windows 8 installed on VirtualBox, and thinking perhaps my installation was hosed, I first tried simply reinstalling VS2012, then repairing the installation when this did not seem to affect anything.
Next, I tried manually adding the reference to the .dll files in question. When I tried this, VS would not allow it, saying they are already included by default.
Next, I tried manually opening the .csproj file for my project, and adding Reference Include = "... lines for a few references which I seemed to be missing (like I said, I'm just using System.Xml as an example.) This didn't appear to change anything, as I was getting exactly the same errors.
Next, I went into "Add or Remove Programs" on a google-inspired hunch and, through Control Panel -> Add/Remove Programs, I turned on .Net Framework 4.5 Advanced Services. This unfortunately did not seem to have any effect.
Adding various .dll-containing paths to "Reference Paths" also did not seem to affect anything at all (that field was empty upon first installing VS.)
I am stumped. Am I missing .dlls or other files, even after repeated re-installation and repairs?
Note that I was able to at least create an XmlDocument by referencing Windows.Data.Xml, Windows.Data.Xml.Dom but that namespace seems to be lacking some methods I need.
System.Xml.XmlDocument is not available for Windows Store applications. Take a look at the MSDN page - classes, methods, and properties marked with the green shopping bag are the only ones you can use with Windows Store apps.
As for System and Windows namespace references, all of the ones you can use are already part of the .NET for Windows Store apps Reference in your project, so there's nothing that can be added.
You probably need to actually add the reference to the dll in your project.
see the following link for details
http://msdn.microsoft.com/en-us/library/wkze6zky.aspx
(The System.XML, and System.XML.Linq should be in the .Net tab)

Cannot find reference to System.Runtime.Serialization.Formatters.Binary

using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
VS says, The type or namespace name 'Formatters' does not exist in the namespace 'System.Runtime.Serialization'. Well, I should add this reference. Click Project > Add reference > .NET, there I can see only System.Runtime.Serialization, nothing like Formatters.
System.Runtime.Serialization.dll appears to be under c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\Silverlight\v4.0\Profile\WindowsPhone71\ - there are no Formatters, neither Formatters.Binary there.
How to find this reference?
The BinaryFormatter is not supported in Silverlight (or Windows Phone).
So apparently Silverlight does not have the BinaryFormatter. However an open source project exists that may be able to provide similar functionality for you. It is called sharpSerializer. It will work with Silverlight and WP7.
I am keeping the other half my answer below, while not appropriate for Silverlight, may be helpful for those that are still missing said reference in other projects, as it should correct it.
For other projects that do not use Silverlight, and have access to the BinaryFormatter you may have your target framework set incorrectly.
Make sure you are using the full .NET Framework 4 Profile and not the .NET Framework 4 Client Profile in your project as your target framework. See the image below, you can find these settings in your project's properties.

Problem creating site using Microsoft Visual Web Developer Express 2008

this is a very newbie question, sorry!
I need to create an aspx website based con C# and am calling some webservices based on some DLL's I already have. Beforem purchasing Visual Studio, I decided to try Microsoft Visual Web Developer Express (is this ok?) creating a Web Application ASP.NET based on Visual C#.
I created the form to enter the data which is submitted when clicking the process button. At this point I need to call stuff from the DLL, which I have added in the Solution Explorer via Add Reference, selecting the DLL from the COM list. But whenever I run the project, I always get the error "the type or namespace xxx cannot be found - maybe a using directive or assembler directive is missing" when trying to create the object.
What is my stupid mistake?
Thanks!
If you look at the error message, there is another half that you havent covered. At the top of each code file that you want to use the namespace from your DLL, you need to include a "using directive" to let the compiler know that it may be linking to that DLL during compilation. Basically the compiler is wondering where something is, and even thoguh you included it in the function, you didnt take the final step of adding in the using statement at the top of the file.

Categories