0

I've tried a few different ways but it won't open when it's saved. How can I accomplish this?

Basically I want to be able to save an MP4 file that's currently a resource file to a temp location that I can access as a path.

Here's something I've tried:

    public static void WriteResourceToFile(string resourceName, string fileName)
    {

        using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
        {

            if (s != null)
            {

                byte[] buffer = new byte[s.Length];

                char[] sb = new char[s.Length];

                s.Read(buffer, 0, (int)(s.Length));

                /* convert the byte into ASCII text */

                for (int i = 0; i <= buffer.Length - 1; i++)
                {

                    sb[i] = (char)buffer[i];

                }

                using (StreamWriter sw = new StreamWriter(fileName))
                {

                    sw.Write(sb);

                    sw.Flush();

                }
            }
        }}
Bill Dekel
  • 47
  • 3
  • 10

3 Answers3

1

You're overcomplicating it.

Try something like this (note, not compiled or tested, and Stream.CopyTo() only exists in .NET 4.0 and later).

using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)))
using (FileStream fs = File.Open("c:\myfile.mp4", FileMode.Create))
{
    s.CopyTo(fs);
}

Job done.

If you don't have .NET 4.0 available, you'll need to implement one yourself, like one of these: How do I copy the contents of one stream to another?

To get a list of all of the resource names in the current assembly, do something like this:

Assembly a = Assembly.GetExecutingAssembly();
foreach (string s in a.GetManifestResourceNames())
{
    Console.WriteLine(s);
}
Console.ReadKey();

Take what turns up on the console and pass it into GetManifestResourceStream() in the first snippet I posted.

http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcenames.aspx

Community
  • 1
  • 1
tomfanning
  • 9,552
  • 4
  • 50
  • 78
  • Can you give me an example of a resource name? I think that's where I'm slipping up. – Bill Dekel Jun 14 '12 at 21:27
  • Try passing that in, it might be your MP4 file, depending on what you called it. Failing that, have you clicked your MP4 file and selected "Embedded Resource"? Take a look here to learn about how to use embedded resources in VS2010: http://fczaja.blogspot.co.uk/2010/11/visual-studio-adding-and-using-resource.html - this way you don't need to muck about with streams at all. – tomfanning Jun 14 '12 at 21:42
  • Thank you so much. Turns out I was referencing a PDF with a similar but slightly different name, because I had thought that I had imported the mp4 first (it was named [name]1 instead of [name]), so it was writing a pdf to an mp4. When I looked into it per your info, I figured it out and fixed it. Problem solved. Thanks. – Bill Dekel Jun 14 '12 at 21:50
  • No worries. Not a clue why someone has drive-by downvoted my answer though. – tomfanning Jun 15 '12 at 07:48
0

Why are you writing an MP4 as a string? You should write out bytes without modification. Your conversion to chars is modifying the data. Use The FileStream call and call the Write method.

usr
  • 168,620
  • 35
  • 240
  • 369
  • That makes more sense. Could you go over briefly how to use the FileStream method in context to the code? – Bill Dekel Jun 14 '12 at 21:15
  • Open a FileStream using File.Create and use the Stream.CopyTo method to copy the resource to the FileStream. – usr Jun 14 '12 at 21:31
-1

you could try something like this:

I pasted the wrong code in.... sorry, i was in a hurry

[HttpPost]
public ActionResult Create(VideoSermons video, HttpPostedFileBase videoFile)
{
    var videoDb = new VideoSermonDb();
    try
    {
        video.Path = Path.GetFileName(videoFile.FileName);
        video.UserId = HttpContext.User.Identity.Name;
        videoDb.Create(video);


        if (videoFile != null && videoFile.ContentLength > 0)
        {
            var videoName = Path.GetFileName(videoFile.FileName);
            var videoPath = Path.Combine(Server.MapPath("~/Videos/"),
                                         System.IO.Path.GetFileName(videoFile.FileName));
            videoFile.SaveAs(videoPath);

        }

        return RedirectToAction("Index");

    }
    catch
    {
        return View();
    }

}

this actually loads video files to a directory, but it should work for your format as well.

-Thanks,

haydnD
  • 2,225
  • 5
  • 27
  • 61