When passing a relative path to File.Delete()
, it tries to delete a file relative to the current working directory as returned by Directory.GetCurrentDirectory()
. However, if my application is run from a batch file, it seems that the current directory refers to the location of the batch file instead of my application. How do I get it to refer to my application's directory consistently, whether it's run from a batch file or directly from the application's exe?
Asked
Active
Viewed 76 times
1

Kyle Delaney
- 11,616
- 6
- 39
- 66
-
1What about [System.Reflection.Assembly.GetExecutingAssembly().Location](https://msdn.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx)? – George Alexandria Jun 16 '17 at 21:47
-
2Don't use a relative path... :) – Rufus L Jun 16 '17 at 21:50
-
1@GeorgeAlexandria I belive that `System.Reflection.Assembly.GetExecutingAssembly().Location` returns where the executing assembly is *currently* located, which may or may not be where the assembly is located when not executing. In the case of shadow copying assemblies, you will get a path in a temp directory. `System.Reflection.Assembly.GetExecutingAssembly().CodeBase` will return the 'permenant' path of the assembly – Rufus L Jun 16 '17 at 21:54
-
1@RufusL, yes, you are right https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application#comment9462946_837501 – George Alexandria Jun 16 '17 at 21:58
-
@GeorgeAlexandria :) – Rufus L Jun 16 '17 at 22:14
-
If I don't use relative paths then how do I make an application that can still run if its folder is moved? – Kyle Delaney Jun 17 '17 at 15:41
-
Okay, https://stackoverflow.com/questions/837488/how-can-i-get-the-applications-path-in-a-net-console-application recommends `System.Reflection.Assembly.GetExecutingAssembly().CodeBase` saying `AppDomain.CurrentDomain.BaseDirectory` can be changed at runtime. But https://stackoverflow.com/questions/6041332/best-way-to-get-application-folder-path recommends `BaseDirectory`, saying `CodeBase` is not guaranteed to be set for assemblies in the GAC. – Kyle Delaney Jun 17 '17 at 18:49