如何使用嵌入式资源(文本文件)StreamReader
并将其作为字符串返回?我当前的脚本使用Windows窗体和文本框,允许用户查找和替换未嵌入的文本文件中的文本.
private void button1_Click(object sender, EventArgs e)
{
StringCollection strValuesToSearch = new StringCollection();
strValuesToSearch.Add("Apple");
string stringToReplace;
stringToReplace = textBox1.Text;
StreamReader FileReader = new StreamReader(@"C:\MyFile.txt");
string FileContents;
FileContents = FileReader.ReadToEnd();
FileReader.Close();
foreach (string s in strValuesToSearch)
{
if (FileContents.Contains(s))
FileContents = FileContents.Replace(s, stringToReplace);
}
StreamWriter FileWriter = new StreamWriter(@"MyFile.txt");
FileWriter.Write(FileContents);
FileWriter.Close();
}
Run Code Online (Sandbox Code Playgroud) 我来自一个主要是Web和一点点Windows窗体背景.对于一个新项目,我们将使用WPF.WPF应用程序需要10-20个小图标和图像用于说明目的.我正在考虑将它们作为嵌入式资源存储在程序集中.这是正确的方法吗?
如何在XAML中指定Image控件应从嵌入式资源加载图像?
我想从我的jar中读取资源,如下所示:
File file;
file = new File(getClass().getResource("/file.txt").toURI());
BufferredReader reader = new BufferedReader(new FileReader(file));
//Read the file
Run Code Online (Sandbox Code Playgroud)
并且它在Eclipse中运行时工作正常,但是如果我将它导出到jar中运行它就会出现IllegalArgumentException:
Exception in thread "Thread-2"
java.lang.IllegalArgumentException: URI is not hierarchical
Run Code Online (Sandbox Code Playgroud)
而且我真的不知道为什么,但经过一些测试我发现如果我改变了
file = new File(getClass().getResource("/file.txt").toURI());
Run Code Online (Sandbox Code Playgroud)
至
file = new File(getClass().getResource("/folder/file.txt").toURI());
Run Code Online (Sandbox Code Playgroud)
然后它的工作正好相反(它适用于jar而不是eclipse).
我正在使用Eclipse,我的文件夹在一个类文件夹中.
我有两个ASP.NET Web项目(ProjectA和ProjectB).当ProjectA中的类实例化一个使用资源文件Blah.resx的ProjectB时,我收到此错误:
mscorlib.dll中出现"System.Resources.MissingManifestResourceException"类型的异常,但未在用户代码中处理.
找不到适合指定文化或中性文化的资源.确保在编译时将"Resources.Blah.resources"正确嵌入或链接到程序集"App_GlobalResources.sn_flri6",或者所有所需的附属程序集都是可加载和完全签名的.
是什么导致了这个?
微软的网站上有一篇关于这个http://support.microsoft.com/kb/318603的文章, 它建议:
若要解决此问题,请移动所有其他类定义,以便它们出现在窗体的类定义之后.
这是Windows Forms项目的解决方案,我不确定这是否也适用于Web项目.
我想在程序集中嵌入一个文本文件,这样我就可以加载文本而无需从磁盘中读取它,因此我需要的所有东西都包含在exe中.(这样它更便携)
有没有办法做到这一点?我假设资源文件的东西?
如果可以的话,你是如何做到的,以及如何将文本加载到字符串中?
这是一个C#.NET 4.0应用程序:
我将文本文件嵌入为资源,然后尝试在对话框中显示它:
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyProj.Help.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
{
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
System.Windows.Forms.MessageBox.Show(result, "MyProj", MessageBoxButtons.OK);
}
}
Run Code Online (Sandbox Code Playgroud)
解决方案是MyProjSolution,可执行文件是MyProj.exe.Help.txt是一个嵌入式资源.但是,流为空.我已经尝试了MyProjSolution.Help.txt和MyProjSolution.MyProj.Help.txt,但似乎没有任何效果.
我什么时候应该使用其中一种?
我想在我的应用程序中使用的所有文件(图像,声音,xml文件等)都在.exe文件中,所以我不会使用一堆文件夹和文件进行部署.
谢谢(你的)信息.
我正在关注Justin Slattery的插件架构教程,并尝试将其用于Razor,而不是WebForm Views.
其他一切(控制器,插件组装加载等)似乎没问题.但是,我无法使嵌入式Razor视图正常工作.当我尝试浏览"HelloWorld/Index"时,出现以下错误:
The view at '~/Plugins/MyProjectPlugin.dll/MyProjectPlugin.Views.HelloWorld.Index.cshtml' must derive from WebViewPage or WebViewPage<TModel>.
Run Code Online (Sandbox Code Playgroud)
抛出异常 System.Web.Mvc.RazorView.RenderView(ViewContext viewContext, TextWriter writer, Object instance) +262
如果需要,我可以包括完整的堆栈跟踪.
任何人都可以建议我可能做错了什么?
我正在使用reStructuredText,我想通过<embed>
标签添加HTML编码交互式flash类型动画.从我的.rst文档中,如何指定这个任意HTML块的位置?就像是:
.. html ::
<embed>
... more html here ...
</embed>
Run Code Online (Sandbox Code Playgroud)
这个功能是否存在?谢谢.