文件可以';找不到吗?C#-获取资源
您好,我想访问Visual Studio中的资源中的文件,但当我生成.exe文件并将其从文件夹中移动时,会收到一条消息,说明找不到这些文件 这是我的代码:文件可以';找不到吗?C#-获取资源,c#,visual-studio,file,path,resources,C#,Visual Studio,File,Path,Resources,您好,我想访问Visual Studio中的资源中的文件,但当我生成.exe文件并将其从文件夹中移动时,会收到一条消息,说明找不到这些文件 这是我的代码: string RunningPath = AppDomain.CurrentDomain.BaseDirectory; string pdf = string.Format("{0}Resources\\me.pdf", Path.GetFullPath(Path.Combine(RunningPath, @".
string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
string pdf = string.Format("{0}Resources\\me.pdf", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\")));
Process.Start(pdf);
我尝试了其他方法,但仍然不起作用:/
有人有主意吗?您可以这样做,从资源流读取文件
var stream = Assembly.GetExecutingAssembly()
.GetManifestResourceStream("me.pdf");
using (FileStream fs = File.Create(@"c:\temp\me.pdf"))
{
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = stream.Read(buffer, 0, buffer.Length)) > 0)
{
fs.Write(buffer, 0, len);
}
};
Process.Start(@"c:\temp\me.pdf");
如果已将pdf文件添加到
Properties.Resources
,则可以按如下方式访问它
调用AppDomain.CurrentDomain.BaseDirectory
首先获取bin\Debug
文件夹
然后使用。
返回父目录
string path = AppDomain.CurrentDomain.BaseDirectory + @"..\..\Resources\test.pdf";
System.Diagnostics.Process.Start(path);
您正在尝试访问作为项目内部结构一部分的
Resources
文件夹。就你的应用而言,这个文件夹是不存在的(也因为它在你部署的时候并不存在)。如果您已将二进制文件添加到项目的资源中,那么它已被嵌入。如何访问该嵌入文件取决于您构建的应用程序以及将该文件添加到的资源对象(默认项目的资源,一个自定义的.resx
文件,直接(以二进制形式)添加到.Resource
文件等)。如何访问pdf文件?例如,如果该文件位于资源内部,您可以将其作为流读取,然后写入文件,然后发送到处理。打印您的运行路径
,检查文件是否存在。PDF作为资源嵌入EXE中,它在文件系统中不可用。