一、問題描述
想將外部檔案改為內部檔案的方式讓C#使用,避免在包裝成 dll 時遺漏檔案;
亦可讓使用者無法直接看到檔案的內容。
二、方法
1。新增檔案:
點選方案總管之專案名稱,
右鍵--> 加入--> 新增項目
一般--> 文字檔
取好文字檔的名稱後,直接點選此文字檔,並編輯其內容。
2。設定檔案為內嵌資源以供使用
點選檔案,並在其下方的檔案屬性視窗做設定。
將原先建置動作為『內容』改為『內嵌資源』。
程式:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Assembly _assembly;
StreamReader _textStreamReader;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
_assembly = Assembly.GetExecutingAssembly();
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("WindowsFormsApplication1.TextFile_1.txt"));
}
catch
{
MessageBox.Show("Error accessing resources!");
}
try
{
if (_textStreamReader.Peek() != -1)
{
string file_content = _textStreamReader.ReadLine();
}
}
catch
{
MessageBox.Show("Error writing text!");
}
}
}
}
留言列表