简体   繁体   English

用C#打开Word文档

[英]open word doc with c#

Im using some code I found to open word docs from a webpage using c#. 我使用一些代码,发现使用c#从网页中打开word文档。 I have added the docs to the project as existing items, im just wondering the besy way to refer to them in the code, 我已将文档作为现有项目添加到项目中,我只是想知道在代码中引用它们的简便方法,

eg 例如

        object filename = "f:\\Online_signup1\\DONE-Signup_Step1.dot";

or just object filename = "DONE-Signup_Step1.dot"; 或者只是对象文件名=“ DONE-Signup_Step1.dot”; thanks again 再次感谢

I'd put them all in a "Resources" folder or similar inside your project, add a Project Settings file ("app.config") which has the name of that folder, and then refer to them using that - 我将它们全部放在项目中的“资源”文件夹或类似文件夹中,添加一个具有该文件夹名称的项目设置文件(“ app.config”),然后使用该文件夹引用它们-
In your app.config file: 在您的app.config文件中:

<setting name="DocumentDirectory" serializeAs="String">
     <value>F:\Online_signup1\Resources</value>
</setting>

Somewhere during program startup: 程序启动期间的某个位置:

string docDir = Settings.Default.DocumentDirectory;

Everywhere else: 其他地方:

string filename = Path.Combine(docDir, myfile)

That way, if you ever distribute your program / move anything around, you won't be stuck going through your code and changing filenames everywhere, just change the directory in the settings file and you're golden. 这样,如果您分发程序/四处移动,就不会卡住代码并在各处更改文件名,只需更改设置文件中的目录就可以了。

For web projects, you can put it in the web.config file, like so: 对于Web项目,可以将其放在web.config文件中,如下所示:

<appSettings>
    <add key="DocumentDirectory" value="F:\Online_signup1\Resources"/>
</appSettings>

And access via: 并通过以下方式访问:

string docDir = ConfigurationManager.AppSettings["DocumentDirectory"];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM