trakker/Utilities/FileExtensions.cs

58 lines
1.7 KiB
C#

using System.Diagnostics;
namespace newcle.us.Utilities
{
public class FileExtensions
{
public static string CreateTempFile(string content)
{
// Best practice: random name + .txt extension for gvim
string tempPath = Path.Combine(Path.GetTempPath(),
Path.GetRandomFileName() + ".txt");
File.WriteAllText(tempPath, content);
return tempPath;
}
public static bool EditFile(string filePath, string editor)
{
try
{
var startInfo = new ProcessStartInfo
{
FileName = editor,
Arguments = $"\"{filePath}\"", // Quote the path
UseShellExecute = true,
WindowStyle = ProcessWindowStyle.Normal
};
using var process = Process.Start(startInfo);
if (process == null)
return false;
process.WaitForExit(); // ← This blocks until gvim is closed
return process.ExitCode == 0; // Usually 0 on normal exit
}
catch (Exception ex)
{
MessageBox.Show($"Failed to launch gvim:\n{ex.Message}", "Error");
return false;
}
}
public static void SafeDeleteTempFile(string path)
{
try
{
if (File.Exists(path))
File.Delete(path);
}
catch (Exception ex)
{
// Log but don't crash
Debug.WriteLine($"Failed to delete temp file: {ex.Message}");
}
}
}
}