-5

I created a Form Application and I tried to get the executable path, and I find this :

System.Reflection.Assembly.GetExecutingAssembly().Location;
System.IO.Path.GetDirectoryName;

but when I puted in my code I had a lot of errors .

This is my code :

namespace inst
{
    public class Program
    {
        System.Reflection.Assembly.GetExecutingAssembly().Location;
        System.IO.Path.GetDirectoryName;

        [STAThread]
        static void Main()
        {
        }
}

It is right where I placed it? And I want to use that location to find a text file, to be able to change, like here:

private void Form1_Load(object sender, EventArgs e)
{
    this.Text = File.ReadLines(Program.Path)
          .First(x => x.StartsWith("Title=\""))
          .Split(new[] { '=', '"' }, StringSplitOptions.RemoveEmptyEntries)[1];
}

The Path is the location of the file text.

So I want to get de location of executable where is a file test.txt, put the location in a variabile and use that variabile in form1 and form2, in my case

0

2 Answers 2

0

Just put the line System.Reflection.Assembly.GetExecutingAssembly().Location; in the Form1_Load method. and use what it returned. By the way if the file you are trying to access is in the same directory as the assembly there is no need to obtain the path. The paths are relative the executing assembly path.

Also in WinForm application it is better to use Application.StartupPath to get the path.

It is not allowed to have a code outside of a method, except of declarations, by the way like yours is:

System.Reflection.Assembly.GetExecutingAssembly().Location;
System.IO.Path.GetDirectoryName;

And also the compiler wouldn't like that it is not a statement at all. you should do something like var value = Something; and not just Something;

0

Simply declare a String

private string Path
    {
        get { return Assembly.GetExecutingAssembly().Location.ToString();}
    }

then use it where you want

Edit: for the directory path it's this:

private string Path
{
    get {return AppDomain.CurrentDomain.BaseDirectory.ToString();}
}

your code will be

namespace inst
{
    public class Program
    {
        private string Path
        {
            get {return AppDomain.CurrentDomain.BaseDirectory.ToString();}
        }

        [STAThread]
        static void Main()
        {
        }
}

and you function (who is in the same class Program)

private void Form1_Load(object sender, EventArgs e)
{
    this.Text = File.ReadLines(Path)//here the string with the directory path
          .First(x => x.StartsWith("Title=\""))
          .Split(new[] { '=', '"' }, StringSplitOptions.RemoveEmptyEntries)[1];
}

if you want the variable Path accessible in all other class you can add a static class and add the string declaration as public and you could use it everywhere like this yourStaticClassName.Path

14
  • I puted this in class Program because I want to be a global variabile, but when I run this I have 12 errors
    – user
    Commented Sep 14, 2015 at 5:17
  • and I want to have only the directory name, without executable name ?
    – user
    Commented Sep 14, 2015 at 5:57
  • I edit my post for the directory path you need.I hope it's help you
    – Nicolas C
    Commented Sep 14, 2015 at 7:20
  • and to attach the file name I have the next row: var link = File.ReadLines(Program.Path + "\test.txt"); but when I run I have 3 errors
    – user
    Commented Sep 14, 2015 at 7:54
  • what are these errors?
    – Nicolas C
    Commented Sep 14, 2015 at 8:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.