File copied to Debug folder is automatically deleted when the C# program launches
Asked Answered
K

3

7

I am using Visual Studio 2012 to build a WPF program. There is a DLL compiled from C that I use DllImport to call. The Dll is added in the project and marked as

  • Build Action: Content
  • Copy to Output Directory: Copy always

When the project solution is built the dll is copied to Debug folder with the .exe generated by Visual Studio successfully. However, when i run it, either by double click the generated .exe from File Explorer or "Run" in the VS debugger, the dll suddenly gets deleted by itself.

Why?

Kilohertz answered 11/1, 2016 at 7:57 Comment(1)
I find that not only DLL but even a simple abc.txt added as Content will also be removed automatically upon program launches.Kilohertz
L
1

If it does this when running from File Explorer, then either

  • Your anti-virus program is deleting the dll when you try to access it

or

  • There must be code in your program to delete files in the current directory.
Lustre answered 19/3, 2016 at 16:48 Comment(0)
H
1

After many tries I've found some pattern which can be also a workaround. Assume we have 3 projects, A, B and C.

  1. A depends on B; B depends on C - problem exists
  2. A depends on B and C; B depends on C - problem disappears

Maybe it'd help someone to track the reasons.

Hemialgia answered 16/1, 2018 at 14:59 Comment(0)
A
0

I have encountered this, and successfully fixed.

Two things you need to check:

  1. In the project App.xaml, you need to make sure

    • For Application x:Class="..." set to YourWpfProject.App
    • For StartupUri="..." set to the folder your View(MainWindow.xaml) belongs to.
  2. In the beginning of your View(MainWindow.xaml) file, you need to make sure

    • For Windows x:class="..." set to exact namespace your xaml file(View) belongs to. Remember to check clearly if you put your views or viewmodels into a folder under this project.

Now, try your "F5".

Astir answered 4/5, 2018 at 12:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.