nclam anti virus is not working with .net application - c#

I am getting this issue: No connection could be made because the target machine actively refused it 127.0.0.1:3310.
using System;
using System.Linq;
using nClam;
class Program
{
static void Main(string[] args)
{
var clam = new ClamClient("localhost", 3310);
var scanResult = clam.ScanFileOnServer(#"C:\inetpub\wwwroot\rarextract\parts\eicar_com.zip"); //any file you would like!
switch(scanResult.Result)
{
case ClamScanResults.Clean:
Console.WriteLine("The file is clean!");
break;
case ClamScanResults.VirusDetected:
Console.WriteLine("Virus Found!");
Console.WriteLine("Virus name: {0}", scanResult.InfectedFiles.First().VirusName);
break;
case ClamScanResults.Error:
Console.WriteLine("Woah an error occured! Error: {0}", scanResult.RawResult);
break;
}
}
}

It looks like you do not have the ClamWin service installed or running on the machine that is executing your code. I wrote some directions a to install it here: http://architectryan.com/2011/05/19/nclam-a-dotnet-library-to-virus-scan/.

Related

How to deploy twincat 3 project to PLC programmatically

I´m looking for a way to activate the configuration and update the boot project via C#.
My Twincat 3 project is compiled and all necessary file are in the /_Boot folder.
Next step is a C# programm (actually unit tests) that loads and executes the project on my PLC.
So far I have read through Beckhoff Information System, but couldn´t find any hint.
You need the Twincat Automation Interface API in order to activate your configuration and start the PLC.
An example from the official documentation:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EnvDTE100;
using System.IO;
using TCatSysManagerLib;
namespace ActivatePreviousConfiguration{
class Program
{
static void Main(string[] args)
{
Type t = System.Type.GetTypeFromProgID("VisualStudio.DTE.10.0");
EnvDTE.DTE dte = (EnvDTE.DTE)System.Activator.CreateInstance(t);
dte.SuppressUI = false;
dte.MainWindow.Visible = true;
EnvDTE.Solution sol = dte.Solution;
sol.Open(#"C:\Temp\SolutionFolder\MySolution1\MySolution1.sln");
EnvDTE.Project pro = sol.Projects.Item(1);
ITcSysManager sysMan = pro.Object;
sysMan.ActivateConfiguration();
sysMan.StartRestartTwinCAT();
}
}
}
There are also many other things you can do with this api, for example generate code for your PLC..
You can find the documentation here:
Automation Interface pdf
If you only have the _Boot folder at your disposal, you just have to copy the content of _Boot\TwinCAT RT(x64)\Plc to your target boot folder C:\TwinCAT\3.1\Boot\Plc and start the PLC via ADS-Command.
The PLC will boot with the replaced compiled project.
Here an example from the official ADS-Documentation for starting the plc:
static void Main(string[] args)
{
//Create a new instance of class TcAdsClient
TcAdsClient tcClient = new TcAdsClient();
try
{
// Connect to local PLC - Runtime 1 - TwinCAT2 Port=801, TwinCAT3 Port=851
tcClient.Connect(851);
Console.WriteLine(" PLC Run\t[R]");
Console.WriteLine(" PLC Stop\t[S]");
Console.WriteLine("\r\nPlease choose \"Run\" or \"Stop\" and confirm with enter..");
string sInput = Console.ReadLine().ToLower();
//Process user input and apply chosen state
do{
switch (sInput)
{
case "r": tcClient.WriteControl(new StateInfo(AdsState.Run, tcClient.ReadState().DeviceState)); break;
case "s": tcClient.WriteControl(new StateInfo(AdsState.Stop, tcClient.ReadState().DeviceState)); break;
default: Console.WriteLine("Please choose \"Run\" or \"Stop\" and confirm with enter.."); sInput = Console.ReadLine().ToLower(); break;
}
} while (sInput != "r" && sInput != "s");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.ReadKey();
}
finally
{
tcClient.Dispose();
}
}

How to see output data through Console Application (.NET CORE) C#

I have made a console application with which I need to see system information.
When I run the application, I can only see the following on the console:
Usage: sysinfo <cpu|win|net|host|user>
Press any key to continue . . .
I have written this program as console application (.net core), I don't know why I cannot see the information about my system?
My code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemInfo
{
class Program
{
class SysInfo
{
public string win, net, cpu;
public string hostname, username;
public SysInfo()
{
net = Environment.Version.ToString();
win = Environment.OSVersion.ToString();
cpu = Environment.ProcessorCount.ToString();
hostname = Environment.MachineName.ToString();
username = Environment.UserName.ToString();
}
}
static void Main(string[] args)
{
string p;
SysInfo info = new SysInfo();
if (args.Length > 0) p = args[0];
else p = "null";
switch (p)
{
case "cpu":
Console.WriteLine("CPU count: {0}", info.cpu);
break;
case "win":
Console.WriteLine("Windows Version: {0}", info.win);
break;
case "net":
Console.WriteLine(".NET Version: {0}", info.net);
break;
case "host":
Console.WriteLine("Hostname: {0}", info.hostname);
break;
case "user":
Console.WriteLine("Username: {0}", info.username);
break;
default:
Console.WriteLine("Usage: sysinfo <cpu|win|net|host|user>");
break;
}
}
}
}
If you run this in debug (via visual studio) you will need to pass the args.
Go to Project-> Properties. Then click on the Debug tab,
and fill in your arguments in the textbox called Command line
arguments.
refference
If you run the actual compiled exe file, then simply add the desired argument.
For example:
c:\>appname.exe cpu
c:\>appname.exe win
c:\>appname.exe user
....
The console output is the expected behaviour for your application. To get system information, you need to pass a parameter, e.g. like this sysinfo cpu
Edit: If you want to read the strings from the Console you could do it e.g. in a loop
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SystemInfo
{
class Program
{
class SysInfo
{
public string win, net, cpu;
public string hostname, username;
public SysInfo()
{
net = Environment.Version.ToString();
win = Environment.OSVersion.ToString();
cpu = Environment.ProcessorCount.ToString();
hostname = Environment.MachineName.ToString();
username = Environment.UserName.ToString();
}
}
static void Main()
{
string p;
SysInfo info = new SysInfo();
while (true)
{
p = Console.ReadLine();
switch (p)
{
case "cpu":
Console.WriteLine("CPU count: {0}", info.cpu);
break;
case "win":
Console.WriteLine("Windows Version: {0}", info.win);
break;
case "net":
Console.WriteLine(".NET Version: {0}", info.net);
break;
case "host":
Console.WriteLine("Hostname: {0}", info.hostname);
break;
case "user":
Console.WriteLine("Username: {0}", info.username);
break;
default:
Console.WriteLine("Usage: sysinfo <cpu|win|net|host|user>");
break;
}
}
}
}
}
On a sidenote, I'd then also add a case for exiting the loop.
You are reading args when the program starts. Did you forget to pass arguments to your app? Your switch condition is not matched with one of your cases, then it prints out as default case.
You can pass arguments in Visual Studio as answered before: https://stackoverflow.com/a/3697320/3678882

C#: How to use Service References in Visual Studio 2015 (Move from Linqpad)

I would like to migrate a C# Program to Visual Studio. I'm fairly new working with C#, however I could have learned some stuff in Linqpad.
In Linqpad I imported my dll as an additional Reference and could use it. The dll was generated with:
"wsdl http://localhost/WebService.asmx"
and
"csc /t:library WebService.cs"
Linqpad Code which worked was like:
WebService ws = new WebService();
void Main()
{
try
{
var connect = ws.Logon("localhost", Port, "", "username", "password);
var morefiles = false;
// do other stuff....
do
{
var results = ws.search(connect, "Culture", "Culture", searchCondition, morefiles);
} while (morefiles == true);
}
catch (Exception x)
{
Console.WriteLine(x.Message);
Console.WriteLine(x.StackTrace);
}
finally {
Console.WriteLine("Search done");
}
}
Now, I would like to use it in Visual Studio. In VS I imported it as a Service References with the given URL, and it showed me the necessary stuff.
Now when I try to use it in my code I get an error message which tells me:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Testproject.WebService;
namespace Testproject
{
class Program
{
static void Main(string[] args)
{
WebService ws = new WebService();
Console.WriteLine("Test");
Console.ReadKey();
}
}
}
The error message is in the line:
WebService ws = new WebService();
It shows me this error message:
WebService is a 'namespace' but is used like a 'type'
I also found out, that "Logon" which I used in Line 6 in Linqpad is in:
WebService.WebServiceSoap
However, if I change the line in:
WebService.WebServiceSoap ws = new WebService.WebServiceSoap();
I also get an error message:
Cannot create an instance of the abstract class or interface
I tried different combinations also, but I don't exactly no, what can be wrong. Can you help me?

Installing a printer using ManagementClass gives me an unidentified error in C#

I am fairly new to C#.
I am trying to install a printer programatically using this post I found however I get an exception here:
inputParam.SetPropertyValue("Print", "\\\\http://ip.of.my.server");
An exception of the type "System.Management.ManagementException" has been found in System.Management.dll.
Additional information: Not found.
Here is the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management;
namespace Printer
{
class Installation
{
static public string addprinter()
{
ManagementClass win32Printer = new ManagementClass("Win32_Printer");
ManagementBaseObject inputParam = win32Printer.GetMethodParameters("AddPrinterConnection");
inputParam.SetPropertyValue("Print", "\\\\http://ip.of.my.server");
ManagementBaseObject result = (ManagementBaseObject)win32Printer.InvokeMethod("AddPrinterConnection", inputParam, null);
uint errorCode = (uint)result.Properties["returnValue"].Value;
switch (errorCode)
{
case 0:
return ("Successfully connected printer.");
case 5:
return ("Access Denied.");
case 123:
return ("The filename, directory name, or volume label syntax is incorrect.");
case 1801:
return ("Invalid Printer Name.");
case 1930:
return ("Incompatible Printer Driver.");
case 3019:
return("The specified printer driver was not found on the system and needs to be downloaded.");
}
return ("Unkown error.");
}
}
}
And here is the form that calls the code, although I don't think the probleme is there:
private void Send_Click(object sender, EventArgs e)
{
if (true)
{
MessageBox.Show(Installation.addprinter());
}

What's the usage of StackExchange.Redis on redis cluster mode

This is using stackexchange.redis v1.1.603, .net 4.6, console application.
Here is my codes:
using System;
using System.Collections.Generic;
using StackExchange.Redis;
namespace RedisClusterTesting
{
class Program
{
static void Main(string[] args)
{
string ip = "192.168.1.20:30001,192.168.1.20:30002,192.168.1.20:30003,resolvedns=1";
var conf = ConfigurationOptions.Parse(ip);
conf.CommandMap = CommandMap.Create(new HashSet<string> {
"INFO", "CONFIG", "CLUSTER","PING", "ECHO", "CLIENT"
}, false);
using (ConnectionMultiplexer conn = ConnectionMultiplexer.Connect(conf))
{
var db = conn.GetDatabase();
Do(db);
}
Console.ReadKey();
}
private static void Do(IDatabase db)
{
/*here throws MOVED Exception:MOVED 12182 192.168.1.20:30003*/
db.StringSet("foo", "changed");
Console.WriteLine("foo now:" + db.StringGet("foo").ToString());
}
}
}
Always show the message "MOVED: 12586[192.168.1.20:30003]".
I search all the offcial document and on the Internet, can't find the right answer. It's OK while I use redis-cli.
How to fix this?Do I need process the exception in my code?If, how?
Seems like you may be running into this issue: https://github.com/StackExchange/StackExchange.Redis/issues/248. If you put a 1 second sleep between your Connect() call and your Do() call, I would guess that you will see the issue go away.

Categories

Resources