0

我用它来加载一个文件(html_file.html)Resources

//string myFile = "C:\\Users\\...\\Resources\\html_file.html";      // this works
var myFile = Path.GetFullPath("html_file.html");            // this doesn't works

//myFile = myFile.ToString();
//myFile = myFile.Replace(@"\", @"\\");
//MessageBox.Show(myFile);  

try
{
    Process.Start(myFile);
}
catch (Win32Exception noBrowser)
{
    if (noBrowser.ErrorCode == -2147467259)
        MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
    MessageBox.Show(other.Message);
}

有人可以告诉我有什么问题吗?

编辑:这有效

Build Action = Embedded ResourceCopy to Output Directory = Copy always

string myFile = @".\Resources\html_file.html";

但我仍然需要Resources文件的路径。有什么办法可以在我的.EXE文件中包含“html_file”?

4

2 回答 2

4

很明显,它在当前目录中找不到文件。确保以下内容正确:

  • 该文件包含在您的项目中,并且其Copy to Output Directory属性设置为Copy alwaysCopy if newer

  • 用于Application.StartupPath确保您指向正确的目录,因此第一行将变为:

代码:

var myFile = Path.Combine(Application.StartupPath, "html_file.html"); 
于 2013-09-02T14:50:16.417 回答
1

在第一种方法中,您指定文件的确切路径。
在第二个中,您要求框架创建一个完整路径。框架需要从某个地方开始,它选择从您的当前目录开始,但文件不存在那里

于 2013-09-02T14:51:45.457 回答