{"id":7709,"date":"2015-01-14T12:59:53","date_gmt":"2015-01-14T12:59:53","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/c-getting-the-directory-of-a-running-executable\/"},"modified":"2016-07-28T10:56:10","modified_gmt":"2016-07-28T10:56:10","slug":"c-getting-the-directory-of-a-running-executable","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/c-getting-the-directory-of-a-running-executable\/","title":{"rendered":"C# &#8211; Getting the Directory of a Running Executable"},"content":{"rendered":"<p>I was trying to remember how to get the directory of a running executable and I ran across this stack overflow entry on the subject:<\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/1658518\/getting-the-absolute-path-of-the-executable-using-c\">http:\/\/stackoverflow.com\/questions\/1658518\/getting-the-absolute-path-of-the-executable-using-c<\/a><\/p>\n<p>The answer the question is really embedded in the comments to the answer.&#160; The answer says to use System.Reflection.Assembly.GetExecutingAssembly which will work if you call the method directly from your assembly.&#160; But if you call that from another assembly and it&#8217;s not in your executable directory then you&#8217;ll get the wrong location.&#160; Anyway, it appears that the correct answer is to use System.Reflection.Assembly.GetEntryAssembly, because it will always return your executable.&#160; However, it returns it as a URI.&#160; So here is a quick method that you can use to return the directory of the current executable that takes the URI into account:<\/p>\n<p>public static DirectoryInfo GetExecutingDirectory()   <br \/>{    <br \/>&#160;&#160;&#160; var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);    <br \/>&#160;&#160;&#160; return new FileInfo(location.AbsolutePath).Directory;    <br \/>}<\/p>\n<p>This method returns a <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.directoryinfo%28v=vs.110%29.aspx\">DirectoryInfo<\/a> object which contains just about any information you could want on the executable directory.&#160; If you just want the directory as a string, you can get that from the FullName property on the DirectoryInfo object, or you can modify the function to look like this:<\/p>\n<p>public static string GetExecutingDirectoryName()   <br \/>{    <br \/>&#160;&#160;&#160; var location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);    <br \/>&#160;&#160;&#160; return new FileInfo(location.AbsolutePath).Directory.FullName;    <br \/>}<\/p>\n<p>Also note that the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.filesysteminfo.fullname(v=vs.110).aspx\">FullName<\/a> property returns the directory without a trailing slash.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I was trying to remember how to get the directory of a running executable and I ran across this stack overflow entry on the subject: http:\/\/stackoverflow.com\/questions\/1658518\/getting-the-absolute-path-of-the-executable-using-c The answer the question is really embedded in the comments to the answer.&#160; The answer says to use System.Reflection.Assembly.GetExecutingAssembly which will work if you call the method directly from&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-7709","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7709","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=7709"}],"version-history":[{"count":3,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7709\/revisions"}],"predecessor-version":[{"id":42458,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7709\/revisions\/42458"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=7709"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=7709"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=7709"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=7709"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}