Namespace of Regex won't work - c#

Im refurbishing some old code that used to work on .NET Framework 3.5 to make it work on .NET Framework 4 using C#.
The following Regex used to work fine with version 3.5 but doesn't work anymore for some strange reason.
public static readonly Regex ChatColorRegex = new Regex("\\|c[A-Za-z0-9]{6,8}"),
ChatLinkRegex = new Regex("\\|H.*?\\|h");
I have added the 'using System.Text.RegularExpressions' at the top of my file, but the following error rises: 'The type or namespace RegularExpressions does not exist in the namespace System.Text.
I've googled about that and read that you have to add a Reference to System.Text.RegularExpressions in Visual Studio. However, when i did, i couldn't find System.Text.RegularExpressions in the list of References i could add.
I'm using Visual Studio 2012.
Could anyone tell me what im doing wrong, or forget to read?

The Regex class is still in the System.Text.RegularExpression namespace. The class is in the System assembly.
If you check your project references in Solution Explorer, you should see a reference to the System assembly. Check the properties of that reference to see what .NET Framework version is being used for the System assembly reference. It should match the .NET Framework version you selected for the "Target framework" in the project properties (Application tab).
Edit: The Regex class is in the System.Text.RegularExpressions namespace, not System.Text.

Related

HttpUtility not recognised in .Net 4.5

I Developed a WinForm application in with the target framework set to .net 4.0, now I wish to add to a project that has it's target framework set to .net 4.5. After I added the 4.0 WinForm application to my 4.5 project I keep getting the an error on my HttpUtility object.
data += "&batch_data=" + HttpUtility.UrlEncode(batch, System.Text.Encoding.GetEncoding("ISO-8859-1"));
"The name 'HttpUtility' does not exist in the current context"
I did include the System.Web namespace where the HttpUtility is located.
Visual Studio Error:
CS0234 The type or namespace name 'HttpUtility' does not exist in the namespace 'System.Web' (are you missing an assembly reference?)
The problem is somewhere else.
As you can see in MSDN the HttpUtility class is present in System.Web in .NET Framework 4.5.
You're probably targeting the Client Profile: target the full framework in Project Properties. Otherwise:
either you did not add the right using statement using System.Web;
or you did not add the reference to System.Web.dll in the project.
WebUtility
You also have another possibility: Use the WebUtility class.
The WebUtility class is recommended by Microsoft itself and should be used outside of web applications.
Like the HttpUtility class it also provides you with the possibility to encode and decode URLs.
This way you don't have the problems with importing the library into your project or setting some specific profiles.
From the Documentation (Source)
The HttpUtility class is used internally by the HttpServerUtility class, whose methods and properties are exposed through the intrinsic ASP.NET Server object. Additionally, the HttpUtility class contains encoding and decoding utility methods that are not accessible from the Server.
To encode or decode values outside of a web application, use the WebUtility class.
The HttpUtility class exists from .NET 1.1, so I think it is not possible for regular projects to 'not see it', as long as you have included a reference to System.Web.
You might be using a PCL (Portable Class Library), which uses a stripped down version of the framework that is supported on the platforms you selected, like Windows Store apps, Windows Phone, Silverlight, etc.
I hope this link will help you.
http://msdn.microsoft.com/en-us/library/system.web.httputility(v=vs.110).aspx
Dot net framework 4.5 support HttpUtility as it is under System.Web namespace.
Also adding a System.Web reference, without System.Web.Extensions reference into your project. If it doesn't work remove the existing and add new reference of System.Web into project. Also check which framework it is targeting it should be .NET Framework 4 or 4.5 without Client.
I encountered this issue in .net 4.5.2 (using VS2019). I did check that I was using full framework and I also tried explicitly declaring System.Web in a using statement, though VS complains that the using clause is not needed.
System.Web.Utility appears to have been replaced by System.Net.Webutility

Visual studio C# packages

this is my problem with VS :S
in the first project :
System.Security.Cryptography.AesCryptoServiceProvider obj;
everything is ok
in the second project:
System.Security.Cryptography.AesCryptoServiceProvider obj1;
it doesn't recognize the AesCryptoServiceProvider?!!
is VS using different packages or what ?!
updated: changed the variable name but still not working
var is a reserved keyword. Use a different identifier name or #var.
System.Security.Cryptography.AesCryptoServiceProvider #var;
This may not be the problem - you need to ensure that each project has a reference to System.Core the assembly containing System.Security.Cryptography.
You will also need to ensure that you are targeting a framework version that contains this class (.NET 3.5 and above) - this can be done in the project property pages.
Check if both projects are referencing System.Core. Probably only the first one has it. You've to add it on both to be able to use AesCryptoServiceProvider.
Moreover, as you can see here AesCryptoServiceProvider is only available since .NET 3.5. Check your project's properties, in particular the Target Framework.
Are the references the same between both project? Just open references and see. I bet you are missing one. However, you really should use a different variable name than var. Also, can you post the exact error?
You can't name a variable var because it's a reserved word, use a different name, this will not cause an error :
System.Security.Cryptography.AesCryptoServiceProvider _var;
Edit :
AesCryptoServiceProvider is only supported in .Net framework 4 and 3.5 SP1, change the target framework and it will work and be sure to have System.Security.Cryptography; in that file.

C# using System.Linq error

Why might "using System.Linq" cause the following error?
The type or namespace name 'Linq' does
not exist in the namespace 'System'
Reference System.Core
And then there are others that merge this namespace too - but that's the primary one on .Net 3.5 and above.
If you're project is currently .Net 2.0, say, and you're using the right version of VS (2005 and above) - you can simply right-click on the proejct properties; and change the 'Target Framework Version' to 3.5. System.Core will then become available.
If you don't see that in the options - then I guess you're using an older VS
The most probable reason is that you are using wrong version of .NET Framework.
Try to add System.Core assembly to your project
You'll get this error if you don't have "System.Core.dll" referenced (the assembly which contains the core LINQ APIs).
System.Linq is available in .Net 3.5 and above version.
Maybe you're targeting an older framework, Linq came in with 3.5 IIRC.
You are using lower version of .NET Framework than 3.5 to compile the source code or you don't have added the System.Core assembly to your project.
Manually type using System.Linq in the starting of the project, you will not be able to find this namespace in add reference dialogue box.
If you are still getting error then try to Add Reference System.Core.
If you are getting an error that it has been already referred then you can unload your project and then edit your csproject file, manually copy reference to System tag and paste and change the name to System.Core and reload the project.
In my case the only thing that worked was:
Adding a new Razor item (e.g. MVC 5 View Page)
That automatically pulls in some NuGet packages
The package that makes System.Linq available to Razor Views IntelliSense seems to be Microsoft.AspNet.WebPages.

Why can't I find or use UrlEncode in Visual Studio 2010?

I have a string that I'd like to encode into the standard URL format. From what I've found, I should be able to do this via the httpUtility.urlEncode method, but I don't seem to have that available.
I've added "using" references to both System.Web and System.Net to no avail. I've also seen other references to server.urlEncode amongst other variants, but I don't see the method anywhere.
I'm using the latest version of C# in Visual Studio 2010. Is the method called something different in this version, hidden somewhere else, or am I completely off base?
By default, new projects in Visual Studio 2010 target the .NET Framework 4.0 Client Profile, which does not include the System.Web assembly.
You can change the version of the Framework that your project targets in your project's Properties. Under the "Application" tab, select ".NET Framework 4.0" from the combobox labeled "Target framework".
Then, make sure that you have added a reference to System.Web using the "Add Reference" dialog.
Finally, add a using directive to the top of your class for the System.Web namespace:
using System.Web;
You'll find the various overloads of the UrlEncode method in the HttpUtility class. Sample code:
HttpUtility.UrlEncode("http://www.google.com/");
In .Net 4.5 you can (should?, 'please use' says a Katana comment) use the System.Net.WebUtility.UrlEncode method.
It can't be named differently since Visual Studio doesn't supply the class or method names, the .NET framework does.
All I can tell you is that the System.Web.HttpUtility AND System.Web.HttpServerUtility classes contain a method called UrlEncode(string).
If your project target ".NET Framework X Client Profile",you cannot not use "System.Web",but you can use "Uri.EscapeUriString | Uri.UnEscapeUriString" instead.
Yes, adding the reference was my answer. But be sure you double check the project, that it is in, if you have more than 1 project in your solution. I had a solution with 3 projects. System.Web was added to 2 projects but not the 3rd project.
I spent an hour trying to figure out why I couldn't use HttpUtility since it was a Reference in the main project. But I didn't check the sub-projects of the Solution.
Hope it helps someone.
Because you only see AspNetHostingPermission, AspNetHostingPermissionAttribute, and AspNetHostingPermissionLevel, I strongly suspect (like the other guys) that you're missing a reference.
The best you can do is start a new project, because it's pretty complicated to add/remove references without ruining your entire project.
How to: Add or Remove References in Visual Studio (MSDN) shows how to add/remove references. In your case, you should check/add the System.Web reference.

The type or namespace name could not be found [duplicate]

This question already has answers here:
Getting "type or namespace name could not be found" but everything seems ok?
(44 answers)
Closed 8 years ago.
I have a C# solution with several projects in Visual Studio 2010.
One is a test project (I'll call it "PrjTest"), the other is a Windows Forms Application project (I'll call it "PrjForm"). There is also a third project referenced by PrjForm, which it is able to reference and use successfully.
PrjForm references PrjTest, and PrjForm has a class with a using statement:
using PrjTest;
Reference has been correctly added
using statement is correctly in place
Spelling is correct
PrjTest builds successfully
PrjForm almost builds, but breaks on the using PrjTest; line with the error:
The type or namespace name 'PrjTest' could not be found (are you missing a using directive or an assembly reference?)
I've tried the following to resolve this:
Removed Resharper (since Resharper had no trouble recognizing the referenced project, I thought it might be worth a shot)
Removed and re-added the reference and using statement
Recreated PrjForm from scratch
PrjForm currently resides inside the PrjTest folder, I tried moving it to an outside folder
Loaded the solution on a different computer with a fresh copy of VS 2010
I have done my homework and spent far too long looking for an answer online, none of the solutions has helped yet.
What else could I try?
See this question.
Turns out this was a client profiling issue.
PrjForm was set to ".Net Framework 4 Client Profile"
I changed it to ".Net Framework 4", and now I have a successful build.
Thanks everyone!
I guess it figures that after all that time spent searching online, I find the solution minutes after posting, I guess the trick is knowing the right question to ask..
In my case I had:
Referenced DLL : .NET 4.5
Project : .NET 4.0
Because of the above mismatch, the 4.0 project couldn't see inside the namespace of the 4.5 .DLL. I recompiled the .DLL to target .NET 4.0 and I was fine.
PrjForm was set to ".Net Framework 4 Client Profile" I changed it to ".Net Framework 4", and now I have a successful build.
This worked for me too. Thanks a lot. I was trying an RDF example for dotNet where in I downloaded kit from dotnetrdf.
NET4 Client Profile:
Always target NET4 Client Profile for all your client desktop applications (including Windows Forms and WPF apps).
NET4 Full framework:
Target NET4 Full only if the features or assemblies that your app need are not included in the Client Profile. This includes:
If you are building Server apps, Such as:
ASP.Net apps
Server-side ASMX based web services
If you use legacy client scenarios, Such as:
o Use System.Data.OracleClient.dll which is deprecated in NET4 and not included in the Client Profile.
Use legacy Windows Workflow
Foundation 3.0 or 3.5 (WF3.0 , WF3.5)
If you targeting developer scenarios and need tool such as MSBuild or need access to design assemblies such as System.Design.dll
Another thing that can cause this error is having NuGet packages that have been built with a newer version of .NET.
The original error:
frmTestPlanSelector.cs(11,7): error CS0246: The type or namespace name 'DatabaseManager'
could not be found (are you missing a using directive or an assembly reference?)
Further up in the log I found this:
C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Common.targets(1605,5): warning MSB3275: The primary reference "[redacted]\DatabaseManager\bin\Release\DatabaseManager.dll" could not be resolved because it has an indirect dependency on the assembly "System.Data.SQLite, Version=1.0.94.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139" which was built against the ".NETFramework,Version=v4.5" framework. This is a higher version than the currently targeted framework ".NETFramework,Version=v4.0".
The solution was to re-install the NuGet packages:
http://docs.nuget.org/docs/workflows/reinstalling-packages
I solved mine because the other project was coded with .NET 4.5 and the other one was coded 4.0
The using statement refers to a namespace, not a project.
Make sure that you have the appropriately named namespace in your referenced project:
namespace PrjTest
{
public class Foo
{
// etc...
}
}
Read more about namespaces on MSDN:
Using Namespaces
I encountered this issue it turned out to be.
Project B references Project A.
Project A compiled as A.dll (assembly name = A).
Project B compiled as A.dll (assembly name A).
Visual Studio 2010 wasn't catching this. Resharper was okay, but wouldn't compile. WinForms designer gave misleading error message saying likely resulting from incompatbile platform targets.
The solution, after a painful day, was to make sure assemblies don't have same name.
It is also possible, that the referenced projects targets .NET 4.0, while the Console App Project targets .NET 4.0 Client Library.
While it might not have been related to this particular case, I think someone else can find this information useful.
The compiled dll should have public Class.
I had the same issue. The target frameworks were fine for me. Still it was not working.
I installed VS2010 sp1, and did a "Rebuild" on the PrjTest. Then it started working for me.
Other problem that might be causing such behavior are build configurations.
I had two projects with configurations set to be built to specific folders.
Like Debug and Any CPU and in second it was Debug and x86.
What I did I went to Solution->Context menu->Properties->Configuration properties->Configuration and I set all my projects to use same configurations Debug and x86 and also checked Build tick mark.
Then projects started to build correctly and were able to see namespaces.
Changing the framework to
.NET Framework 4 Client Profile
did the job for me.
For COM/ActiveX references, VS 2012 will show this error right on using statement. Which is quite funny, since it's saying that may be you are missing a using statement.
To solve this: register the actual COM/ActiveX dll even if it's in the neighbor project, and add a reference through COM channel, not project channel. It will add Interop.ProjectName instead of ProjectName as a reference and this solves this strange bug.
If your project (PrjTest) does not expose any public types within the PrjTest namespace, it will cause that error.
Does the project (PrjTest) include any classes or types in the "PrjTest" namespace which are public?
just changed Application's target framework to ".Net Framework 4".
And error got Disappeared.
good luck;
:D
check your Project Properties, your Reference Paths should be empty like this:
Regards

Categories

Resources