我似乎無法讓我的按鈕正常工作。這是我第一次嘗試製作應用程序。我只需要一個簡單的查找/替換。我在互聯網上發現了一些代碼,似乎無法使其正常工作。按鈕點擊操作查找/替換
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:\用戶\千斤頂\文檔\的Visual Studio 2012 \項目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs的59 1 WindowsFormsApplication1
錯誤4語法錯誤, '(' 預期C:\用戶\傑克\文件\的Visual Studio 2012 \項目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs中27 19 WindowsFormsApplication1
錯誤6語法錯誤,'('預期的C:\用戶\ Jack \ Documents \ Visual Studio 2012 \ Projects \ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs 28 19 WindowsFormsApplication1
錯誤8語法錯誤,'('expected 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:\用戶\傑克\文件\的Visual Studio 2012 \項目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs中27 25 WindowsFormsApplication1
錯誤7)預期C:\用戶\傑克\文件\的Visual Studio 2012 \項目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1.cs中28 28 WindowsFormsApplication1
錯誤9)預期C:\用戶\傑克\文件\的Visual Studio 2012 \項目\ WindowsFormsApplication1 \ WindowsFormsApplication1 \ Form1中.cs 29 49 WindowsFormsApplication1
請在此處發佈您的代碼,而不是pastebin。不是每個人都可以從工作中查看pastebin - 也可以在完成的所有 – BugFinder
上更輕鬆地閱讀您的問題。剛剛發現CTRL + K,我曾經爲每行添加四個空格,耗時。 :\ :) –
我唯一能想到的就是Windows 7上的UAC可能會阻止您的應用程序寫入該區域..如果該文件更明智一些,該怎麼辦?該代碼看起來是合理的面值 - 當然,如果你尋找「我的名字」,並找到「我的名字」它不會取代它..否則代碼應該工作 - 你可以用另一個字符串測試它..你也可以使用file.ReadAllText來讀取文件 - 但林想知道如果你需要添加一些正則表達式選項,如RegexOptions.MultiLine – BugFinder