{"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":"2026-10-28T16:41:26","modified_gmt":"2026-10-28T16:41:26","slug":"c-getting-the-directory-of-a-running-executable","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/other\/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.\u00a0 The answer says to use System.Reflection.Assembly.GetExecutingAssembly which will work if you call the method directly from your assembly.\u00a0 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.\u00a0 Anyway, it appears that the correct answer is to use System.Reflection.Assembly.GetEntryAssembly, because it will always return your executable.\u00a0 However, it returns it as a URI.\u00a0 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 \/>\n{<br \/>\nvar location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);<br \/>\nreturn new FileInfo(location.AbsolutePath).Directory;<br \/>\n}<\/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.\u00a0 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 \/>\n{<br \/>\nvar location = new Uri(Assembly.GetEntryAssembly().GetName().CodeBase);<br \/>\nreturn new FileInfo(location.AbsolutePath).Directory.FullName;<br \/>\n}<\/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. For more on this, see <a href=\"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/creating-tray-applications-in-net-a-practical-guide\/\">building a system tray application in .NET.<\/a> For more on this, see <a href=\"https:\/\/www.red-gate.com\/simple-talk\/development\/dotnet-development\/using-linq-lambda-expressions-to-design-customizable-generic-components\/\">customisable file-processing components with lambdas.<\/a><\/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.\u00a0 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":[7575],"class_list":["post-7709","post","type-post","status-publish","format-standard","hentry","category-other"],"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":5,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7709\/revisions"}],"predecessor-version":[{"id":114234,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/7709\/revisions\/114234"}],"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}]}}