在C中调用静态方法#
我实现了一个插件(使用pGina软件),允许用户通过扫描NFC标签在其计算机中验证用户名/密码。在C中调用静态方法#
我使用了一个我发现名为CSharp PC/SC Wrapper for .NET的程序来读取标签ID。每次扫描标签时,程序都会将ID写入文本文件,并检查ID是否与字符串上设置的ID相同。
if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina")
&& text.Equals("UID = 0x04 82 EC BA 7A 48 80"))
该插件设置为查找读取ID(PC/SC Wrapper)的.exe文件。一切正常。但是,我不认为读者程序是在不同的文件中。我希望一切都在插件文件中。
我创建了一个方法并从执行标签ID(runme())读取的包装中复制代码,但我不知道如何用方法I替换调用.exe文件的行创建
ProcessStartInfo ps = new ProcessStartInfo(@"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");
有什么建议?我是新的C#
下面是我用含有读取ID
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using pGina.Shared.Types;
using log4net;
using System.IO;
using System.Diagnostics;
using GS.PCSC;
using GS.Apdu;
using GS.SCard;
using GS.Util.Hex;
using System.Threading;
namespace HelloPlugin
{
public class PluginImpl : pGina.Shared.Interfaces.IPluginAuthentication
{
private ILog m_logger;
private static readonly Guid m_uuid = new Guid("CED8D126-9121-4CD2-86DE-3D84E4A2625E");
public PluginImpl()
{
m_logger = LogManager.GetLogger("pGina.Plugin.HelloPlugin");
}
public string Name
{
get { return "Hello"; }
}
public string Description
{
get { return "Authenticates users with 'hello' in the username and 'pGina' in the password"; }
}
public Guid Uuid
{
get { return m_uuid; }
}
public string Version
{
get
{
return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public void Starting()
{
}
public void Stopping() { }
public BooleanResult AuthenticateUser(SessionProperties properties)
{
UserInformation userInfo = properties.GetTrackedSingle<UserInformation>();
ProcessStartInfo ps = new ProcessStartInfo(@"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");
Process.Start(ps);
Thread.Sleep(2000);
string text = File.ReadAllText(@"C:\Users\Student\Desktop\text.txt", Encoding.UTF8);
text = text.Trim();
if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina") && text.Equals("UID = 0x04 82 EC BA 7A 48 80"))
{
// Successful authentication
m_logger.InfoFormat("Successfully authenticated {0}", userInfo.Username);
return new BooleanResult() { Success = true };
}
// Authentication failure
m_logger.ErrorFormat("Authentication failed for {0}", userInfo.Username);
return new BooleanResult() { Success = false, Message = "Incorrect username or password." };
}
static void runme()
{
ConsoleTraceListener consoleTraceListener = new ConsoleTraceListener();
Trace.Listeners.Add(consoleTraceListener);
PCSCReader reader = new PCSCReader();
string cardid = "";
try
{
reader.Connect();
reader.ActivateCard();
RespApdu respApdu = reader.Exchange("FF CA 00 00 00"); // Get NFC Card UID ...
if (respApdu.SW1SW2 == 0x9000)
{
Console.WriteLine("UID = 0x" + HexFormatting.ToHexString(respApdu.Data, true));
cardid = "UID = 0x" + HexFormatting.ToHexString(respApdu.Data, true);
cardid = cardid.Trim();
}
}
catch (WinSCardException ex)
{
Console.WriteLine(ex.WinSCardFunctionName + " Error 0x" +
ex.Status.ToString("X08") + ": " + ex.Message);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
string path = @"C:\Users\Student\Desktop\text.txt";
string text2write = cardid;
System.IO.StreamWriter writer = new System.IO.StreamWriter(path);
writer.Write(text2write);
writer.Close();
reader.Disconnect();
Environment.Exit(0);
Console.WriteLine("Please press any key...");
Console.ReadLine();
}
}
}
}
您已经创建了一个名为PluginImpl
类,并在类中声明的代码的方法插件代码方法runme
。要从任何地方调用该方法,您需要编写PluginImpl.runme()
。
由于您已将课程放在命名空间HelloPlugin
中 - 如果调用的*.cs
文件位于不同的命名空间中,您需要在顶部有using HelloPlugin
指令。
就这样!
这可能是我误解了你的问题,如果是的话请重新提出你的问题,并给我发一条评论。
如果要替换的方法调用的行
ProcessStartInfo ps = new ProcessStartInfo(
@"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\"
+"ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");
相反,你想是这样的
ProcessStartInfo ps = runme();
因为你是从类中调用的静态方法,你不需要前缀PluginImpl.
。
好吧,现在它会抱怨runme不会返回ProcessStartInfo。你将需要改变runme,以便它可以。 ProcessStartInfo的任何子类都可以。
static ProcessStartInfo runme()
{
// ... Some code
ProcessStartInfo toReturn = new ProcessStartInfo(//...
);
// ... More code
return toReturn;
}
一旦我使用PluginImpl.runme(),我该如何替换ProcessStartInfo()来调用该方法。 ProcessStartInfo ps = new ProcessStartInfo(@“C:\ Users \ Student \ Desktop \ CSharpPCSC \ CSharpPCSC \ ExamplePCSCReader \ bin \ Release \ ExamplePCSCReader.exe”); –
@mj_h我希望我最近的编辑能够解决您的问题。如果这还不清楚,让我知道。 –