用c#打开word文档 [英] open word doc with c#

查看:181
本文介绍了用c#打开word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用我发现的一些代码从使用 c# 的网页打开 Word 文档.我已将文档作为现有项目添加到项目中,我只是想知道在代码中引用它们的最佳方式,

Im using some code I found to open word docs from a webpage using c#. I have added the docs to the project as existing items, im just wondering the besy way to refer to them in the code,

例如

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

或者只是对象文件名 = "DONE-Signup_Step1.dot";再次感谢

or just object filename = "DONE-Signup_Step1.dot"; thanks again

推荐答案

我会将它们全部放在项目内的Resources"文件夹或类似文件夹中,添加一个项目设置文件(app.config"),其中包含该文件夹的名称,然后使用该名称引用它们 -
在您的 app.config 文件中:

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 -
In your app.config file:

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

程序启动期间的某处:

string docDir = Settings.Default.DocumentDirectory;

其他地方:

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.

对于 web 项目,你可以把它放在 web.config 文件中,像这样:

For web projects, you can put it in the web.config file, like so:

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

并通过以下方式访问:

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

这篇关于用c#打开word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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