1

I try get my project diretory with GetCurrentDirectory() method, but returns AppData\Local\temp diretory.

I tried use AppContext.BaseDirectory, this worked but property returns C:\\projectName\\projectName\\bin\\Debug\\, I want only C:\\projectName\\projectName\\

I also tried

string currentPath = findCurrentPath.Replace("\\projectName\\bin\\Debug\\", "");
archivePath = $@"{currentPath}\Image_Senarios_projectName\{imageName}";

It works in my computer, but this project run in multiple computers and on one of the computers this method returns

"C:\Jenkins\workspace_crm_aut\5.3.59\CrmAutomacao\Web\Automacao\projectName\projectName\bin\Release\\"

So I tried it

string findCurrentPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;

but it's returned C:\Users\nameUser\AppData

wczanin
  • 11
  • 3
  • Possible duplicate of [How do you get the current project directory from C# code when creating a custom MSBuild task?](https://stackoverflow.com/questions/816566/how-do-you-get-the-current-project-directory-from-c-sharp-code-when-creating-a-c) – GSerg Oct 14 '19 at 20:24
  • Check : https://stackoverflow.com/questions/1174903/convert-vb-to-c-sharp-my-application-info-directorypath –  Oct 14 '19 at 20:27
  • 3
    Your project, the source code, is meaningless when running an application. It's just a directory as any other else. There is no function like "get the project directory" since normally, the application resides not on the same computer as the source code. The Exe-File is in bin\debug, and this is what you can find out. – Holger Oct 14 '19 at 20:38
  • 1
    As Holger pointed out, the location of your source code and the build output should be irrelevant to your executable and the way it functions. You're tightly coupling a development/build detail to the runtime execution of your app. I'd suggest you look at another way to provide the app at runtime with the path you're interested in. Assuming this is a console app, you could pass it in as an arg and grab it as `args[0]` for instance. If you're code is running in the context of a web app, there's framework APIs you can use to retrieve the base directory of the website. – benmccallum Oct 14 '19 at 20:48
  • I could understand why on my computer returns the temp directory and on another computer the project directory using the same code. – wczanin Oct 15 '19 at 18:57

1 Answers1

2

It was the same for me - I was running some tests from Nunit and provided static method for test case source. In that case it was the path where Nunit Console was located (or some of its files).

Command that worked for me:

NUnit.Framework.Internal.AssemblyHelper.GetAssemblyPath(typeof(PieceTests).Assembly);

You can also you look up: How do I get the path of the assembly the code is in?

Mr Patience
  • 1,564
  • 16
  • 30