What is the proper namespace for IPHostEntry and DNS? - c#

My program uses IPHostEntry and DNS, i know i have to add System.Object, System.Net.IPHostEntry, System.Net.DNS to my project reference but i can not find it in the list.
After the System.IO.Log the next to follow is System. Management (no System.Object)
After the System.Net is System.Numerics (no System.Net.IPHostEntry or System.Net.DNS)
I am using .NET 4.0
What do i do to add the following reference and make it work?
i have the following codes
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Windows.Forms;
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}

Classes belong to assemblies and exist in namespaces.
You reference assemblies. You specify classes in your code by using their full name (namespace + class name) or you can add certain namespaces in using directives.
So you need to find, from the documentation, where each class belongs and then ensure that you're referencing those assemblies:
IPHostEntry and Dns belong to the System.Net namespace and are in the System assembly.
Object belongs to the System namespace and is in mscorlib.
You almost certainly already have references to these assemblies. But you might want to add using directives at the head of your code file.
Add:
using System.Net;
to your using directives (you already have a using System; for referring to Object)

Related

Available Serial ports are not shown in Visual studio

I am recently working on a project for Serial port communication, I want to show the available serial ports in a comboBox, But while running it is not showing the available list of com ports.
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace Serial_application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
getPortsNames();
}
void getPortsNames()
{
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
}
}
}
There are no old style com ports on most modern computers, they are USB converters. If the USB device driver has not been installed there are no ports listed in the computer. Just solved this problem for myself.

TcpChannel could not be found

I'm following a lab tutorial on .NET remoting and for that I need to create TcpChannel object. For that I have added using System.Runtime.Remoting namespace but it still gives me an error The type or namespace name 'TcpChannel' could not be found (are you missing a using directive or an assembly reference?)
I'm using Visual Studio 2015 Community Edition. I can't figure out what is causing this problem. I even googled it. Here's my code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.Remoting;
namespace RemotingServer
{
class Program
{
static void Main(string[] args)
{
TcpChannel channel = new TcpChannel(8001);
}
}
}
The TcpChannel class is located in System.Runtime.Remoting.Channels.Tcp, so you have to get using that. Also check if you reference System.Runtime.Remoting.dll in your project.

Assembly reference for filedownloader?

What is the assembly reference for a Filedownloader in c#?
I am looking for to download the files from ftp to my local drive 'c' ,
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
FileDownloader f = new FileDownloader();
which reference should I need to add for the filedownloader?
I guess that you are trying to download files from FTP,and you don't have to use the FileDownloader class.
You can use this link for example: "http://msdn.microsoft.com/en-us/library/ms229711(v=vs.110).aspx".
You can also use an open source to do that, try looking at this link: "http://www.codeproject.com/Tips/443588/Simple-Csharp-FTP-Class"
There is an example of how simple it is to use (From the link above):
ftp ftpClient = new ftp(#"ftp://10.10.10.10/", "user", "password");
ftpClient.download("etc/test.txt", #"C:\Users\metastruct\Desktop\test.txt");

C# Blocking IP by modifying Windows 7 Firewall

I'm starting to build an anti-ddos application in C#, it's going to block IP's by checking how many connections there are, in an amount of time, but I can't find whats needed for NetFwMgrType.
Here's code, I just started:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NETCONLib;
using NATUPNPLib;
using NetFwTypeLib;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
public Form1()
{
InitializeComponent();
}
}
}
The errors I get is:
Error 1 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.NetFwMgrType'
Error 2 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.mgr'
(I added references: \Windows\System32\hnetcfg.dll and \Windows\System32\FireWallAPI.dll)
Thanks for answer.
Oh and if you know how to block IPs with Firewall modifying, as well, it would save me a lot of googling:)

c# youtube api error

hi i am trying to make an application which can submit a comment to a youtube video. I am using .net framework 4.0.
when i click a button it spits out this error
Could not load file or assembly 'Google.GData.Client, Version=1.7.0.1, Culture=neutral, PublicKeyToken=04a59ca9b0273830' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)
the code i am using is as follows
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.IO;
using System.Web;
using System.Net;
using HtmlAgilityPack;
using System.Xml;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.YouTube;
using Google.GData.Extensions.MediaRss;
using Google.YouTube;
the rest of the code is
string developerKey;
string username;
string password;
developerKey = "mykey";
username = "my username";
password = "my pass";
YouTubeRequestSettings settings = new YouTubeRequestSettings("youtube app", developerKey, username, password);
YouTubeRequest request = new YouTubeRequest(settings);
Uri videoEntryUrl = new Uri("my vid");
Video video = request.Retrieve<Video>(videoEntryUrl);
Comment c = new Comment();
c.Content = "This is my comment from my app";
request.AddComment(video, c);
You're missing an assembly. You must have had it handy as a reference when compiling your app, but it's not there where you're testing.
So it has nothing to do with your code, but your build and deploy process — i.e., shell scripts, NAnt, VS.
A typical YouTube client will have the following DLL's in the same directory as the EXE (or DLL).
Google.GData.Client.dll
Google.GData.Extensions.dll
Google.GData.YouTube.dll
If not, then they need to be installed in the GAC, or otherwise locatable using .NET's assembly binding configuration. Probably you want the first option, though.

Categories

Resources