在 C# 中调用静态方法

Calling a static method in C#

我实现了一个插件(使用 pGina 软件)允许用户通过扫描 NFC 标签在他们的计算机中验证 username/password。

我使用了一个名为 CSharp PC/SC Wrapper for .NET 的程序来读取标签 ID。每次扫描标签时,程序都会将 ID 写入文本文件并检查 ID 是否与字符串中设置的相同。

if (userInfo.Username.Contains("hello") && userInfo.Password.Contains("pGina") 
  && text.Equals("UID  = 0x04 82 EC BA 7A 48 80"))

插件设置为查找读取ID的.exe文件(PC/SCWrapper)。一切正常。但是,我不希望 reader 程序位于不同的文件中。我希望一切都在插件文件中。

我创建了一个方法并从执行读取标签 ID (runme()) 的包装器中复制了代码,但我不确定如何用该方法替换调用 .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 的 class,并在其中 class 声明了方法 runme。要从任何地方调用该方法,您需要编写 PluginImpl.runme().

由于您已将 class 放入命名空间 HelloPlugin - 如果调用 *.cs 文件位于不同的命名空间中,您将需要一个 using HelloPlugin顶部的指令。

就这些了!

可能是我误解了你的问题,如果是的话请重新表述你的问题并给我发评论。


如果要替换行

ProcessStartInfo ps = new ProcessStartInfo(
         @"C:\Users\Student\Desktop\CSharpPCSC\CSharpPCSC\"
        +"ExamplePCSCReader\bin\Release\ExamplePCSCReader.exe");

用方法调用代替,你想要这样的东西

ProcessStartInfo ps = runme();

由于您是从 class 中调用静态方法,因此不需要 PluginImpl. 前缀。

好的,现在它会抱怨 runme 没有 return ProcessStartInfo。您将需要更改 runme 以使其生效。 ProcessStartInfo 的任何子class 都可以。

static ProcessStartInfo runme()
{
   // ... Some code

   ProcessStartInfo toReturn = new ProcessStartInfo( //...
   );

   // ... More code

   return toReturn;
}