访问已发布应用程序中的资源

时间:2011-07-02 08:33:09

标签: c# c#-4.0 resources

我跟着a tutorial on Video Player

这很好,但我想象的是另一种情况。

我需要在资源文件中嵌入一些视频,并根据用户输入动态更改视频。 我使用switch语句在Visual Studio 2010中运行此操作。 但我必须在资源文件中指定该视频的路径。例如:

case 1 : video = new Video("..//..//Resources//The video name");

但是当我使用click once向导发布此应用程序时,最终的应用程序最终会出现异常:

System.NullReferenceException: Object reference not set to an instance of an object.
   at VideoPlayer.Form1.button2_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e).............

似乎视频文件未包含在应用程序中。 但是,当我看到安装程序文件夹时,一个清晰的外观显示有一个名为VideoPlayer.exe.deploy的文件,它通常为59MB,没有任何资源的这些文件是轻量级的......

它肯定包含视频文件。但我无法访问该文件。

我如何访问它?

请帮帮我......

提前致谢: - )

正如一些用户所说,我在C#.NET中发布了整个代码:

Designer.cs文件:

namespace PlayVideo
{
    partial class Form1
    {
        // <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.viewport = new System.Windows.Forms.Panel();
            this.button1 = new System.Windows.Forms.Button();
            this.button2 = new System.Windows.Forms.Button();
            this.button3 = new System.Windows.Forms.Button();
            this.label1 = new System.Windows.Forms.Label();
            this.comboBox1 = new System.Windows.Forms.ComboBox();
            this.SuspendLayout();
            // 
            // viewport
            // 
            this.viewport.Location = new System.Drawing.Point(12, 39);
            this.viewport.Name = "viewport";
            this.viewport.Size = new System.Drawing.Size(391, 368);
            this.viewport.TabIndex = 0;
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(55, 418);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(79, 27);
            this.button1.TabIndex = 0;
            this.button1.Text = "Play";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // button2
            // 
            this.button2.Location = new System.Drawing.Point(167, 418);
            this.button2.Name = "button2";
            this.button2.Size = new System.Drawing.Size(79, 27);
            this.button2.TabIndex = 1;
            this.button2.Text = "Pause";
            this.button2.UseVisualStyleBackColor = true;
            this.button2.Click += new System.EventHandler(this.button2_Click);
            // 
            // button3
            // 
            this.button3.Location = new System.Drawing.Point(279, 418);
            this.button3.Name = "button3";
            this.button3.Size = new System.Drawing.Size(79, 27);
            this.button3.TabIndex = 2;
            this.button3.Text = "Stop";
            this.button3.UseVisualStyleBackColor = true;
            this.button3.Click += new System.EventHandler(this.button3_Click);
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(12, 15);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(108, 13);
            this.label1.TabIndex = 3;
            this.label1.Text = "Load Video number : ";
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "Video number 1",
            "Video number 2",
            "Video number 3"});
            this.comboBox1.Location = new System.Drawing.Point(132, 12);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(121, 21);
            this.comboBox1.TabIndex = 4;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(415, 457);
            this.Controls.Add(this.comboBox1);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.button3);
            this.Controls.Add(this.button2);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.viewport);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Video Player";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Panel viewport;
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.Button button2;
        private System.Windows.Forms.Button button3;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.ComboBox comboBox1;
    }
}

另一个.cs文件是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.DirectX.AudioVideoPlayback;

namespace PlayVideo
{
    public partial class Form1 : Form
    {
        private Video video;
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex)
            {
                case 0: video = new Video("..//..//Resources//Video1.mp4");
                        break;
                case 1: video = new Video("..//..//Resources//Video2.DAT");
                        break;
                case 2: video = new Video("..//..//Resources//Video3.DAT");
                        break;
            }
            int width = viewport.Width;
            int height = viewport.Height;
            // set the panel as the video object’s owner
            video.Owner = viewport;

            // stop the video
            video.Stop();

            // resize the video to the size original size of the panel
            viewport.Size = new Size(width, height);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (video.State != StateFlags.Running)
            {
                video.Play();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (video.State == StateFlags.Running)
            {
                video.Pause();
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (video.State != StateFlags.Stopped)
            {
                video.Stop();
            }
        }
    }
}

我认为截图可能包含一些信息:

enter image description here

它在调试模式下工作正常.....但是一旦我使用点击一次安装程序安装它,它就会崩溃成异常。如何使用DEPLOY文件中的资源管理相同的内容?

1 个答案:

答案 0 :(得分:2)

您正在引用VS解决方案中的文件,这在调试应用程序时工作正常,因为路径在项目的\ bin \ debug文件夹中有效。如果已将资源的构建操作设置为“嵌入式资源”,则可以使用以下命令访问这些文件:

typeof(Form1).Assembly.GetManifestResourceStream("PlayVideo.Resources.Video1.dat");

MSDN:http://msdn.microsoft.com/en-us/library/xc4235zt.aspx

<强>更新 我已经确认Video类不接受Stream,也不能使用res:// - Protocol。我建议您不要将视频嵌入可执行文件中,而是将它们放入Application目录中。您可以将视频文件的构建操作更改为none,并将“Copy to Output Directory”属性更改为“Copy If Newer”。构建应用程序后,所有视频都将放在输出目录中,您可以将其与可执行文件一起部署。然后你应该使用你的初始方法,但使用像“视频名称.dat”而不是“..//..//Resources/The video name.dat”这样的路径。