Exemplo n.º 1
0
        public void Initialise()
        {
            m_truePath    = PlatformHelper.ConvertPath(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath));
            m_getFullPath = str => PlatformHelper.ConvertPath(Path.GetFullPath(Path.Combine(m_truePath, str)));

            m_tempPath = PlatformHelper.ConvertPath(m_truePath + @"\..\tmp");
            if (!Directory.Exists(m_tempPath))
            {
                Directory.CreateDirectory(m_tempPath);
            }
        }
        /// <summary>
        ///   Exports the specified embedded resource from the given assembly to the path specified.
        ///   Within the assembly the fully qualified name of the resource is expected to be resourcePath + "." + resourceName.
        ///   Note: If the embedded resource is in a subdirectory, the subdirectory name can not consist of only numbers. For e.g.
        ///   if your resource is in a subdirectory names 1234, you must rename it to something like test1234 such that the
        ///   directory
        ///   name does not contain only numbers. C# in its infinite wisdom does not like directories with names containing only
        ///   numbers
        /// </summary>
        /// <param name="source">The assembly containing the resource</param>
        /// <param name="resPath">The fully qualified (ie namespace + subdirectory) resource path (excluding the resourceName)</param>
        /// <param name="resName">The resource name</param>
        /// <param name="outputPath">The path to save to. If you want to rename the resource specify a file name in this path.</param>
        /// <returns>The path to the saved file</returns>
        public static string ExportToPath(Assembly source, string resPath, string resName, string outputPath)
        {
            string fullResourceName = string.Format("{0}.{1}", resPath, resName);

            // Get manifest resource stream
            Stream resourceStream = source.GetManifestResourceStream(fullResourceName);

            if (resourceStream == null)
            {
                // This means that the resource was not found, so now throw an exception
                string message = string.Format("Unable to find {0} in assembly {1}", fullResourceName, source.Location);
                throw new Exception(message);
            }

            if (!Directory.Exists(outputPath))
            {
                Directory.CreateDirectory(outputPath);
            }

            string filePath = string.IsNullOrWhiteSpace(Path.GetExtension(outputPath)) ? outputPath + Path.DirectorySeparatorChar + resName : outputPath;

            filePath = Path.GetFullPath(PlatformHelper.ConvertPath(filePath));

            BinaryReader reader = new BinaryReader(resourceStream);
            BinaryWriter writer = new BinaryWriter(new FileStream(filePath, FileMode.Create));

            byte[] buffer = reader.ReadBytes(1024);
            while (buffer.Length > 0)
            {
                writer.Write(buffer);
                buffer = reader.ReadBytes(1024);
            }

            writer.Close();
            reader.Close();

            return(filePath);
        }