C#控制台项目中从Visual Studio Properties加载视频字节数组失败
Hey there! Let's work through getting your video loaded properly. The issue you're facing likely stems from a combination of resource configuration details and the fact that console apps don't have built-in video playback capabilities. Here's a step-by-step breakdown:
1. Double-Check Your Resource Setup
First, make sure your video file is configured correctly in the project:
- Right-click your
videoplayback.mp4file in theResourcesfolder → Select Properties - Set Build Action to
Embedded Resource(if you want the video bundled directly into your executable) orContent - For
Contentsetup, set Copy to Output Directory toCopy if newerto ensure the file gets copied to your build folder - If you added the video via the project's
Resources.resxfile (not just manually placing it in the folder), the resource name will automatically drop the.mp4extension—so you'd reference it asConsoleApplication7.Properties.Resources.videoplayback(no file suffix)
2. Correctly Retrieve the Video Resource
Your current code has a small issue with the resource reference. Try one of these approaches based on your setup:
If Using Resources.resx
using System.IO; using System.Diagnostics; // Get the embedded video as a byte array byte[] videoBytes = ConsoleApplication7.Properties.Resources.videoplayback; // Console apps can't play videos directly—save to a temp file first string tempVideoPath = Path.Combine(Path.GetTempPath(), "temp_video.mp4"); File.WriteAllBytes(tempVideoPath, videoBytes); // Launch the system's default video player to open the temp file Process.Start(new ProcessStartInfo(tempVideoPath) { UseShellExecute = true });
If Manually Adding as Embedded Resource
If you just dropped the file into the Resources folder without using Resources.resx, you'll need to fetch it via the assembly:
using System.Reflection; using System.IO; using System.Diagnostics; Assembly currentAssembly = Assembly.GetExecutingAssembly(); // Resource name follows the pattern: [Namespace].[Folder].[FileName] string resourceFullName = "ConsoleApplication7.Resources.videoplayback.mp4"; using (Stream resourceStream = currentAssembly.GetManifestResourceStream(resourceFullName)) { if (resourceStream == null) { Console.WriteLine("Error: Could not find the video resource!"); return; } byte[] videoBytes = new byte[resourceStream.Length]; resourceStream.Read(videoBytes, 0, videoBytes.Length); // Save and play the video like before string tempVideoPath = Path.Combine(Path.GetTempPath(), "temp_video.mp4"); File.WriteAllBytes(tempVideoPath, videoBytes); Process.Start(new ProcessStartInfo(tempVideoPath) { UseShellExecute = true }); }
3. Verify Resource Names (If You're Stuck)
If you're unsure of the exact resource name, run this code to list all embedded resources in your assembly:
using System.Reflection; Assembly currentAssembly = Assembly.GetExecutingAssembly(); foreach (string resourceName in currentAssembly.GetManifestResourceNames()) { Console.WriteLine(resourceName); }
Match the output to the name you're using in your code—this will catch typos or namespace mismatches.
Key Note
Remember: Console applications don't have built-in UI controls for video playback. That's why we're saving the byte array to a temporary file and launching the system's default player—this is the simplest way to play videos in a console context.
内容的提问来源于stack exchange,提问作者aDoN

