The code that I have is the one you see below.
I am implementing a sandbox mode in WPF, where user can type plantuml code, and then create diagram, which is then represented in the app.
What happens is that the first time it can generate the image, but on the other tries, it just throws an error saying that the file is being used and I can't access it.
It seems that the image I generate doesn't close, I've tried with FileStream, File.Create and then closing it somehow, killing/closing/dispose the process, waiting for the file to be available, but it always says the same thing.
I know it is not the typical plantuml question, but I thought someone had issue like this before.
Thx for the help
public class SandboxModel
{
private const string plantUMLJarPath = "..\\..\\..\\..\\PULCSI_CL\\plantuml.jar";
private const string outputDirectory = "..\\..\\..\\PULCSI\\Resources\\temp";
private const string outputFilePath = "..\\..\\..\\Resources\\temp\\diagram.txt";
private const string outputImagePath = "..\\..\\..\\Resources\\temp\\diagram.png";
public SandboxModel() { }
public void RefreshDiagram(string sandboxCode)
{
if(sandboxCode.Contains("\r"))
{
sandboxCode = sandboxCode.Substring(sandboxCode.Split("\r")[0].Length);
sandboxCode = "@startuml" + sandboxCode;
}
File.WriteAllText(outputFilePath, sandboxCode);
GeneratePlantUMLImage();
}
private void GeneratePlantUMLImage()
{
Process p = new Process();
p.StartInfo.FileName = "java";
p.StartInfo.Arguments = $"-jar \"{plantUMLJarPath}\" \"{outputFilePath}\" -o \"{outputDirectory}\"";
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.UseShellExecute = false;
try
{
p.Start();
string output = p.StandardOutput.ReadToEnd();
string error = p.StandardError.ReadToEnd();
p.WaitForExit();
if (!string.IsNullOrEmpty(output))
Debug.WriteLine($"PlantUML Output: {output}");
if (!string.IsNullOrEmpty(error))
Debug.WriteLine($"PlantUML Error: {error}");
}
catch (Exception ex)
{
Debug.WriteLine($"Error: {ex.Message}");
}
}
}