2017-01-26 9 views

答えて

1

まず、ファイルをプロジェクトのリソースとして追加する必要があります。

This explains what to do

は、その後、あなたのファイルを選択して、プロパティで「ビルドアクション」に「埋め込まれたリソース」に変更します。これで、出力ファイル(.exe)にファイルが埋め込まれます。

ファイルを抽出するには、次の操作を行う必要があります。

String myProject = "Name of your project"; 
String file = "Name of your file to extract"; 
String outputPath = @"c:\path\to\your\output"; 

using (System.IO.Stream stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(myProject + ".Resources." + file)) 
{ 
    using (System.IO.FileStream fileStream = new System.IO.FileStream(outputPath + "\\" + file, System.IO.FileMode.Create)) 
    { 
     for (int i = 0; i < stream.Length; i++) 
     { 
      fileStream.WriteByte((byte)stream.ReadByte()); 
     } 
     fileStream.Close(); 
    } 
} 

これを行う前に、ファイルが存在していないことを確認するのが理想的です。例外をキャッチすることも忘れないでください。これは、ファイルシステムを扱うときに非常に一般的になります。

3

リソース名が文字列の場合:

var assembly = Assembly.GetExecutingAssembly(); 
using (var stream = assembly.GetManifestResourceStream(resourceName)) 
using (var reader = new StreamReader(stream)) 
{ 
    string text = reader.ReadToEnd(); 
    File.WriteAllText(fileName, text); 
} 

File.WriteAllText(fileName, Properties.Resources.TextFile1); 

そしてまた、あなたが「埋め込まれたリソース」にリソースファイルのビルドアクションを設定していることを確認してください。

0
  1. Properties -> Resources -> Add Resource 
    
  2. File.WriteAllText(@"C:\test\testOut.txt", text); 
    
でファイルに

var text = Properties.Resources.textFile; 
  • 書き込みを使用してリソースからデータを読み込み、プロジェクトのリソースにテキストファイルを追加します。

  • 関連する問題