C
C#2w ago
ROBERT311

magic code???

i am running my project in visual studio code exactly the same way with nothing changed, the project has an UI but the important part that sometimes works sometimes not is the fact that sometimes it outputs in the csv files then i delete them and readd them and then it wont work again, i have tried dotnet clean dotnet build restarting machine but i can't find a pattern why sometiems it works sometimes it doesent
8 Replies
Adise
Adise2w ago
Hello Could you provide more context? What exactly is failing, is it periodic (Works only on first build or works then does not then works then does not...)? What's the error u get?
Mayor McCheese
It sounds like what you have going on is you have some csv's in your project that aren't being copied to the output directory so it's not failing to load files that aren't there? This is a wild ass guess, like @Adise said there's not a lot here to go on.
ROBERT311
ROBERT311OP2w ago
ive deleted and i am trying to figure it out again i will post updates if i can figure something out the issue was that it was literally random i couldnt find any type or kind of pattern ok so there are csv files in my project and the output needs to be stored inside them through this method
public void SaveResultsToCsv(string unitName)
{

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

string relativeDirectory = Path.Combine(baseDirectory, @"..\..\..\Assets\ProductionUnitResults");

// Resolve the absolute path
string resolvedDirectory = Path.GetFullPath(relativeDirectory);

string filePath = unitName switch
{
"GB1" => Path.Combine(resolvedDirectory, "GB1_Results.csv"),
"GB2" => Path.Combine(resolvedDirectory, "GB2_Results.csv"),
"OB1" => Path.Combine(resolvedDirectory, "OB1_Results.csv"),
_ => throw new Exception($"Unknown unit name: {unitName}")
};

var resultsForUnit = optimizationResults.Where(r => r.UnitName == unitName).ToList();

// Writing to CSV
if (File.Exists(filePath))
{
using (var writer = new StreamWriter(filePath, append: true))
{

foreach (var result in resultsForUnit)
{
writer.WriteLine($"{result.UnitName},{result.HeatProduced},{result.Cost},{result.FuelConsumed}");
}
}


}
else
{
Console.WriteLine($"File does not exist: {filePath}. File cannot be saved.");
}
}
}
}
public void SaveResultsToCsv(string unitName)
{

string baseDirectory = AppDomain.CurrentDomain.BaseDirectory;

string relativeDirectory = Path.Combine(baseDirectory, @"..\..\..\Assets\ProductionUnitResults");

// Resolve the absolute path
string resolvedDirectory = Path.GetFullPath(relativeDirectory);

string filePath = unitName switch
{
"GB1" => Path.Combine(resolvedDirectory, "GB1_Results.csv"),
"GB2" => Path.Combine(resolvedDirectory, "GB2_Results.csv"),
"OB1" => Path.Combine(resolvedDirectory, "OB1_Results.csv"),
_ => throw new Exception($"Unknown unit name: {unitName}")
};

var resultsForUnit = optimizationResults.Where(r => r.UnitName == unitName).ToList();

// Writing to CSV
if (File.Exists(filePath))
{
using (var writer = new StreamWriter(filePath, append: true))
{

foreach (var result in resultsForUnit)
{
writer.WriteLine($"{result.UnitName},{result.HeatProduced},{result.Cost},{result.FuelConsumed}");
}
}


}
else
{
Console.WriteLine($"File does not exist: {filePath}. File cannot be saved.");
}
}
}
}
sometimes it works sometimes it doesen't i cannot find a pattern i have tried closing opening, dotnet clean dotnet build , restarting pc , recloning the project i have no idea project runs through avalonia framework if thats relevant i can share the starting poin i am running the project through vs code so it shouldnt have any permission issue to the csv files
Mayor McCheese
You're going to have trouble referencing files like that, generally navigating up .. is a bad idea. It implies a directory structure must exist in a place that isn't under your control. One thing to do is copy the files to the output directory and reference them from the same directory you're executing from Alternatively you can store them in a dedicated well known and configurable location.
Mayor McCheese
Build actions for files - Visual Studio (Windows)
Learn how all files in a Visual Studio project have a build action and the build action controls what happens to the file when the project is compiled.
Mayor McCheese
There's more advanced strategies, like creating files when they don't exist.
ROBERT311
ROBERT311OP2w ago
i know but when i reference normally it doesent work since we ve added another unit test project in the same folder while i agree with you i still dont understand why it sometimes wont properly output to the csv files and sometimes it will
Mayor McCheese
I can't help you understand the dithering component, if it's truly non deterministic then you've got other problems, otherwise I suspect you don't understand the circumstances. It's likely a set of actions that you're taking creating a state where it works the next execution. Without seeing the code it's hard to tell. Is this in a git repo public somewhere?

Did you find this page helpful?