System.IO.FIleNotFoundException с System.IO.Ports - PullRequest
0 голосов
/ 11 апреля 2020

Я хочу использовать инфраструктуру SerialPort. NET и просто изучать веревки кодирования в среде Windows в C#. Я не новичок в кодировании, но у меня все еще есть проблемы с пониманием среды Windows.

При попытке запустить программу я получаю следующую ошибку:

System.IO.FileNotFoundException
  HResult=0x80070002
  Message=Could not load file or assembly 'System.IO.Ports, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' or one of its dependencies. The system cannot find the file specified.
  Source=ConnectionRepository.Serial
  StackTrace:
   at ConnectionRepository.Serial.SerialConnection.GetSerialPortNames() in C:\Users\KG-OFFICE\source\repos\DeepSky\ConnectionRepository.Serial\SerialConnection.cs:line 17
   at DeepSky.Form1.Form1_Load(Object sender, EventArgs e) in C:\Users\KG-OFFICE\source\repos\DeepSky\DeepSky\Form1.cs:line 25
   at System.Windows.Forms.Form.OnLoad(EventArgs e)
   at System.Windows.Forms.Form.OnCreateControl()
   at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   at System.Windows.Forms.Control.CreateControl()
   at System.Windows.Forms.Control.WmShowWindow(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WmShowWindow(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Класс, использующий SerialPort Я написал, выглядит так:

using ConnectionRepository.Interface;
using System;
using System.IO.Ports;

namespace ConnectionRepository.Serial
{
    public class SerialConnection : IConnectionRepository
    {

        static SerialPort _serialPort;

        public static string[] GetSerialPortNames()
        {
            string[] portNames = SerialPort.GetPortNames();

            return portNames;
        }

        public void CloseConnection()
        {
            throw new NotImplementedException();
        }

        public void OpenConnection()
        {
            _serialPort = new SerialPort();
            _serialPort.Open();
        }
    }
}

Основная программа:

namespace DeepSky
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            IConnectionRepository serialConnection = new SerialConnection();
            string[] portNames = SerialConnection.GetSerialPortNames();
            cBoxComPort.Items.AddRange(portNames);
        }
    }
}

, когда я добавил System.IO.Ports, он сначала не распознал его и выдал сообщение об ошибке. Затем я прочитал, мне нужно добавить его через NuGet, что я и сделал, и ошибка исчезла. Но при создании решения я получаю эту ошибку выше.

Я также добавил ссылки соответственно, но не могу заставить его работать. Вы можете мне помочь?

...