【发布时间】:2012-09-17 02:02:47
【问题描述】:
我似乎无法让我的按钮工作。这是我第一次尝试制作应用程序。我只需要一个简单的查找/替换。我在互联网上找到了一些代码,但似乎无法正常工作。
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
【问题讨论】:
-
请在此处发布您的代码,而不是在 pastebin 上。不是每个人都可以在工作中查看 pastebin - 它还可以让所有人更轻松地阅读您的问题
-
完成。刚发现CTRL+K,以前每行加四个空格,很费时间。 :\ :)
-
我能想到的唯一一件事是 Windows 7 上的 UAC 可能会阻止您的应用程序写入该区域。如果文件在某个更合理的地方怎么办?代码看起来很合理——当然,如果你寻找“我的名字”并且它发现“我的名字”它不会替换它。否则代码应该可以工作——你可以用另一个字符串测试它。你也可以使用 file.ReadAllText 读取文件-但我想知道是否需要添加一些正则表达式选项,例如 RegexOptions.MultiLine
-
按钮本身不起作用。我发布了上面的错误代码。
-
啊 - 你看,你最初并没有发布你的错误 - 这使它更像是一个猜谜游戏。