public class Shifang
{
public Shifang()
{
if (!Directory.Exists(System.Environment.CurrentDirectory + "\\pkconfig\\"))
{
Directory.CreateDirectory(System.Environment.CurrentDirectory + "\\pkconfig\\");
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (string item in assembly.GetManifestResourceNames())
{
if (item.Contains("resources") || item.Contains("costura"))//过滤掉其他资源
{
continue;
}
using (FileStream fs = new FileStream(System.Environment.CurrentDirectory + "\\pkconfig\\" + item.Replace(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + ".pkconfig.", ""), FileMode.OpenOrCreate, FileAccess.ReadWrite))
{
try
{
byte[] by = new byte[assembly.GetManifestResourceStream(item).Length];
assembly.GetManifestResourceStream(item).Read(by, 0, by.Length);
fs.Write(by, 0, by.Length);
fs.Flush();
fs.Close();
}
catch (Exception)
{
MessageBox.Show("释放资源错误");
}
}
}
}
}
}
嵌入资源1 可参考
https://blog.csdn.net/u013542549/article/details/52038274
private void sfresources()
{
if (!File.Exists(System.Environment.CurrentDirectory + "\\System.Data.SQLite.dll"))
{
FileStream fs1 = new FileStream(System.Environment.CurrentDirectory + "\\System.Data.SQLite.dll", FileMode.Create, FileAccess.ReadWrite);
try
{
fs1.Write(Resources.System_Data_SQLite, 0, Resources.System_Data_SQLite.Length);
fs1.Flush();
fs1.Close();
}
catch (Exception ex)
{
throw ex;
}
}
}
}
嵌入资源2
private Image _glassDownImg = GetImageFormResourceStream("ControlExs.QQControls.QQGlassButton.Image.glassbtn_down.png");
public static Image GetImageFormResourceStream(string imagePath)
{
return Image.FromStream(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + imagePath));
}
例子
private void getresources()
{
String projectName = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name.ToString();
Stream stmFont = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(projectName + ".Resources" + ".楷体_GB2312.ttf");
//“楷体_GB2312.ttf”的属性必须以 “嵌入的资源”作为资源文件。设置只需要在对应属性框设置就是了
}
可参考
https://www.cnblogs.com/lonelyDog/archive/2012/02/16/2354407.html
https://blog.csdn.net/eit520/article/details/51025470
//动态遍历循环一
ResourceManager resourceManager = new ResourceManager(typeof(Resources));
ResourceSet resourceSet = resourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentUICulture, true, true);
foreach (System.Collections.DictionaryEntry item in resourceSet)
{
if (item.Value.GetType().Name.Equals("Bitmap"))
{
string name = item.Key.ToString();
object obj = item.Value;
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
this.pictureBox1.Image = (Bitmap)obj;
}
}
//动态遍历循环二
//string imagepath = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace + "." + "ImageFile";
Assembly assembly = Assembly.GetExecutingAssembly();
foreach (string item in assembly.GetManifestResourceNames())
{
if (item.Substring(item.LastIndexOf(".") + 1).Equals("jpg"))
{
Image obj = Image.FromStream(assembly.GetManifestResourceStream(item));
this.pictureBox1.Image = (Bitmap)obj;
}
}
//动态遍历循环三 类似方法二
Assembly assembly = Assembly.Load(new System.Reflection.AssemblyName("内嵌资源动态释放"));
foreach (var item in assembly.GetManifestResourceNames())
{
//"内嵌资源动态释放.ImageFile.1.jpg"
if (item.Substring(item.LastIndexOf(".") + 1).Equals("jpg"))
{
Image obj = Image.FromStream(assembly.GetManifestResourceStream(item));
this.pictureBox1.Image = (Bitmap)obj;
}
}
public static string GetCityID(string city)
{
string retstr = string.Empty;
//string name = Assembly.GetExecutingAssembly().GetName().Name;
//string namespc = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType.Namespace;
Assembly asm = Assembly.GetExecutingAssembly();
//var cdm = asm.GetManifestResourceNames();
foreach (string c in asm.GetManifestResourceNames() )
{
if (c.Contains("CityDataMobile.json"))
{
//var stream1 = asm.GetManifestResourceStream(MethodBase.GetCurrentMethod().DeclaringType.Namespace+ ".CityDataMobile.json");
var stream2 = asm.GetManifestResourceStream(c);
using (StreamReader sr = new StreamReader(stream2))
{
retstr = sr.ReadToEnd();
}
}
}
//JObject.Parse
return retstr;
}