-1

In the past this worked fine, but not anymore. I used the line of code below to retrieve files from this location:

C:\Users\ZAT\source\repos\KPItoolConsole\KPItoolConsole\input\patterns

string[] files = Directory.GetFiles(@"input\patterns", "*.json", SearchOption.AllDirectories);

But now I get the following error when debugging:

Could not find a part of the path 'C:\Users\ZAT\source\repos\KPItoolConsole\KPItoolConsole\bin\Debug\netcoreapp2.0\input\patterns'

Can someone tell me how I can make it look at the parent folder again?

This is for a Console .NET Core application. I think it stopped working after a Visual Studio update.

croxy
  • 4,082
  • 9
  • 28
  • 46
Zarif
  • 587
  • 1
  • 4
  • 26
  • 2
    It looks at the current working directory. Print the result of `System.IO.Directory.GetCurrentDirectory()`. – ProgrammingLlama Apr 17 '18 at 06:54
  • You might want to copy input\patterns folder to the project output during the build. – Andrii Litvinov Apr 17 '18 at 06:55
  • @john Thanks for your reply. The result of that is: C:\Users\ZAT\source\repos\KPItoolConsole\KPItoolConsole\bin\Debug\netcoreapp2.0 Is there a way to change the working directory? – Zarif Apr 17 '18 at 06:59
  • 2
    There is _no relation_ between your program's working directory and your project folder. In fact, even your working directory and the location of your .exe file can never be assumed to be the same. Put the files you need in a subfolder of _where your exe file is_, and figure out how to access the location of that exe file, rather than using the working directory. – Nyerguds Apr 17 '18 at 07:02
  • Possible duplicate of [C# File Not Found For Custom Font in Visual Studio Designer](https://stackoverflow.com/questions/49103892/c-sharp-file-not-found-for-custom-font-in-visual-studio-designer) – Nyerguds Apr 17 '18 at 07:04
  • Zarif [this](https://stackoverflow.com/questions/1658518/getting-the-absolute-path-of-the-executable-using-c) might help. The `Directory` object has a method for changing the current working directory. – ProgrammingLlama Apr 17 '18 at 07:08
  • @Nyerguds I didn't have an .exe file yet. I was running it from Visual Studio in debug mode. I published the app and now it's working fine. Thanks all. – Zarif Apr 17 '18 at 07:35
  • 1
    You can mark files in your project folder to be copied to the output folder as part of the build process. Just select the file in your project explorer and check the properties; it's called "Copy to output directory", and, if I remember correctly, it retains directory structures. As noted though, unless given as command line args, you should really access local files using the program's location as base; the "current working directory" is completely unreliable. – Nyerguds Apr 17 '18 at 07:37
  • @Nyerguds Oh wow great! Thanks. I'll make sure to use the program's location instead. – Zarif Apr 17 '18 at 07:42

1 Answers1

0

You can solve by this way.These code below was tested from my side.

string src_path = @"C:\Users\ZAT\source\repos\KPItoolConsole\KPItoolConsole\input\patterns";
string dest_path = @"C:\Users\NBY81HC\Desktop\hello";
foreach (string file_path in Directory.GetFiles(src_path,"*.json", SearchOption.AllDirectories))
{
   string fileName = Path.GetFileName(file_path);
   File.Copy(file_path, Path.Combine(dest_path, fileName), true);
}