如何在C#中打开帮助文件的代码 [英] code for how to open a help file in c#

查看:84
本文介绍了如何在C#中打开帮助文件的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个应用程序,但我不知道用于打开存储在我的c#项目目录中的pdf文件的代码,请有人告诉我该怎么做,我正在尝试这种方式

System.Diagnostics.Process.Start("C:\\ info.pdf");

没关系,但是我将文件放入项目目录,但我不知道如何编写pdf文件的目录.

I''m making an application but I don''t know the code for opening a pdf file that is stored on my c# project directory, please can somebody tell me how to do it, I was trying on this way

System.Diagnostics.Process.Start("C:\\info.pdf");

and its ok but whet I put the file into my project directory I don''t know how to write the directory of my pdf file .

推荐答案

好吧,"C :\\ info.pdf"确实很糟糕.您绝对不应在此目录中放置任何内容.更重要的是,使用源代码中硬编码的任何路径名,不仅不好,而且永远没用.命名的路径应总是在运行时计算或从可配置的数据文件(如配置文件)中加载.

在项目中有两种使用只读文件的方式.首先,您可以将它们放在输出目录中.第二种方法是在嵌入可执行文件的资源中取出文件.

如果使用第一种方法,则文件将与可执行文件部署在同一目录中,或者在某个子目录中进行更深入的部署(一个或多个级别).因此,问题归结为:如何计算一段时间内的可执行目录,以及该可执行目录的本质是什么?

好吧,这就是这样:应用程序的入口程序集的主要可执行模块的位置.是的,确切地说,是的.这是最通用的计算方法:

Well, "C:\\info.pdf" is really bad. You should never put anything in this directory. More importantly, using any path name hard-coded in the source code, not just bad, it can never be useful. The path named should always be calculated during run time or loaded from configurable data files like configuration file.

There are two ways of using read-only files in the projects. First, you can put them in output directory. Second method is to out a file in the resource embedded in the executable file.

If you are using the first method, your file is deployed in the same directory as the executable files, or in some sub-directory, one or more levels deeper. So, the problem is reduced to this one: how to calculate the executable directory of during time, and what is, essentially, that executable directory?

Well, this is such a thing: a location of the main executable module of the entry assembly of the application. Yes, exactly, to be precise. Here is the most universal method to calculate it:

string exeDirectory = System.IO.Path.GetDirectoryName(
    System.Reflection.Assembly.GetEntryAssembly().Location);



还有其他方法,但是它们要么需要某些应用程序类型的库(例如System.Windows.Forms),要么更糟糕的是,结果取决于应用程序的托管方式(特别是,以Windows Service的身份运行代码可以得出不同的结果)或代码在哪里调用方法.我的解决方法没有这些问题.

—SA



There are other methods, but they either require certain library of application type (such as System.Windows.Forms), or worse, the result depends on how the application is hosted (in particular, running the code as Windows Service can give different result) or where the code calls the method. My way of doing it is free from these problems.

—SA


我假设您可以选择启动任何应用程序,并显示.pdf文件.

.NET环境"类提供了一组丰富的工具,可在运行时动态地定位正在运行的.NET应用程序中文件的位置,或在计算机上定位其他特殊文件夹". />
看一下其中的一些代码示例:设置断点,并检查结果:
I''m assuming it''s a choice on your part to launch whatever application is going display the .pdf file.

The .NET "Environment" class provides a rich set of tools to locate, dynamically, at run-time, the location of files in your .NET application that is running, or to locate other "Special Folders" on the computer.

Take a look at some of these code examples: set breakpoints, and examine the results:
// will require this reference for the final example shown here
using System.Collections;

// will get Application Data folder in Documents and Settings
string CurrentAppDataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

// will get path of your currently executing .NET application
string CurrentAppPath = Environment.CurrentDirectory;

// gets the Windows System Folder path
string CurrentWinDirPath = Environment.GetEnvironmentVariable("windir");

// list all of the Environment Variables
foreach (DictionaryEntry theEVar in Environment.GetEnvironmentVariables())
{
    Console.WriteLine(theEVar.Key + " : " + theEVar.Value);
}


这篇关于如何在C#中打开帮助文件的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆