0

いくつかの方法を試しましたが、保存しても開きません。どうすればこれを達成できますか?

基本的に、現在リソースファイルであるMP4ファイルを、パスとしてアクセスできる一時的な場所に保存できるようにしたいです。

これが私が試したものです:

    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();

                }
            }
        }}
4

3 に答える 3

1

あなたはそれを複雑にしすぎています。

このようなことを試してください(コンパイルもテストもされていません。Stream.CopyTo()は.NET 4.0以降にのみ存在します)。

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

仕事は終わりました。

.NET 4.0を利用できない場合は、次のいずれかのように、自分で実装する必要があります。あるストリームのコンテンツを別のストリームにコピーするにはどうすればよいですか。

現在のアセンブリ内のすべてのリソース名のリストを取得するには、次のようにします。

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

コンソールに表示されたものを取得し、投稿した最初のスニペットのGetManifestResourceStream()に渡します。

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

于 2012-06-14T21:21:01.290 に答える
0

なぜMP4を文字列として書いているのですか?変更せずにバイトを書き出す必要があります。charsへの変換は、データを変更しています。FileStream呼び出しを使用して、Writeメソッドを呼び出します。

于 2012-06-14T21:10:37.347 に答える
-1

あなたはこのようなことを試すことができます:

間違ったコードを貼り付けました...ごめんなさい、急いでいました

[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();
    }

}

これは実際にはビデオファイルをディレクトリにロードしますが、あなたのフォーマットでも機能するはずです。

-ありがとう、

于 2012-06-14T21:12:44.120 に答える