如何在Visual Studio 2010中的项目文件夹中访问bin / debug中的文件?

时间:2023-01-06 07:35:33

I have my docx.xsl file in my project/bin/debug folder.Now i want to access this file whenever i needed.But i could not able to access this file.

我在我的project / bin / debug文件夹中有我的docx.xsl文件。现在我想在需要时访问此文件。但是我无法访问此文件。

 WordprocessingDocument wordDoc = WordprocessingDocument.Open(inputFile, true);
 MainDocumentPart mainDocPart = wordDoc.MainDocumentPart;
 XPathDocument xpathDoc = new XPathDocument(mainDocPart.GetStream());
 XslCompiledTransform xslt = new XslCompiledTransform();

 string xsltFile = @"\\docx.xsl"; // or @"docx.xsl";

 xslt.Load(xsltFile);
 XmlTextWriter writer = new XmlTextWriter(outputFile, null);
 xslt.Transform(xpathDoc, null, writer);
 writer.Close();
 wordDoc.Close();

Please Guide me to put correct valid path to access docx.xsl file...

请指导我输入正确的有效路径来访问docx.xsl文件...

4 个解决方案

#1


32  

You can determine the location of your executable, and assuming the file will be deployed with the application to the relevant directory, then this should help you find the file in debugging and in deployment:

您可以确定可执行文件的位置,并假设文件将与应用程序一起部署到相关目录,这样可以帮助您在调试和部署中找到该文件:

string executableLocation = Path.GetDirectoryName(
    Assembly.GetExecutingAssembly().Location);
string xslLocation = Path.Combine(executableLocation, "docx.xsl");

You might need the following namespaces imported at the top of your file:

您可能需要在文件顶部导入以下命名空间:

using System;
using System.IO;
using System.Reflection;

#2


7  

If you add the file as resource you don't need to deal with paths at runtime.

如果将文件添加为资源,则无需在运行时处理路径。

  • Add the file to the visual studio project and set the build action to "Embedded Resource".
  • 将文件添加到visual studio项目并将构建操作设置为“Embedded Resource”。

The name of the resource is the project default namespace + any folders just like any code file in the project.

资源的名称是项目默认名称空间+任何文件夹,就像项目中的任何代码文件一样。

string resourceName = "DefaultNamespace.Folder.docx.xsl";

If you have the code in the same folder you can do like this

如果您将代码放在同一文件夹中,则可以执行此操作

string resourceName = string.Format("{0}.docx.xsl", this.GetType().Namespace);
  • Then you read the file using a resource stream Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
  • 然后使用资源流Assembly.GetExecutingAssembly()读取文件.GetManifestResourceStream(resourceName)

In your case, it would look like this:

在你的情况下,它看起来像这样:

using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (var reader = XmlReader.Create(stream))
    xslt.Load(reader);

#3


1  

Application.StartupPath gives you the full path upto bin/debug.

Application.StartupPath为您提供bin / debug的完整路径。

So what you need to do is:

所以你需要做的是:

string xsltFile =Application.StartupPath + @"\\docx.xsl";

#4


-2  

In order to access file from Bin/Debug folder you only have to specify file name. See below

要从Bin / Debug文件夹访问文件,您只需指定文件名。见下文

xslt.Load("docx.xsl");

#1


32  

You can determine the location of your executable, and assuming the file will be deployed with the application to the relevant directory, then this should help you find the file in debugging and in deployment:

您可以确定可执行文件的位置,并假设文件将与应用程序一起部署到相关目录,这样可以帮助您在调试和部署中找到该文件:

string executableLocation = Path.GetDirectoryName(
    Assembly.GetExecutingAssembly().Location);
string xslLocation = Path.Combine(executableLocation, "docx.xsl");

You might need the following namespaces imported at the top of your file:

您可能需要在文件顶部导入以下命名空间:

using System;
using System.IO;
using System.Reflection;

#2


7  

If you add the file as resource you don't need to deal with paths at runtime.

如果将文件添加为资源,则无需在运行时处理路径。

  • Add the file to the visual studio project and set the build action to "Embedded Resource".
  • 将文件添加到visual studio项目并将构建操作设置为“Embedded Resource”。

The name of the resource is the project default namespace + any folders just like any code file in the project.

资源的名称是项目默认名称空间+任何文件夹,就像项目中的任何代码文件一样。

string resourceName = "DefaultNamespace.Folder.docx.xsl";

If you have the code in the same folder you can do like this

如果您将代码放在同一文件夹中,则可以执行此操作

string resourceName = string.Format("{0}.docx.xsl", this.GetType().Namespace);
  • Then you read the file using a resource stream Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)
  • 然后使用资源流Assembly.GetExecutingAssembly()读取文件.GetManifestResourceStream(resourceName)

In your case, it would look like this:

在你的情况下,它看起来像这样:

using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
using (var reader = XmlReader.Create(stream))
    xslt.Load(reader);

#3


1  

Application.StartupPath gives you the full path upto bin/debug.

Application.StartupPath为您提供bin / debug的完整路径。

So what you need to do is:

所以你需要做的是:

string xsltFile =Application.StartupPath + @"\\docx.xsl";

#4


-2  

In order to access file from Bin/Debug folder you only have to specify file name. See below

要从Bin / Debug文件夹访问文件,您只需指定文件名。见下文

xslt.Load("docx.xsl");