C# 访问已发布应用程序中的资源
我跟着 这很好,但我正在想象一个不同的情况 我需要在资源文件中嵌入一些视频,并根据用户输入动态更改视频。 我已经在VisualStudio2010中管理了这个。使用switch语句。 但我必须在资源文件中指定该视频的路径。例如:C# 访问已发布应用程序中的资源,c#,c#-4.0,resources,C#,C# 4.0,Resources,我跟着 这很好,但我正在想象一个不同的情况 我需要在资源文件中嵌入一些视频,并根据用户输入动态更改视频。 我已经在VisualStudio2010中管理了这个。使用switch语句。 但我必须在资源文件中指定该视频的路径。例如: case 1 : video = new Video("..//..//Resources//The video name"); 但是,当我使用“单击一次”向导发布此应用程序时,最终的应用程序会出现异常: System.NullReferenceException:
case 1 : video = new Video("..//..//Resources//The video name");
但是,当我使用“单击一次”向导发布此应用程序时,最终的应用程序会出现异常:
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;
}
}
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();
}
}
}
}
我想屏幕截图可能是一些信息:
它在调试模式下工作正常。。。。。但是,一旦我使用click once安装程序安装它,它就会出现异常。如何使用部署文件中的资源管理这些文件?调试应用程序时,您正在引用VS解决方案中的文件,这很好,因为路径在项目的\bin\debug文件夹中是有效的。如果已将资源的生成操作设置为“嵌入式资源”,则可以使用以下方式访问这些文件:
typeof(Form1).Assembly.GetManifestResourceStream("PlayVideo.Resources.Video1.dat");
MSDN:
更新
我已经确认视频类不会接受流,并且res://-Protocol 也不能使用。我建议您不要将视频嵌入到可执行文件中,而是将它们放在应用程序目录中。您可以将视频文件的构建操作更改为“无”,并将“复制到输出目录”属性更改为“如果更新,则复制”。构建应用程序后,所有视频都会放在输出目录中,您可以将其与可执行文件一起部署。然后,您应该使用初始方法,但使用类似“video name.dat”的路径,而不是“../../../Resources/video name.dat”。您是否只是混淆了斜杠和反斜杠?不。。。实际上,当我在VisualStudio下“调试”解决方案时,我发布的代码运行得非常好。注意:实际上,我的Resources文件夹比debug文件夹高两级。因此使用斜杠。但是当我使用Click Once应用程序安装应用程序时,它给了我我提到的一个例外。我认为这是因为一旦我们在Windows机器上安装应用程序,工作程序集就不同了。此外,安装的文件系统也不同。有人能帮我吗?????????请