【问题标题】:How to save output redirect to a file?如何将输出重定向保存到文件?
【发布时间】:2018-05-02 17:18:08
【问题描述】:

C# 新手,希望在下面实现从input.txt 读取并将结果写入output.txt

myprogram.exe < input.txt > output.txt

我的代码如下

class Program
    {
        static void Main(string[] args)
        {
            String line = Console.ReadLine();
            while(line != null)
            {
                Console.Out.WriteLine(line);
                line = Console.ReadLine();
            }
        }
    }

结果是从input.txt读取没问题,可以创建output.txt但是里面什么都没有。

更新:根本原因是 output.txt 被与 C# 语言无关的其他进程锁定。

【问题讨论】:

  • 您没有读取文件,也没有重定向标准输入或输出,所以这真的是您要显示的代码吗?
  • @Crowcoder 将它们读出,控制台命令行重定向文件作为输入流和输出流。阅读有效,但写作无效。
  • 你能说明你是如何重定向输出的吗?
  • @Crowcoder 通过使用“myprogram.exe output.txt”运行我的应用程序
  • 我现在明白了。这应该有效,它对我有用。您是否收到任何控制台错误?

标签: c# .net io


【解决方案1】:
using System;
using System.IO;

namespace ConsoleApplication1
{
    public class Program
    {
        static void Main(string[] args)
        {
            var parameters = GetParameters(args);

            var inputContent = GetInputContent(parameters);

            CreateOutputFile(parameters, inputContent);

            Console.WriteLine("it's ok. Press key to exit");
            Console.ReadKey();
        }

        private static void CreateOutputFile(MyProgramParameters parameters, string inputContent)
        {
            File.WriteAllText(parameters.OutputFileName, inputContent);
        }

        private static string GetInputContent(MyProgramParameters parameters)
        {
            var result = File.ReadAllText(parameters.InputFileName);

            return result;
        }

        private static MyProgramParameters GetParameters(string[] args)
        {
            CheckInputParameters(args);

            return new MyProgramParameters
            {
                InputFileName = args[0],
                OutputFileName = args[1]
            };
        }

        private static void CheckInputParameters(string[] args)
        {
            if (args.Length != 2)
                throw new ArgumentException("invalid parameters. For example, myProgram.exe input1.txt   output.txt");

            var inputFileName = args[0];

            if (!File.Exists(inputFileName))
                throw new IOException(string.Format("File {0} not found", inputFileName));
        }
    }

    public class MyProgramParameters
    {
        public string InputFileName { get; set; }

        public string OutputFileName { get; set; }
    }
}

【讨论】:

  • OP 正在使用命令行重定向运算符,而不是尝试从参数加载文件路径。
【解决方案2】:

您根本没有阅读文档。这里有链接:click

您应该分两步完成:

  • 从文件读取到string[]
  • 从string[] 写入文件

阅读使用

string[] lines = System.IO.File.ReadAllLines(@"C:\Users\Public\TestFolder\WriteLines2.txt");

写用

System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);

整个函数看起来像这样:

private void CopyFromFileToFile(string inputFileDest, string outputFileDest)
{
    string[] lines;
    lines = System.IO.File.ReadAllLines(inputFileDest);
    System.IO.File.WriteAllLines(outputFileDest, lines);
}

【讨论】:

  • OP 没有询问如何读写文件,OP 正在尝试使用标准输入/输出来做到这一点。
猜你喜欢
  • 2019-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-29
  • 2012-06-26
  • 2017-04-04
  • 2012-01-11
  • 2011-11-01
相关资源
最近更新 更多