使用C#引用存储在项目的bin \ debug文件夹中的docx文件 [英] Refer to docx file stored in bin\debug folder of project using c#

查看:204
本文介绍了使用C#引用存储在项目的bin \ debug文件夹中的docx文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用程序项目bin \ debug文件夹中添加了一个.docx文件,我想使用c#在代码中提供其路径.我应该怎么做??

在此先感谢!!! ....

I have added a .docx file in my windows application project bin\debug folder and i want to give path of it in code using c#. how should I do it ???

Thanks in Advance !!!....

推荐答案

"bin \ debug"字典仅存在于项目中;这不是可执行文件目录.但是,当您部署应用程序时(仅通过将输出目录复制到应用程序应该工作的任何其他目录中),它可以是任何其他目录.另外,"bin \ debug"目录名称表明这是一个调试版本,通常部署发行版本.实际上,这些名称没有什么特别的意思.您可以定义任意数量的构建配置,例如调试"和发布",并根据需要将输出定向到不同的输出目录.

在所有情况下,您实际上都需要在运行时确定应用程序所在的目录.由于可以将应用程序定位"在一个以上的目录中,因此您需要确定一些内容.最好是这样:应用程序的入口程序集的主要可执行模型的位置.另外,此代码不应依赖于从哪个程序集调用.

这是这样的:
The "bin\debug" dictionary exists only in the project; this is not the executable directory. But when you deploy the application (just by copying of output directory to any other directory where the application is supposed to work), it could be any other directory. Also, the "bin\debug" directory name suggests that this is a debug version, and you usually deploy release version. If fact, these names mean nothing special; you can define any number of build configurations like "Debug" and "Release" and direct output to different output directories at you liking.

In all cases, you really need to determine the directory where you application is located during run-time. As the application can be "located" in more then one directory, you need something certain. This should better be the following: the location of the main executable model of the entry assembly of your application. Also, this code should not depend on which assembly it is called from.

This is how:
string exeDirectory = 
    System.IO.Path.GetDirectoryName(
        System.Reflection.Assembly.GetEntryAssembly().Location);

此代码是最通用的.还有其他方法,但是它们可能依赖于某些其他库(如Forms)或根据托管应用程序的方式产生不同的结果.相比之下,此方法对于所有类型的应用程序始终正确起作用.

顺便说一句,如果要在此目录中存储一些数据文件,则应了解该目录通常应该是只读的,因此这些文件也应以只读模式使用.

一种替代方法(一种非常好用的方法)是将数据存储在可执行模块中嵌入的资源中.但这是一个不同的故事.如有兴趣,请询问后续问题或其他问题.

—SA

This code is the most universal. There are other methods, but they may depend on some other library (like Forms) or produce different results depending on how application is hosted. In contrast, this method always works correctly for all types of applications.

By the way, if you want to store some data files in this directory, you should understand that this directory should normally be read-only, so those files also should be used in a read-only mode.

An alternative (and a very good and easy-to-use method) is to store data in the resources embedded in the executable module. But this is a different story. Ask a follow-up or a separate question if interested.

—SA


这篇关于使用C#引用存储在项目的bin \ debug文件夹中的docx文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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