Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, March 24, 2013 2:08 PM | 1 vote
I've seen this question asked here before, but the ultimate answer after reading the entire post was "Okay, thanks I got it", not very helpful. So, I haven't seen an answer that fixed the problem.
The scenario is this... I am on .NET Framework 4.5, building a C# web application in VisualStudio 2012 Express.
I'm trying to use Microsoft.VisualBasic.FileIO.TextFieldParser in my code. From what I've read it is appropriate to do so by adding a project reference to Microsoft.VisualBasic, which I've done, and coding <%@ Import Namespace="Microsoft.VisualBasic"%>; in my view (.aspx). However when I code...
Microsoft.VisualBasic.FileIO.TextFieldParser parser = new Microsoft.VisualBasic.FileIO.TextFieldParser(new StringReader(xxxxxxxx));
...in the view and rebuild the solution it returns errors: The type or namespace name 'FileIO' does not exist in the namespace 'Microsoft.VisualBasic' (are you missing an assembly reference?)
I know the reference is in the correct project because when I add and remove it I see it come and go from the references folder. I know the code I'm trying to reference exists because I can see it in Object Browser. I've even tried adding the reference using Object Browser instead of through the menus and although it gets added, the results are the same.
The solution it's in is a bit complicated so I tried just creating a test stand alone C# project and I get the same results. I also tried creating a test stand alone Visual Basic project, and sure enough, it works there as advertised. The Microsoft.VisualBasic references in both projects point to the same file, C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\NETFramework\v4.5\Microsoft.VisualBasic.dll. I'm by no means an expert on Visual Studio so I'm guessing that it's just something I'm missing in the configuration, but I'm at a loss to figure it out.
I've spent days on this, so any help anyone could offer would be greatly appreciated.
All replies (5)
Monday, March 25, 2013 7:43 AM
Hi Tim,
I create a C# console application and add the reference of Microsoft.VisualBasic. Then I coding like:
using (Microsoft.VisualBasic.FileIO.TextFieldParser MyReader =
new Microsoft.VisualBasic.FileIO.TextFieldParser("D:\\a.txt"))
{
MyReader.TextFieldType = Microsoft.VisualBasic.FileIO.FieldType.Delimited;
MyReader.SetDelimiters(",");
string[] currentRow = null;
while (!MyReader.EndOfData)
{
try
{
currentRow = MyReader.ReadFields();
foreach (var currentField in currentRow)
{
//set values for your object here
Console.WriteLine(currentField);
}
}
catch (Microsoft.VisualBasic.FileIO.MalformedLineException ex)
{
//handle the exception
}
}
}
It can run well in my computer. Can post the print screen here to show the issue you have?
Best regards,
Ego [MSFT]
MSDN Community Support | Feedback to us
Develop and promote your apps in Windows Store
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Monday, March 25, 2013 2:31 PM
Okay, I had everything you requested entered and when I hit Submit got the message... Body text cannot contain images or links until we are able to verify your account.
So if you tell me how to fix that I can get it to you.
Monday, March 25, 2013 2:41 PM
An alternate would be to simply use LINQ
var People = (from line in IO.File.ReadAllLines("People.cvs") where line.Length > 0 let Items = line.Split(',') select Items).ToList;
Sample data
Joe,Smith, 123 Mock Lane,Salem,15699,555-555-1111Mary,Wilson, 444 Apple St,Salem,99299,555-555-2222
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
Monday, March 25, 2013 3:00 PM
I'm trying to use TextFieldParser because the data I'm reading is CSV and some of the fields are quoted text fields that contain commas. TextFieldParser takes care of them quite nicely. I looked at ReadAllLines and it does not look like it will do the same. Thanks for trying though. If you know of another way to do what I'm trying to do I'm open. TextFieldParser is attractive because it does what I want with about 3 lines of code.
Monday, March 25, 2013 3:59 PM
I don't code in ASP.NET so the following may not be possible.
Can you create a class project that has all code using TextFieldParser in it that returns back a list to your web application? This means the class library (DLL) project is only responsible for reading data into a known structure to your web app.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.