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

エラー 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

4

1 に答える 1

1

クラスを構築する方法、またはおそらくC# チュートリアルを読む必要があります。通常、クラスには次の要素の順序があります

// 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 を思い出してください)。また、括弧の不一致などのエラーを特定するのにも役立ちます。


編集:もう1つのヒントは、右クリック-usingの整理-削除と並べ替えオプションで、ファイルの先頭にあるusingステートメントの混乱を解消します。多くの場合、Visual Studio がファイルを作成するときに既定で含まれているものの半分は必要ありません。後で、認識されていないクラスがあることがわかった場合は、Ctrl + を押します。("ctrl dot") を入力すると、そのクラスに必要な using ステートメントを含めるオプションが表示されます。

于 2012-09-25T16:46:28.990 に答える