将资源提取到当前目录?

时间:2018-11-13 01:02:12

标签: c# visual-studio

我想将资源“ cygz.dll”解压缩到加载表单后程序立即启动的当前目录。

这是我的代码。

private static void extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName)
    {
        Assembly assembly = Assembly.GetCallingAssembly();

        using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName))

            using (BinaryReader r = new BinaryReader(s))

                using (FileStream fs = new FileStream(outDirectory + "\\" + resourceName, FileMode.OpenOrCreate))

                    using (BinaryWriter w = new BinaryWriter(fs))

                        w.Write(r.ReadBytes((int)s.Length));

    }

private void Form1_Load(object sender, EventArgs e)
    {

        extract("myNamespace", "\\TEMP", "resources", "cygz.dll");
    }

我想将“ cygz.dll”解压缩到当前目录,而不是“ \\ TEMP”

2 个答案:

答案 0 :(得分:0)

如果通过“当前目录”表示启动应用程序的可执行文件的路径,则可以使用Application.ExecutablePath这样:

System.IO.Path.GetDirectoryName(Application.ExecutablePath);

您可以在此处找到更多详细信息:https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.application.executablepath?view=netframework-4.7.2

答案 1 :(得分:0)

这解决了我的问题

    private void Form1_Load(object sender, EventArgs e)
{

    string path = Environment.CurrentDirectory;

    extract("myNamespace", path, "resources", "cygz.dll");
}
相关问题