C
C#10mo ago
brownie

✅ WPF application doesn't boot when packed to single file

Hello, I made a program in WPF, and in the publish settings I enabled "Produce single file", but the output .exe crashes before the window even shows up. There is an error in the event viewer, but I'm not exactly sure what to make of it (https://pastebin.com/85J3ZzDL). Here are my publish settings:
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>D:\Mistakes\VideoCompressor</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net8.0-windows</TargetFramework>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
<?xml version="1.0" encoding="utf-8"?>
<!--
https://go.microsoft.com/fwlink/?LinkID=208121.
-->
<Project>
<PropertyGroup>
<Configuration>Release</Configuration>
<Platform>Any CPU</Platform>
<PublishDir>D:\Mistakes\VideoCompressor</PublishDir>
<PublishProtocol>FileSystem</PublishProtocol>
<_TargetId>Folder</_TargetId>
<TargetFramework>net8.0-windows</TargetFramework>
<RuntimeIdentifier>win-x86</RuntimeIdentifier>
<SelfContained>true</SelfContained>
<PublishSingleFile>true</PublishSingleFile>
<PublishReadyToRun>false</PublishReadyToRun>
</PropertyGroup>
</Project>
17 Replies
Buddy
Buddy10mo ago
at System.IO.Directory.SetCurrentDirectory(String path)
at VideoCompressor.MainWindow..ctor() in C:\Users\ashle\Documents\C# Practise\VideoCompressor\VideoCompressor\VideoCompressor.xaml.cs:line 67
at System.IO.Directory.SetCurrentDirectory(String path)
at VideoCompressor.MainWindow..ctor() in C:\Users\ashle\Documents\C# Practise\VideoCompressor\VideoCompressor\VideoCompressor.xaml.cs:line 67
fix your error at line 67 in VideoCompressor.xaml.cs
brownie
brownieOP10mo ago
No description
brownie
brownieOP10mo ago
string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location; i dont get why this is making it crash thank you or the response, btw
Buddy
Buddy10mo ago
use AppDomain.CurrentDomain.BaseDirectory AppContext.BaseDirectory That might work instead of using GetExecutingAssembly
brownie
brownieOP10mo ago
oh shit its working now, thank you so much i did this:
c#
// Sets current working directory to the .exe path
// string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string workingDir = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(workingDir);
c#
// Sets current working directory to the .exe path
// string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string workingDir = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(workingDir);
brownie
brownieOP10mo ago
christ why is the executeable so fat
No description
Buddy
Buddy10mo ago
Because the runtime is embedded within the application You can decide not to, but that would also mean requiring the user to install the runtime You can turn it off by not using self-contained
brownie
brownieOP10mo ago
yeahh also what i did doesnt seem to have worked the application starts now but theres a problem basically when my program starts it checks to see if some required binaries exist in the working directory, and if they dont, then to download them
Buddy
Buddy10mo ago
Don't use Environment.CurrentDirectory* Use what I mentioned
brownie
brownieOP10mo ago
ah ok
c#
string workingDir = AppDomain.CurrentDomain.BaseDirectory;
c#
string workingDir = AppDomain.CurrentDomain.BaseDirectory;
like this?
Buddy
Buddy10mo ago
Actually, it's better if you use AppContext.BaseDirectory
brownie
brownieOP10mo ago
same issue came up again with this
Buddy
Buddy10mo ago
I meant the other
brownie
brownieOP10mo ago
yeah im trying that now
Buddy
Buddy10mo ago
it should not give you null
brownie
brownieOP10mo ago
same issue again gimme a second
c#
// Sets current working directory to the .exe path
// string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string workingDir = AppContext.BaseDirectory;

// Check if ffmpeg directory exists in working directory
bool ffmpegDirExists = Directory.Exists("ffmpeg");

if (!ffmpegDirExists)
{
Directory.CreateDirectory("ffmpeg");
}

// Check if tmp directory exists
bool tmpDirExists = Directory.Exists("tmp");

if (!tmpDirExists)
{
Directory.CreateDirectory("tmp");
}
c#
// Sets current working directory to the .exe path
// string strExeFilePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string workingDir = AppContext.BaseDirectory;

// Check if ffmpeg directory exists in working directory
bool ffmpegDirExists = Directory.Exists("ffmpeg");

if (!ffmpegDirExists)
{
Directory.CreateDirectory("ffmpeg");
}

// Check if tmp directory exists
bool tmpDirExists = Directory.Exists("tmp");

if (!tmpDirExists)
{
Directory.CreateDirectory("tmp");
}
this is the code for checking if the binaries i need exist does Directory.Exists check in the working directory? hold on i think i had a brainfart its working now its 11:30pm gimme a break 😭 thank you so much for your help
Buddy
Buddy10mo ago
No problem type /close if you want to close this thread
Want results from more Discord servers?
Add your server