Search code examples
c#serial-portopenfiledialog

C# Error the type or namespace


I get this error

Error 2 The type or namespace name 'StringList' could not be found (are you missing a using directive or an assembly reference?)

This is my source code:

try
 {
      StringList list = new StringList().FromFile("...Filename...");
      // Read list:);
      foreach (String line in list)
      {
            // .....
      }
}

I want to open a textfile and send i trough the comport but i got the error above.


Solution

  • It just means that this class StringList is not referenced properly in your project. Make sure that you include the reference to StringList.

    Find where this class belongs (could be a binary file, another project, other .NET references). And add that project/binary file to your reference by doing this.

    Some were saying that you should add reference to Microsoft.TeamFoundation.Build.Workflow.Activities. But there is also a possibility that you are not using this one. Bottomline is that you should just reference that namespace where that class belongs to properly.

    Also I would like to add that referencing Microsoft.TeamFoundation.Build.Workflow.Activities just to use StringList would be overkill and a waste. Only add references to namespaces that you truly need.

    EDIT:

    Okay so the OP just needs to read lines of string from the text file and put it in a list. What you can do is:

            List<string> listOfStringFromYourFile = new List<string>();
            using (FileStream fs = new FileStream("yourtextfile.txt", FileMode.Open))
            {
                using (StreamReader sr = new StreamReader(fs))
                {
                    listOfStringFromYourFile.Add(sr.ReadLine());
                }
            }
            //now you can traverse to your listOfStringFromYourFile either through foreach or whatever you like.
    

    And don't forget to put using System.IO; before your namespace.