0

我有这个(并且有效):

if (Sport == "Athletics")
            {
                excel_init("C:/Users/Dries Canfyn/Desktop/score/ScoretableMenAthletics.xlsx");
            }

但现在我想在我的项目中包含 excel 文件。

excel 文件现在位于一个名为 IO 的文件夹中,该文件夹与我的班级所在的这个 'if' 所在的文件夹相同。我怎样才能访问 excel 文件?我应该使用什么路径?

4

3 回答 3

0

Determine your WorkingDirectory and then use a relative path from there.
Usually the WorkingDirectory is where your .exe resides, but it depends on how you started your process. Then you can use a relative path like

if (Sport == "Athletics")
     {
         excel_init("IO/ScoretableMenAthletics.xlsx");
     }

I don't know what happens inside your excel_init, but consider MSDN, ProcessStartInfo.WorkingDirectory Property:

When the UseShellExecute property is false, gets or sets the working directory for the process to be started. When UseShellExecute is true, gets or sets the directory that contains the process to be started.

于 2013-05-21T09:28:36.097 回答
0

您可以使用“资源文件”板的方法轻松地在项目中添加和访问您的文件。这是许多可能性之一。

在您的项目中添加文件后。

  1. 右键单击文件并单击属性
  2. 在文件属性中将“构建操作”更改为“嵌入式资源”
  3. 和“复制到输出目录”到“如果从不复制”

每次编译项目时,文件都会在执行过程中添加并实现。像这样。

//Get the full path of the application directory "release / debug" depending on your compiler.
string currentPath = AppDomain.CurrentDomain.BaseDirectory;
excel_init(currentPath + @"IO\ScoretableMenAthletics.xlsx");

图片要“编辑构建动作”和“复制到输出目录”

我希望这对你有帮助

于 2013-05-21T09:38:40.640 回答
0

您可以使用Server.MapPath

string path = HttpContext.Current.Server.MapPath("~/IO/ScoretableMenAthletics.xlsx");
于 2013-05-21T09:39:54.867 回答