1

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

3 Answers 3

2

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

Example:

DeleteOnReboot(args[0]);

Sign up to request clarification or add additional context in comments.

2 Comments

He already did, just replace DeleteOnReboot(@"C:\test.txt"); with DeleteOnReboot(args[0]);
Well, I guess you'd need to change consoleapp.exe /C:\test2.exe to consoleapp.exe C:\test2.exe
0

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

Comments

0

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.

1 Comment

You dont "set" it, the args are already available, check the declaration of main, they are there for you to use.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.