在代码 C# 中将参数传递给变量

本文关键字:参数传递 变量 代码 | 更新日期: 2023-09-27 17:57:01

我正在尝试使用这个程序,但我希望能够在以下位置传递一个参数:

DeleteOnReboot(@"C:'test.txt");

"C:''Text" 是

所以我可以调用控制台应用程序.exe/C:''test2.exe

所以我会在代码中有一个变量,例如

DeleteOnReboot(@"%VARIABLE%");

完整代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
    class Program
    {
[DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
public static extern bool MoveFileEx(string
lpExistingFileName, string lpNewFileName, int dwFlags);
public const int MOVEFILE_DELAY_UNTIL_REBOOT = 0x4;
public static void DeleteOnReboot(string filename)
{
if (!MoveFileEx(filename, null,
MOVEFILE_DELAY_UNTIL_REBOOT))
    Console.WriteLine("Failed");
}
static void Main(string[] args)
{
DeleteOnReboot(@"C:'test.txt");
Console.ReadKey();
}
    }
}

在代码 C# 中将参数传递给变量

只需使用程序入口点中的args数组(Main

例:

DeleteOnReboot(args[0]);

你需要从 string[] args 中提取文件路径、名称。

DeleteOnReboot(args[0]);

或者类似的东西,这样称呼它:consoleapp.exe C:''test2.exe

您是否在 main 中检查了 args 变量的内容? 这是参数传递到的位置,以及如何在控制台应用中访问它们。