admin管理员组

文章数量:1122832


I would like to create a deb package programmatically inside a c# method.
I did a method to create a zip package using 7z.
You just have to install 7z and setup a ENV variable.
private string GeneratePackageZip(string tempDirectory, string directoryPrefix, int presetId)
{
    try
    {
        _logger.LogInformation("Creating zip archive");

        string packageSavePath = Path.Combine(_storagePath, PackageDirectory, $"{directoryPrefix}{presetId}", _packageFileName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        // adds all files and subfolders from folder subdir to archive archive.zip.
        // 7z a -t7z Files.7z *.txt -r
        var process = new Process
        {
            StartInfo =
            {
                FileName = "7z",
                Arguments = $"a -t7z \"{packageSavePath}\" \"{tempDirectory}/*\"",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };
        process.Start();
        process.WaitForExit();

        if (Directory.Exists(tempDirectory))
        {
            Directory.Delete(tempDirectory, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate 7zip archive");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

And I wanted to do the same with dpkg-deb to create a deb package.
But dpkg-deb cannot be install on windows.

private string GeneratePackageDeb(string tempDirectory, string directoryPrefix, int id)
{
    try
    {
        _logger.LogInformation("Creating deb package");

        string packageSavePath = Path.Combine(_storagePath, PackageDirectory, $"{directoryPrefix}{id}", _packageFileName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        var process = new Process
        {
            StartInfo =
            {
                FileName = "dpkg-deb",
                Arguments = $"-b \"{packageSavePath}\" \"{tempDirectory}/*\"",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        process.Start();
        process.WaitForExit();

        if (Directory.Exists(tempDirectory))
        {
            Directory.Delete(tempDirectory, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate Deb");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

How can I do that ? Is there a way to do it or is it a dead end ?
I thought of to do it.
By creating a .csproj that would contains the binary I need.
But I need a very specific debian-binary. Will I be able to control what I have inside of it?

Thanks for any help given.


I would like to create a deb package programmatically inside a c# method.
I did a method to create a zip package using 7z.
You just have to install 7z and setup a ENV variable.
private string GeneratePackageZip(string tempDirectory, string directoryPrefix, int presetId)
{
    try
    {
        _logger.LogInformation("Creating zip archive");

        string packageSavePath = Path.Combine(_storagePath, PackageDirectory, $"{directoryPrefix}{presetId}", _packageFileName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        // adds all files and subfolders from folder subdir to archive archive.zip.
        // 7z a -t7z Files.7z *.txt -r
        var process = new Process
        {
            StartInfo =
            {
                FileName = "7z",
                Arguments = $"a -t7z \"{packageSavePath}\" \"{tempDirectory}/*\"",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };
        process.Start();
        process.WaitForExit();

        if (Directory.Exists(tempDirectory))
        {
            Directory.Delete(tempDirectory, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate 7zip archive");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

And I wanted to do the same with dpkg-deb to create a deb package.
But dpkg-deb cannot be install on windows.

private string GeneratePackageDeb(string tempDirectory, string directoryPrefix, int id)
{
    try
    {
        _logger.LogInformation("Creating deb package");

        string packageSavePath = Path.Combine(_storagePath, PackageDirectory, $"{directoryPrefix}{id}", _packageFileName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        var process = new Process
        {
            StartInfo =
            {
                FileName = "dpkg-deb",
                Arguments = $"-b \"{packageSavePath}\" \"{tempDirectory}/*\"",
                UseShellExecute = false,
                CreateNoWindow = true
            }
        };

        process.Start();
        process.WaitForExit();

        if (Directory.Exists(tempDirectory))
        {
            Directory.Delete(tempDirectory, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate Deb");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

How can I do that ? Is there a way to do it or is it a dead end ?
I thought of https://github.com/quamotion/dotnet-packaging to do it.
By creating a .csproj that would contains the binary I need.
But I need a very specific debian-binary. Will I be able to control what I have inside of it?

Thanks for any help given.

Share Improve this question edited Nov 21, 2024 at 17:10 Arnaud VDR asked Nov 21, 2024 at 17:08 Arnaud VDRArnaud VDR 133 bronze badges 2
  • Arnaud. I am unfamiliar with dpkg-deb, but if the path to the executable is not in the Environment Path, then you need to specify the full path to the executable in the file name. Try using @"{path to executable}\dpkg-deb"; if this works consider adding the path to your environment path. – Jim Commented Nov 21, 2024 at 18:06
  • Hello @Jim , dpkg-deb is a linux lib to make debian package. I cannot use it on windows inside c# code (from my understanding) – Arnaud VDR Commented Nov 22, 2024 at 13:30
Add a comment  | 

1 Answer 1

Reset to default 1

My solution was to use wsl.

private string GeneratePackageDeb(string tempDirectory, string packageDebName)
{
    try
    {
        _logger.LogInformation("Creating deb package");

        string packageSavePath = Path.Combine(tempDirectory, packageDebName + ".deb");
        string directoryDebPath = Path.Combine(tempDirectory, packageDebName);

        if (File.Exists(packageSavePath))
        {
            File.Delete(packageSavePath);
        }

        // Execute wsl command:
        using (var proc = new Process
        {
            StartInfo = new ProcessStartInfo
            {
                FileName = @"cmd.exe",
                UseShellExecute = false,
                RedirectStandardOutput = true,
                RedirectStandardInput = true,
                CreateNoWindow = true,
            }
        })
        {
            proc.Start();
            // --nocheck because " dpkg-deb: error: control directory has bad permissions 777 (must be >=0755 and <=0775) "
            proc.StandardInput.WriteLine("wsl " + "--cd " + tempDirectory + " dpkg-deb -b --nocheck " + packageDebName);
            System.Threading.Thread.Sleep(500); // give some time for command to execute
            proc.StandardInput.Flush();
            proc.StandardInput.Close();
            proc.WaitForExit(5000); // wait up to 5 seconds for command to execute
            Console.WriteLine(proc.StandardOutput.ReadToEnd());
        }

        if (Directory.Exists(directoryDebPath))
        {
            Directory.Delete(directoryDebPath, true);
        }

        return packageSavePath;
    }
    catch (Exception ex)
    {
        _logger.LogError(ex, "Failed to generate Deb");
        Directory.Delete(_packageWorkingDirectory, true);
        throw;
    }
}

本文标签: cHow to create a deb package with Process NETStack Overflow