【问题标题】:Visual Studio environmental variable OUTPUT_PATHVisual Studio 环境变量 OUTPUT_PATH
【发布时间】:2020-08-14 13:23:18
【问题描述】:

我在尝试使用 Visual Studio 2019 解决简单问题时遇到此错误

System.ArgumentNullException: '值不能为空。参数名称:路径'

这是导致错误的代码行:

TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

我的环境变量有问题吗?如果是这样,我该如何解决这个问题?

完整代码如下:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{
    /*
     * Complete the simpleArraySum function below.
     */
    static int simpleArraySum(int[] ar)
    {
        /*
         * Write your code here.
         */
        return 0;
    }

    static void Main(string[] args)
    {
        TextWriter textWriter = new StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);

        int arCount = Convert.ToInt32(Console.ReadLine());

        int[] ar = Array.ConvertAll(Console.ReadLine().Split(' '), arTemp => Convert.ToInt32(arTemp));
        int result = simpleArraySum(ar);

        textWriter.WriteLine(result);

        textWriter.Flush();
        textWriter.Close();
    }
}

【问题讨论】:

  • 让你的代码具有防御性。
  • 为什么不只为您的控制台应用程序使用 命令行参数
  • 或者写信给Console.Out,这样你就可以把它传送到一个文件或另一个应用程序。

标签: c#


【解决方案1】:

您不应使用环境变量“OUTPUT_PATH”。它不是每台机器上的标准变量。

尝试写入相对于 exe 的文件夹或“%userprofile%”中的一些其他文件夹,例如文档

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-27
    • 2014-10-02
    • 1970-01-01
    • 2021-09-17
    • 2020-12-02
    相关资源
    最近更新 更多