0

我似乎无法让我的按钮工作。这是我第一次尝试制作应用程序。我只需要一个简单的查找/替换。我在互联网上找到了一些代码,但似乎无法正常工作。

http://pastebin.com/9v6TEFMs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Deneuralyzer : Form
    {
        public Deneuralyzer()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            using System;
            using System.IO;
            using System.Text.RegularExpressions;


                    string filePath = @"C:\Program Files (x86)\location\to\application\textfile.txt";
                    string searchText = "Count,2,";
                    string replaceText = "Count,200,";
                    ReplaceInFile(filePath, searchText, replaceText);


                    static public void ReplaceInFile(string filePath, string searchText, string replaceText)
                    {

                        StreamReader reader = new StreamReader(filePath);
                        string content = reader.ReadToEnd();
                        reader.Close();

                        content = Regex.Replace(content, searchText, replaceText);

                        StreamWriter writer = new StreamWriter(filePath);
                        writer.Write(content);
                        writer.Close();
                    }

        }

    }
}

另外,是否需要做一些特定的事情,以便应用程序可以编辑文件?因为手工操作我必须更改文件权限和所有权。

当我运行测试时出错

错误 3 类型或命名空间定义,或预期文件结尾 C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 59 1 WindowsFormsApplication1

Error 4 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 27 19 WindowsFormsApplication1

Error 6 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 19 WindowsFormsApplication1

Error 8 Syntax error, '(' expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 29 19 WindowsFormsApplication1

Error 2 Expected class, delegate, enum, interface, or struct C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 54 17 WindowsFormsApplication1

Error 1 } expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 35 70 WindowsFormsApplication1

Error 5 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 27 25 WindowsFormsApplication1

Error 7 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 28 28 WindowsFormsApplication1

Error 9 ) expected C:\Users\Jack\Documents\Visual Studio 2012\Projects\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs 29 49 WindowsFormsApplication1

4

1 回答 1

1

You should read up on how to structure a class, or perhaps a C# tutorial. Normally you have the following order of elements in a class

// using statements, other components you use in your class
using System;
// namespace name (a group so to speak)
namespace NamespaceName {

    // class, this gets nested under a namespace
    public class MyClass {

        // private variables
        private int myVariable;

        // constructors
        public MyClass() {
            // this is where you create the instance, set variables and stuff
            myVariable = 314;
        }

        // methods
        public void DoSomething() {
            ++myVariable;
        }

        private void anotherMethod() { }
    }
}

Now, when the compiler tries to parse your codefile, since it's not structured in this manner, it complains

When you try to build the project the Error list window pops up with the errors you specified. You can double click each of these items and address them. What you can do is after each fix try to compile again, since some errors might be "follow up errors", i.e. errors which are fixed due to the first error being fixed.

In your case you have a method in the button click method. This is not allowed for a C# class, so you need to close the buttonClick method scope (the { } brackets that is) and move the using statements to the top of the cs file.

A tip is to indent your code since well formatted code is easier to read. Visual Studio makes this easy, you can click the Edit menu, choose Advanced and click Format Document (remember the shortcut Ctrl-k Ctrl-d). It can also help spot some errors, such as unmatched brackets.


Edit: Another tip btw is the Right click - Organize usings - Remove and sort option, to clear the cludder in the using statements in the beginning of the file. In many cases you don't need half of the ones which are included by default when Visual Studio creates a file. Later, if you find that you have a unrecognized class, press Ctrl + . ("ctrl dot") and you get the option to include the using statement needed for that class.

于 2012-09-25T16:46:28.990 回答