file not found, wrong path in C #

0

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 ;

    
asked by Sergio Ramos 03.03.2017 в 03:22
source

1 answer

1

Basically it's because in the path you put the user Androide and in your browser image says Android , it should look like this:

filePath = "C:\Users\Android\Desktop\prueba.exe";

Update

Because of your comment my explanation would be the following: it sends you that message not because the file does not exist, but rather because valid that when calling console.exe carries arguments when invoking it, and since you do not put any, that's why goes to the statement else :

When you put console.exe into the command console, you do not put an argument to it, and the code works like this:

if (args.Length > 0)
{
    //tu código
}
else
{
    Console.Write("File not found!");
}

I do not understand exactly so that you can validate the existence of arguments, but try to call console.exe in the following way:

console.exe 1

In this case, it will already take some argument and it will go directly to if instead of else

    
answered by 03.03.2017 / 03:26
source