如何将文件添加到项目中并查找和替换字符然后另存为新文件? [英] How do I add a file to a project and find and replace characters then save as new file?

查看:295
本文介绍了如何将文件添加到项目中并查找和替换字符然后另存为新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段代码现在给我带来了错误......我是Visual Studio和C#的新手,我一直遇到的错误是在类结构中的= invalid token'='或接口成员声明中

|给我预期的错误类型元组必须包含至少两个元素)预期

预期类型

元组必须包含至少两个元素。 )预期

类,结构或接口成员声明中的无效标记@C:\ Users \Documents\test_shared_folder \ EDITest3.txt。



这些错误意味着什么?



我尝试过的事情:



This code is now throwing me errors... I am really new to Visual Studio and C# the errors I keep getting are under the = invalid token '=' in class struct, or interface member declaration
"|" gives me the error type expected Tuple must contain at least two elements ) expected
Type expected
Tuple must contain at lease two elements. ) expected
invalid token @"C:\Users\Documents\test_shared_folder\EDITest3.txt" in class, struct, or interface member declaration.

what do these errors mean??

What I have tried:

using System.IO;

namespace text
{
	internal class Replace
	{
		string text = File.ReadAllText(@"C:\Users\OneDrive\Desktop\EDI_Examples_855_Orders\ftpedi.edi855_o_1397.EDI.16701");
		text = text.Replace("|","*");
File.WriteAllText(@"C:\Users\Documents\test_shared_folder\EDITest3.txt", text);
	}
}

推荐答案

你需要花更多的时间来学习C#的语法,特别是关于方法:



方法(C#编程指南)| Microsoft Docs [ ^ ]



在班级,您实际上只能声明。这里有一点作弊,因为您可以为您声明的内容提供初始化。所以,在某种程度上,你也可以事(以有限的方式)。



一般来说,如果你想做的事情他们应该在方法内部。因此,例如,以下内容将允许您编译代码。但是,您需要在其他位置添加 DoSomeStuff 方法的调用。



You need to spend a bit more time learning the syntax of C#, specifically about methods:

Methods (C# Programming Guide) | Microsoft Docs[^]

At the class level, you are really only allowed to declare things. There is a bit of a cheat here, because you can provide initializations for the things you declare. So, to some extent, you can also do things (in a limited fashion).

Generally, if you want to do things they should be inside of a method. So, for example, the following would allow you to compile your code. Though, you'd need to add an invocation of the DoSomeStuff method elsewhere.

using System.IO;

namespace text
{
  internal class Replace
  {
    string text = File.ReadAllText(@"C:\Users\OneDrive\Desktop\EDI_Examples_855_Orders\ftpedi.edi855_o_1397.EDI.16701");

    internal void DoSomeStuff()
    {
      text = text.Replace("|", "*");
      File.WriteAllText(@"C:\Users\Documents\test_shared_folder\EDITest3.txt", text);
    }
  }
}





这留下了明显的鸡蛋和鸡蛋问题。如果我需要在方法中执行操作并调用该方法,那么它是如何开始的。这取决于应用程序的类型。对于控制台应用程序,这通常发生在 Main 方法中。对于其他应用程序,它通常在处理某些外部事件的方法中,并在事件发生时由.NET框架调用。



This leaves the obvious chicken-and-the-egg question. If I need to do things in a method, and invoke the method, how does it all get started. That depends on the type of application. For console applications, this usually occurs in the Main method. For other applications, its usually in a method that is handling some external event and is called by the .NET framework when the event occurs.


这篇关于如何将文件添加到项目中并查找和替换字符然后另存为新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆