在资源目录下打开一个文件

本文关键字:一个 文件 资源 | 更新日期: 2023-09-27 17:54:15

我想在资源目录中打开一个包含的html文件,但似乎我的路径是错误的,或者我正在犯一些其他错误。

我目前在一个表单类,我想打开文件,如果用户按下F1按钮。

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo.FileName = "f1.html";
proc.Start();

在资源目录下打开一个文件

如果你想获得一些嵌入式资源,你需要调用

ResourceManager.GetStream
http://msdn.microsoft.com/en-us/library/zxee5096.aspx

返回一个内存流。将内存流读入字节数组,并将字节数组写入某个临时位置,然后调用

Process.Start()

使用临时文件路径作为参数。

下面是一个示例代码:
public class Class1{
    public static void Main(string[] args){
        FileStream stream = null;
        string fullTempPath = null;
        try{
            byte[] page = Resources.HTMLPage1;
            fullTempPath = Path.GetTempPath() + Guid.NewGuid() + ".html";
            stream = new FileStream(fullTempPath, FileMode.Create, FileAccess.Write, FileShare.Read);
            stream.Write(page, 0, page.Length);
            stream.Flush(true);
            stream.Close();
            Process proc = new Process{StartInfo ={FileName = fullTempPath}};
            proc.Start();
        }
        finally{
            if (stream != null){
                stream.Dispose();
            }
        }
    }
}

使用以下代码使用默认浏览器打开html文件

        string filename = Environment.CurrentDirectory + System.IO.Path.DirectorySeparatorChar + "f1.html";
        System.Diagnostics.Process.Start(filename);