按钮单击操作FIND / REPLACE

时间:2012-09-25 14:15:50

标签: c# winforms

我似乎无法让我的按钮工作。这是我第一次尝试申请。我只需要一个简单的查找/替换。我在互联网上找到了一些代码,似乎无法让它发挥作用。

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

     

错误4语法错误,'('预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 27 19 WindowsFormsApplication1

     

错误6语法错误,'('预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 28 19 WindowsFormsApplication1

     

错误8语法错误,'('预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 29 19 WindowsFormsApplication1

     

错误2预期的类,委托,枚举,接口或结构C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 54 17 WindowsFormsApplication1

     

错误1}预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 35 70 WindowsFormsApplication1

     

错误5)预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 27 25 WindowsFormsApplication1

     

错误7)预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 28 28 WindowsFormsApplication1

     

错误9)预期C:\ Users \ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 29 49 WindowsFormsApplication1

1 个答案:

答案 0 :(得分:1)

您应该了解如何构建一个类,或者C# tutorial。通常,您在类

中具有以下元素顺序
// 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() { }
    }
}

现在,当编译器试图解析你的代码文件时,由于它没有以这种方式构造,它会抱怨

当您尝试构建项目时,将弹出错误列表窗口,其中包含您指定的错误。您可以双击这些项目并解决它们。您可以做的是在每个修复程序尝试再次编译之后,因为某些错误可能是“跟进错误”,即由于第一个错误被修复而导致的错误。

在您的情况下,您在按钮单击方法中有一个方法。对于C#类,这是不允许的,因此您需要关闭buttonClick方法范围({ }括号)并将using语句移动到cs文件的顶部。

提示是缩进代码,因为格式良好的代码更容易阅读。 Visual Studio使这很容易,您可以单击“编辑”菜单,选择“高级”,然后单击“格式化文档”(记住快捷键Ctrl-k Ctrl-d)。它还可以帮助发现一些错误,例如无法比拟的括号。


编辑:另一个提示btw是右键单击 - 组织使用 - 删除和排序选项,以清除文件开头的using语句中的cludder。在许多情况下,当Visual Studio创建文件时,您不需要默认包含的一半。稍后,如果您发现自己有一个无法识别的类,请按Ctrl +。 (“ctrl dot”),您可以选择包含该类所需的using语句。