Mohammad Fneish
Mohammad Fneish

Reputation: 989

System couldn't find an existing file after publish

I am running the following c# code in .Net5.0:

public static void Main(string[] args)
{
    Console.WriteLine(File.Exists(@"C:\WINDOWS\system32\dsregcmd.exe"));
    Directory.GetFiles(@"C:\WINDOWS\system32\").ToList().ForEach(f => Console.WriteLine(f));
}

When running it from Visual Studio or from cmd using the dotnet command the first reports true where the second line prints all 4834 files in the target directory including the dsregcmd file path. However, after publishing the project as a self-contained application the first line prints false where the second line is printing 3027 files out of 4838 in the system32 path without the dsregcmd file path.

I checked the file permissions and all users have read access on the file, also tried to run as administrator but still the same. I'm writing c# since 10 years ago and didn't see something like this before.

Here is how I'm publishing the project:

dotnet publish ".\MyProject.csproj" -c Release -r win-x86 --self-contained -o ".\output"

Thanks in advance.

Upvotes: 0

Views: 965

Answers (2)

yaakov
yaakov

Reputation: 5851

Most likely the file cannot be found due to Windows Filesystem Redirection for 32-bit apps. You would have to compile your .NET app as x64 in order to see the real system32 directory.

Upvotes: 1

Leandro Bardelli
Leandro Bardelli

Reputation: 11578

Set property "Copy to output directory" with value "Copy always" on the file if it's included in the project.

Upvotes: 0

Related Questions