System.IO.FIleNotFoundException with System.IO.Ports

0

I want to use the SerialPort framework of .NET and just learning the ropes of coding in a Windows Environment in C#. I am not new to coding, but I have still challenges understanding the Windows environment.

I get the following error when trying to start the program:

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)

The class using SerialPort I wrote looks like this:

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();
        }
    }
}

The main program:

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);
        }
    }
}

when I added System.IO.Ports it first did not recognize it and threw an error message. Then I read, I need to add it via NuGet, which I did and the error disappeared. But when building the solution, I get this error above.

I also added the references accordingly, but can't get it to run. Can you help me out?

c#
visual-studio
serial-port
asked on Stack Overflow Apr 11, 2020 by (unknown user) • edited Apr 11, 2020 by (unknown user)

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0