[英]C# - How do I build/debug this code in VS2015?
I was given some code for a simple text file parser that I would like to build on and modify. 给了我一些我想在其上进行修改的简单文本文件解析器的代码。 It was built in VS and I've installed VS2015 Community so that I can work with it, but for the life of me I can't figure out how to set it up in VS2015.
它是在VS中构建的,并且我已经安装了VS2015社区,以便可以使用它,但是对于我一生来说,我不知道如何在VS2015中进行设置。
A snippet of the very beginning of the code is below. 下面是代码开头的一小段。 Do I build it as a class, or a console application, or something else?
是否将其构建为类,控制台应用程序或其他? How can I modify it to read a local file line by line?
如何修改它以逐行读取本地文件?
Any help would be tremendously appreciated! 任何帮助将不胜感激!
using System;
using System.Collections.Generic;
using System.IO;
public static class Cawk
{
public static IEnumerable<Dictionary<string, object>> Execute(StreamReader input)
{
Dictionary<string, object> row = new Dictionary<string, object>();
string line;
//string[] lines = File.ReadAllLines(path);
//read all rows
while ((line = input.ReadLine()) != null)
{
The snippet you posted is a class file definition. 您发布的代码段是一个类文件定义。
You will need a class called Cawk.cs with that code inside. 您将需要一个名为Cawk.cs的类,其中包含该代码。
To run it, you will need something to invoke it, either a console app or a unit test will do. 要运行它,您需要一些东西来调用它,控制台应用程序或单元测试都可以。
For a console app: 对于控制台应用程序:
To debug it, put a breakpoint on a line and press F5. 要调试它,请将断点放在一行上,然后按F5。
Considering you've never build an application in visual studio, the easiest way are: 考虑到您从未在Visual Studio中构建过应用程序,最简单的方法是:
That will give you a basic console application with one file Program.cs that has static method Main() inside. 这将为您提供一个基本的控制台应用程序,其中包含一个文件Program.cs,其中包含静态方法Main()。 Now let's add the new class.
现在让我们添加新类。
You will create a new file "Cawk.cs" for Cawk class. 您将为Cawk类创建一个新文件“ Cawk.cs”。 Let's fill it up.
让我们填补它。
So it will become something like: 因此它将变为:
using System;
using System.Collections.Generic;
using System.IO;
namespace ConsoleApplication1
{
public static class Cawk
{
...
The method Execute() accepts a StreamReader object. 方法Execute()接受StreamReader对象。 This object reads data from a byte stream - a flow of data coming from some kind of source - user input, file, another application, etc.
该对象从字节流中读取数据-某种来源的数据流-用户输入,文件,其他应用程序等。
In order to parse a file through Cawk you need to instantiate StreamReader first with a proper constructor , and dispose it afterwards (see " using " statement in C#). 为了通过Cawk解析文件,您需要首先使用适当的构造函数实例化StreamReader,然后再对其进行处理(请参阅C#中的“ using ”语句)。
Let me provide you an example of code: 让我为您提供示例代码:
using (var sr = new StreamReader("C:\temp\file.txt"))
{
var results = Cawk.Execute(sr);
foreach (item in results)
{
// do something with item which is Dictionary<string, object>
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.