open word doc with c#

风流意气都作罢 提交于 2019-12-25 02:54:00

问题


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,

eg

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

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


回答1:


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>

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:

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

And access via:

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


来源:https://stackoverflow.com/questions/2786999/open-word-doc-with-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!