I am trying to load a file, to which I indicate the path of the desktop but I do not know why it always returns:
File not found!
The code is as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
namespace ConsoleLauncher
{
class Program
{
static void Main(string[] args)
{
string filePath;
filePath = "C:\Users\Androide\Desktop\prueba.exe";
if (args.Length > 0)
{
//string filePath = args[0];
if (File.Exists(filePath))
{
// read the bytes from the application exe file
FileStream fs = new FileStream(filePath, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length));
fs.Close();
br.Close();
// load the bytes into Assembly
Assembly a = Assembly.Load(bin);
// search for the Entry Point
MethodInfo method = a.EntryPoint;
if (method != null)
{
// create an istance of the Startup form Main method
object o = a.CreateInstance(method.Name);
// invoke the application starting point
method.Invoke(o, null);
}
}
}
else
{
Console.Write("File not found!");
}
}
}
}
Basically I assign the route to him in the following way:
string filePath;
filePath = "C:\Users\Androide\Desktop\prueba.exe";
The file is located on the desktop:
The error:
In this example, indicating args.Length > 0
had to include some argument. You can be solved by putting args.Length = 0
;