VSCode C# Issue Adding An Assembly Reference - c#

I have the C# VSC extension as well as the NuGet Package Manager installed. Here is my VSC version info:
First, I create a brand new console application in VSC with the command:
dotnet new console
This gives me the code:
using System;
namespace ReferenceTest
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}
I now want to create a using System.Windows.Forms; reference. When I start typing this using statement however, intelli-sense fails me:
I found this question/answer already but in step #5 the autofill option shown is not there:
When I try to improvise and follow other instructions I've found in my searching by using the
NuGet Package Manager: Add Package
option, I cannot select System.Windows.Forms
I have looked for a project.json file per the instructions of several sites but I can only find a project.assets.json file and it doesn't look very similar to what I see in the examples I find. This issue is not only for System.Windows.Forms but other references I try as well.

Windows Forms and WPF applications are not supported in .NET Core, only in .NET Framework.
have a look at the following article:
https://stackify.com/net-core-vs-net-framework/

Related

Type or namespace name TSocket could not be found apache thrift C# Visual Studio 2019

Screenshot of installed thrift package and Thrift.dll reference:
I am trying to create a simple thrift client in Visual Studio 2019 using C#. I have generated the c# thrift bindings and everything else. However, I get "Type or namespace name "TSocket" could not be found". I have no other errors. Here is a snippet from my setup code:
// imports, namespaces
using System;
using System.Net;
using Thrift;
using Thrift.Transport;
using Thrift.Protocol;
TTransport socket1 = new TSocket("hostname", port);
TTransport transport = new TFramedTransport(socket1);
(more stuff .....)
I have no other issues with other references to thrift modules within the client. It just can't find "TSocket" somehow. I am super new to c# and thrift.
I am using thrift 0.14.1 and have installed it using the NuGet packages add-on Visual Studio 2019.
What could be the problem? Thanks.
screenshot of error
After looking at, it seems "TSocket" is not even listed under Transport as you can see in the screenshot below.
screenshot of available modules under Thrift Transport
Thrift 0.14 changed some names and the nested namespace structure. What was Thrift.Transport.TSocket is now Thrift.Transport.Client.TSocketTransport. See if swapping that fixes it.
If not check your project ref to the Thrift lib. Something like this may help (quoting "The Programmer's Guide to Apache Thrift"):
"To add the C# Apache Thrift library reference, right-click the References item in the project in the Solution Explorer and choose “Add Reference”. Next use the “Browse...” button to locate the Thrift.dll in thrift/lib/csharp/src/bin/Debug (or wherever). Make sure that there is a check next to the Thrift.dll entry in the Reference Manager dialog and then click OK. After a brief pause Intelisense errors should clear."
... or using the package manager:
"To add a Thrift.dll reference you can simply run the PackageManager install command:
PM> Install-Package -Prerelease ApacheThrift -ProjectName <your proj name here>
'ApacheThrift 1.0.0-dev' already installed.
Adding 'ApacheThrift 1.0.0-dev' to tradeServer.
Successfully added 'ApacheThrift 1.0.0-dev' to tradeServer.
"

How can i connect access database on console application?

I want to update the database on my console application.
but here has an error:
The type name ‘OleDbConnection’ could not be found in the namespace ‘System.Data.OleDb’.
This type has been forwarded to assembly ‘System.Data.OleDb, Version=4.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35’
Consider adding a reference to that assembly.
I can easily reach datas from form application but when i try it on console application, i can not.
there is my code:
using System;
using System.Data;
using System.Data.OleDb;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
OleDbConnection baglanti = new OleDbConnection(#"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=.\botbase.accdb");
}
}
}
there is the photo:
I assume you are using .NET Core and not the .NET Framework?
So you probably have to use the .NET Platform Extensions (available as NuGet package):
System.Data.OleDb
The issue-comment that announces it:
Everyone,
Now that System.Data.OleDb is available on NuGet.org, it would be great if you could take it for a spin on .NET Core 3.0 preview 6. If you have an existing application, sample code, or a test that was blocked from working on .NET Core for lack of this functionality, please try now. We are hoping to get your help to cover a variety of OLEDB providers and scenarios, so we can identify any issues with the port before RTM.
Thanks!
Another really useful package for Windows dependent features:
Microsoft.Windows.Compatibility
OleDbConnection is in the System.Data.Common.DbConnection namespace.
Add a using clause to the top of your file for that namespace.

Bitmap object not present in system.drawing?

I am new to C# and I am trying to create a bitmap object in C# in Visual Studio 2019, but it isn't working. I have .NET Framework updated to the latest version and I have resharper installed. I am using Windows 10.
Ive tried adding the references manually with Add/Reference... and it does not stay checked.
I added using system.drawing and using system.drawing.common to the top of my code.
I updated my .net core and enabled it in my project.
using System;
using System.Drawing.Common;
namespace Bot
{
class Program
{
static void Main(string[] args)
{
var bmp = new Bitmap();
}
}
}
I expected it to create a bitmap object, but it won't compile and gives me errors saying that the bitmap object does not exist in system.drawing.
The Bitmap class if part of the assembly System.Drawing.Common.dll, so you need to install System.Drawing.Common via the Nuget Package.
It looks like you don't have the package in your system/project.
In you're project directory or from VScode terminal do this:
dotnet add package System.Drawing.Common --version 6.0.0
then add a reference to the package in you're project

Why WindowsIdentity class is not visible in .NET Core

Having the code below in VisualStudio 2017 .NET Core 2.0 Console App
using System;
using System.Security.Principal;
namespace smallTests
{
class Program
{
static void Main(string[] args)
{
var identity = WindowsIdentity.GetCurrent();
}
}
}
Why am I getting the error:
The name 'WindowsIdentity' does not exist in the current context
If I can see this class in .NET Core 2.0 library in .Net Core docs ?
Same code works in .NET Console app.
[EDIT]
#Will #JohnnyL Commented that I do not refer, System.Security.Principal.Windows.dll, that is true.
But I am curious why it is not working, because
in .NET 4.6.1 project (where class WindowsIdentity is visible) I also do not refer this System.Security.Principal.Windows.dll specifically. However i refer System.dll.
I always thought that it works like namespace hierarchy. For instance, when I refer to
System.Security.Principal.dll
i can use class which is in
System.Security.Principal.Windows.dll.
Am I wrong?
I added System.Security.Principal.dll to .NetCore solution by hand but it still does not work.
[EDIT2]
#Will Thank you a lot for expaining the subject it helped me a lot.
I tried to figure out is WindowsIdentity compatible with Core and it seems that it is please see:
in this apisof.net in Declarations area i can see that WindowsIdentity is in .Net Core 2.0 System.Security.Principal.Windows, Version=4.1.1.0, PublicKeyToken=b03f5f7f11d50a3a
but i do not have System.Security.Principal.Windows.dll in references, should I add it? If yes from where?
in .NET Core api reference i see this class in the list (what is the purpose of that listing if it is not compatible with core?
I also find information about that class in that link
Am I looking in wrong places?
Microsoft announced Windows Compatibility Pack for .NET Core a few weeks ago,
https://blogs.msdn.microsoft.com/dotnet/2017/11/16/announcing-the-windows-compatibility-pack-for-net-core/
And by analyzing the source code of System.Security.Principal.Windows.csproj and the commit adding it,
https://github.com/dotnet/corefx/blob/master/src/System.Security.Principal.Windows/src/System.Security.Principal.Windows.csproj
My conclusion is that this is also part of the Windows only compatibility libraries, so can only be used on Windows.
To add that to your project, open your csproj and add a PackageReference tag for System.Security.Principal.Windows manually (or use Visual Studio's NuGet Package Manager).

How to use external library in c# script having different namespace and dll

I want to use novacode-docx in cs-script. how can I give correct reference to the assembly. I tried following but didn't work around the assembly reference missing.
//css_reference D:\lib\DocX.dll;
using System;
using System.Diagnostics;
using System.Windows.Forms;
class Script
{
[STAThread]
static public void Main(string[] args)
{
using (DocX doc = DocX.Create(#"C:\Users\name\Desktop\test.docx"))
{
doc.PageLayout.Orientation = Orientation.Landscape;
var table = doc.AddTable(12, 2);
doc.InsertTable(table);
doc.Save();
}
}
}
You cannot reference an explicit path like that for presumably security reasons. The assembly must be placed in one of the following locations and referenced as //css_reference DocX.dll;
File location The assembly to be loaded must be from one of the following locations (the order indicates the assembly search
priority):
the same directory where the script is
Default Script Library directory Script Library (%CSSCRIPT_DIR%\Lib)
Custom Script Library directory(s) (specified in the configuration console SearchDirs)
GAC
See here for more info: http://www.csscript.net/help/using_.net_assemblies.html
Drop the Docx.dll into the same folder as where the cs script is and try this:
//css_reference DocX.dll;
using System;
using System.Diagnostics;
using System.Windows.Forms;
using Novacode;
class Script
{
[STAThread]
static public void Main(string[] args)
{
using (DocX doc = DocX.Create(#"C:\Users\name\Desktop\test.docx"))
{
doc.PageLayout.Orientation = Orientation.Landscape;
var table = doc.AddTable(12, 2);
doc.InsertTable(table);
doc.Save();
}
}
}
DocX seems to be available on NuGet, so I would heavily recommend fetching the dependency from there rather than having it in a file on your local system. (This helps ensuring repeatable builds, should you share this code with others, with packaging your application, and it will also make it easier to upgrade DocX if a new version is released.)
If you're using Visual Studio, you can right-click the project in Solution Explorer and choose "Manage NuGet Packages..." to open a dialog that helps you install the package, or you can open Package Manager Console and enter Install-Package DocX.
If you're building on .NET Core without Visual Studio, just add "DocX": "1.0.0.19" to the dependencies node of your project.json.
When the package is installed, you can just do using DocX; like with any other namespace import.
Have you read this link
To add a reference in Visual C#
In Solution Explorer, right-click 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.
Select the components you want to reference, and then click OK.
Without VS:
Go to the csproj file there is a <ItemGroup> where references can be added:
<ItemGroup>
<Content Include="libs\...">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
...
There you can add libs.
both required in order to use docx.
//css_reference DocX.dll;
using Novacode;
You can also give reference to any place like
//css_reference D:\lib\DocX.dll;
using Novacode;

Categories