-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

Matyas
  • 1,122
  • 5
  • 23
  • 29
user
  • 9
  • 4

2 Answers2

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;

Matyas
  • 1,122
  • 5
  • 23
  • 29
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

Nicolas C
  • 225
  • 2
  • 8
  • I puted this in class Program because I want to be a global variabile, but when I run this I have 12 errors – user Sep 14 '15 at 05:17
  • and I want to have only the directory name, without executable name ? – user Sep 14 '15 at 05:57
  • I edit my post for the directory path you need.I hope it's help you – Nicolas C Sep 14 '15 at 07: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 Sep 14 '15 at 07:54
  • what are these errors? – Nicolas C Sep 14 '15 at 08:01
  • you need to escape \ by another \ like this `Program.Path + "\\test.txt");` – Nicolas C Sep 14 '15 at 08:02
  • I tried with \\ but I have the next 3 errors :" The property or indexer 'WindowsFormApp2.Program.Path' cannot be used in this context because the get accessor is inaccessible" – user Sep 14 '15 at 08:07
  • another : "an object reference is required for the non-static field, methode, or property 'windFormApp2.Program.Path.get'" and 3."'windFormApp2.Program.Path' is inaccessible due to its protected level" – user Sep 14 '15 at 08:10
  • Is the string Path is declare in the same class as your function? If not you must declare it as public. the more simple is to declare it in the same class as your function. – Nicolas C Sep 14 '15 at 08:18
  • and if I put the private string Path in Form1, I don't have errors bur the link variable remain null – user Sep 14 '15 at 08:19
  • also you can add the string directly in the function `File.ReadLines(AppDomain.CurrentDomain.BaseDirectory.ToString())` ... – Nicolas C Sep 14 '15 at 08:19
  • I put this before var link=..... but the same thing link remain null – user Sep 14 '15 at 08:24
  • are you in winForm? So it's should work with this `Application.Current.BaseDirectory`, see this post [link](http://stackoverflow.com/questions/295687/get-path-to-execution-directory-of-windows-forms-application) – Nicolas C Sep 14 '15 at 08:25
  • Hmm!? Something must be wrong somewhere in your code. May be give us more detailed code, the main class with the string declaration and the function – Nicolas C Sep 14 '15 at 08:41