質問

Visual Studio 2008でプログラムをデバッグしたいと思います。問題は、議論が得られない場合に終了することです。これは主な方法からです。

if (args == null || args.Length != 2 || args[0].ToUpper().Trim() != "RM") 
{
    Console.WriteLine("RM must be executed by the RSM.");
    Console.WriteLine("Press any key to exit program...");
    Console.Read();
    Environment.Exit(-1);
}

私はそれをコメントアウトしたくないので、コンパイルするときに戻ってきます。デバッグ時に引数でプログラムを開始するにはどうすればよいですか?スタートアッププロジェクトとして設定されています。

役に立ちましたか?

解決

行きます Project-><Projectname> Properties. 。次に、をクリックします Debug タブ、そして「テキストボックス」と呼ばれる引数に記入してください Command line arguments.

他のヒント

使用することをお勧めします 指令 以下のように:

        static void Main(string[] args)
        {
#if DEBUG
            args = new[] { "A" };
#endif

            Console.WriteLine(args[0]);
        }

幸運を!

私の提案は、ユニットテストを使用することです。

アプリケーションでは、次のスイッチを実行します Program.cs:

#if DEBUG
    public class Program
#else
    class Program
#endif

そして同じです static Main(string[] args).

または、または使用します 友達のアセンブリ 追加することによって

[assembly: InternalsVisibleTo("TestAssembly")]

あなたに AssemblyInfo.cs.

次に、ユニットテストプロジェクトと、そうするようなテストを作成します。

[TestClass]
public class TestApplication
{
    [TestMethod]
    public void TestMyArgument()
    {
        using (var sw = new StringWriter())
        {
            Console.SetOut(sw); // this makes any Console.Writes etc go to sw

            Program.Main(new[] { "argument" });

            var result = sw.ToString();

            Assert.AreEqual("expected", result);
        }
    }
}

この方法で、自動化された方法で、別のものをチェックするたびに、コードを編集したり、メニュー設定を変更したりせずに、引数の複数の入力をテストできます。

為に ビジュアルスタジオコード:

  • 開ける launch.json ファイル
  • 構成にargsを追加します:

「args」:[「いくつかの議論」、「もう一つ」]、

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top