如果语句有帮助;符合特定标准的不同颜色
我是初学者,If 语句是我的弱点。我有一个简单的程序,可以显示位于某个文件夹中的文件名。但是,某些文件可能包含以 LIFT 开头的行。我想捕获那些具有该行的文件,并以不同的颜色(最好是红色)显示文件名。这是我到目前为止所得到的:任何帮助将不胜感激!谢谢!
public partial class ShippedOrders : System.Web.UI.Page
{
class Program
{
static void Main()
{
string[] array1 = Directory.GetFiles(@"C:\Kaplan\Replies\");
string[] array2 = Directory.GetFiles(@"C:\Kaplan\Replies\", "*.REP");
Console.WriteLine("---Files:---");
foreach (string name in array1)
{
Console.WriteLine(name);
}
Console.WriteLine("---REP Files: ---");
foreach (string name in array2)
{
Console.WriteLine(name);
}
}
}
}
Im a beginner, and If statements are my weakness. I have a simple program that displays files names that are located in a certain folder. However, some files might have lines that begin with LIFT. I want to catch those files that have that certain line, and display the file name in a different color (preferably red). Here is what I have so far: Any assistance would be greatly appreciated!! Thanks!
public partial class ShippedOrders : System.Web.UI.Page
{
class Program
{
static void Main()
{
string[] array1 = Directory.GetFiles(@"C:\Kaplan\Replies\");
string[] array2 = Directory.GetFiles(@"C:\Kaplan\Replies\", "*.REP");
Console.WriteLine("---Files:---");
foreach (string name in array1)
{
Console.WriteLine(name);
}
Console.WriteLine("---REP Files: ---");
foreach (string name in array2)
{
Console.WriteLine(name);
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Directory.GetFiles(directoryPath) 将返回一个字符串数组,列出该目录中的文件名(完整路径)。您必须使用返回的字符串数组实际打开并读取每个文件。循环中逐行读取每个文件并测试是否有任何行以“LIFT”开头。
此外,您为此网页设置代码隐藏的方式也很时髦。您正在页面的分部类中声明一个类。尝试像这样设置您的代码:
Directory.GetFiles(directoryPath) will return an array of strings listing the file names (full paths) within that directory. You're going to have to actually open and read each file, using the string array returned. Read each file line by line in a loop and test if any lines begins with "LIFT".
Also the way you set up your code-behind for this webpage is funky. You're declaring a class inside the partial class of the page. Try setting up your code like this:
您可以使用
Console.ForegroundColor
属性更改控制台输出颜色。要知道该文件是否包含您想要的文本,您需要打开它并扫描该文件。
然后执行以下操作:
编辑
我没有注意到您试图在 ASP.NET 服务器页面内写入控制台...在这种情况下,您需要告诉我们您正在创建什么类型的应用程序。 ..它是控制台应用程序、Web应用程序还是网站...这取决于情况。
Console
的使用不适合 Web 应用程序。编辑 2
顺便说一句,您只能在控制台应用程序中使用
控制台
。控制台应用程序是独立的 Windows 应用程序,与 Web 应用程序不同。如果您想创建控制台应用程序,在“新建项目”窗口中,您可以在Windows类别下找到它,然后您可以找到名为控制台应用程序的项目类型。
You can change the console output color by using the
Console.ForegroundColor
property.To know if the file contains the text you want, you need to open it and scan the file.
Then do this:
EDIT
I didn't notice you were trying to write to Console inside an ASP.NET server page... in that case you need to tell us what kind of app you are creating... is it a Console application, a WebApplication or a Website... it depends.
The use of
Console
is not suited for web applications.EDIT 2
By the way, you may use the
Console
only in console applications. A console application is a stand-alone windows application, that is different from a web application.If you ever want to create a console app, in the New Project window, you can find it under Windows category, then you can find a project type called Console Application.
您可以在 foreach 循环中执行以下操作:
if(name.contains("LIFT"))
{
//设为红色。
但
它确实存在一个问题,即它只检查字符串(名称)是否包含字符串 LIFT,而不检查该字符串是否位于文件名的开头。如果要检查 LIFT 是否位于文件名的开头,则必须使用一些 Trim 方法。
You can do like this inside your foreach loop:
if(name.contains("LIFT"))
{
//make red.
}
it does though have the issue that it only checks if the string (name) contains the string LIFT, and not if the string is in the beginning of the filename. If you want to check if LIFT is in the beggining of the file name you must use some of the Trim methods.