Jay
Jay

Reputation: 189

Passing a parameter to a variable in code C#

I am trying to use this program, but I would like to be able to pass a parameter where:

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

"C:\Text" is

So I could call consoleapp.exe /C:\test2.exe

So I would have a variable in code e.g.

DeleteOnReboot(@"%VARIABLE%");

Full Code:

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();
}
    }
}

Upvotes: 1

Views: 107

Answers (3)

M3NTA7
M3NTA7

Reputation: 1347

You need to pull the file path, name from string[] args.

DeleteOnReboot(args[0]);

Or something similar and call it like this: consoleapp.exe C:\test2.exe

Upvotes: 0

kstev
kstev

Reputation: 740

just use the args array that is in the entry point of your program (Main)

Example:

DeleteOnReboot(args[0]);

Upvotes: 2

BugFinder
BugFinder

Reputation: 17868

Have you checked the contents of your args variable in main? this is where the parameters are passed to and how you can access them in your console app.

Upvotes: 0

Related Questions