Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用ZipArchive类添加文件时,不会调用NuGet-install.ps1_C#_Visual Studio_Zip_Nuget_Nuget Package - Fatal编程技术网

C# 使用ZipArchive类添加文件时,不会调用NuGet-install.ps1

C# 使用ZipArchive类添加文件时,不会调用NuGet-install.ps1,c#,visual-studio,zip,nuget,nuget-package,C#,Visual Studio,Zip,Nuget,Nuget Package,我想通过软件将tools\Install.ps1添加到我的nuget软件包中。这已在使用以下代码: Zipper.AddItem(pathToNuGetFile, pathToToolsFolder); 当我安装nuget软件包时,install.ps1脚本被简单地跳过而不被调用。但它确实存在!它也会在包文件夹中解包 软件包文件夹(在解决方案中) 工具(子文件夹) 当我打开*.nupkg文件(在NuGet Sources文件夹中,而不是在packages文件夹中)并将Install.ps1

我想通过软件将
tools\Install.ps1
添加到我的
nuget
软件包中。这已在使用以下代码:

Zipper.AddItem(pathToNuGetFile, pathToToolsFolder);
当我安装nuget软件包时,
install.ps1
脚本被简单地跳过而不被调用。但它确实存在!它也会在
文件夹中解包

软件包文件夹(在解决方案中)

工具(子文件夹)

当我打开*.nupkg文件(在NuGet Sources文件夹中,而不是在packages文件夹中)并将
Install.ps1
重命名为
Install.ps
,将其重命名回
Install.ps1
并保存“zip”/
*.nupkg
文件时,Install.ps1脚本被调用!!为什么

拉链类
内部类拉链
{
/// 
///正在将项目添加到现有Zip存档
/// 
/// 
/// 
公共静态void AddItem(字符串路径文件、字符串路径项)
{
Program.WriteLine($“正在打开存档文件”{pathZipFile}');
使用(FileStream FileStream=newfilestream(pathZipFile,FileMode.Open))
{
使用(ZipArchive archive=new ZipArchive(fileStream,ZipArchiveMode.Update))
{
Program.WriteLine(“已成功打开”);
if(File.Exists(pathItem))
{
Program.WriteLine($“{pathItem}是一个文件。正在开始将该文件添加到存档..”);
FileInfo FileInfo=新的FileInfo(路径项);
CreateEntryFromFile(fileInfo.FullName,fileInfo.Name);
Program.WriteLine($“{pathItem}已成功添加。”);
}
else if(Directory.Exists(pathItem))
{
Program.WriteLine($“{pathItem}是一个目录。开始递归添加文件..”);
DirectoryInfo DirectoryInfo=新的DirectoryInfo(路径项);
foreach(directoryInfo.AllFilesAndFolders()中的FileInfo文件,其中(o=>o是FileInfo.Cast())
{
Program.WriteLine($“正在添加文件“{file.FullName}”);
string relPath=file.FullName.Substring(directoryInfo.FullName.Length-directoryInfo.Name.Length);
archive.CreateEntryFromFile(file.FullName,relPath);
}
}
}
}
}
}
公共静态类文件扩展
{
公共静态IEnumerable AllFilesAndFolders(此目录信息目录)
{
foreach(dir.GetFiles()中的var f)
收益率f;
foreach(dir.GetDirectories()中的变量d)
{
收益率d;
foreach(所有文件和文件夹中的var o(d))
收益率o;
}
}
}
结果


微软的
ZipArchive
类似乎有问题。当我使用
7zip
添加文件时,一切正常

我已在解决方案中实施了
7zip
,并更改了
AddItem
方法:

/// <summary>
/// is adding an item to an existing Archive
/// </summary>
/// <param name="pathZipFile"></param>
/// <param name="pathItem"></param>
public static void AddItem(string pathZipFile, string pathItem)
{
    string appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip", "7za.exe");
    if (SystemInformation.Is64BitOperatingSystem())
    {
        appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip", "x64", "7za.exe");
    }

    //
    // Setup the process with the ProcessStartInfo class.
    //
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = appPath;
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    start.Arguments = $"a -r {pathZipFile} {pathItem}";

    //
    // Start the process.
    //
    using (Process process = Process.Start(start))
    {
        //
        // Read in all the text from the process with the StreamReader.
        //
        if(process == null)
            throw new Exception("Failed to start the 7zip Process!");

        using (StreamReader reader = process.StandardOutput)
        {
            string result = reader.ReadToEnd();
            Console.Write(result);
        }

        process.WaitForExit();
        if (process.ExitCode != 0)
            throw new Exception($"Fehlercode von 7zip: {process.ExitCode}");
    }
}
//
///正在将项目添加到现有存档中
/// 
/// 
/// 
公共静态void AddItem(字符串路径文件、字符串路径项)
{
字符串appPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“7zip”、“7za.exe”);
if(SystemInformation.is64位操作系统())
{
appPath=Path.Combine(AppDomain.CurrentDomain.BaseDirectory,“7zip”、“x64”、“7za.exe”);
}
//
//使用ProcessStartInfo类设置进程。
//
ProcessStartInfo start=新的ProcessStartInfo();
start.FileName=appPath;
start.UseShellExecute=false;
start.RedirectStandardOutput=true;
Arguments=$“a-r{pathZipFile}{pathItem}”;
//
//开始这个过程。
//
使用(Process进程=Process.Start(Start))
{
//
//使用StreamReader读入流程中的所有文本。
//
if(进程==null)
抛出新异常(“启动7zip进程失败!”);
使用(StreamReader=process.StandardOutput)
{
字符串结果=reader.ReadToEnd();
控制台。写入(结果);
}
process.WaitForExit();
如果(process.ExitCode!=0)
抛出新异常($“fehlercodevon7zip:{process.ExitCode}”);
}
}

希望这对其他人有所帮助。

安装.ps1时,其他程序似乎仍在使用
Install.ps1
脚本。您可以尝试通过手动将
Install.ps1
脚本添加到包中,然后将其安装到项目中来确认这一点。或者通过软件将
tools\Install.ps1
添加到您的nuget软件包中,然后重新启动Visual Studio并安装该软件包。手动不是解决方案。我想自动包含它。重新启动VS也不起作用。nuget由第三方工具操作/更新(并使用上述代码),因此不涉及VS。我知道手动不是解决方案,我建议手动添加
install。ps1
旨在缩小此问题的范围,如果手动添加效果良好,则此问题应与第三方工具操作/更新更相关。您可以简单地使用NuGet Package Explorer手动添加它。
/// <summary>
/// is adding an item to an existing Archive
/// </summary>
/// <param name="pathZipFile"></param>
/// <param name="pathItem"></param>
public static void AddItem(string pathZipFile, string pathItem)
{
    string appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip", "7za.exe");
    if (SystemInformation.Is64BitOperatingSystem())
    {
        appPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "7zip", "x64", "7za.exe");
    }

    //
    // Setup the process with the ProcessStartInfo class.
    //
    ProcessStartInfo start = new ProcessStartInfo();
    start.FileName = appPath;
    start.UseShellExecute = false;
    start.RedirectStandardOutput = true;
    start.Arguments = $"a -r {pathZipFile} {pathItem}";

    //
    // Start the process.
    //
    using (Process process = Process.Start(start))
    {
        //
        // Read in all the text from the process with the StreamReader.
        //
        if(process == null)
            throw new Exception("Failed to start the 7zip Process!");

        using (StreamReader reader = process.StandardOutput)
        {
            string result = reader.ReadToEnd();
            Console.Write(result);
        }

        process.WaitForExit();
        if (process.ExitCode != 0)
            throw new Exception($"Fehlercode von 7zip: {process.ExitCode}");
    }
}