在项目C#WPF中找不到文件夹路径

编程入门 行业动态 更新时间:2024-10-27 16:29:47
本文介绍了在项目C#WPF中找不到文件夹路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将gridview导出到excel,我将其保存在我的项目中的文件夹中,之后我将设置密码到excel文件。 问题是我找不到路径 错误: 抱歉,我们找不到../../files/Test.xlsx。它是否可能被移动,重命名或删除? 我尝试过:

I am exporting my gridview to excel and i am saving it in the folder which is in my project and after that i am setting a password to the excel file. Problem is i cannot find the path ERROR: Sorry, we couldn't find ../../files/Test.xlsx. Is it possible it was moved, renamed or deleted? What I have tried:

public static void Setpassword() { string filename = "../../files/Test.xlsx"; string password = "1245"; if (!File.Exists(filename)) return; Microsoft.Office.Interop.Excel.Application oexcel; Microsoft.Office.Interop.Excel.Workbook obook; oexcel = new Microsoft.Office.Interop.Excel.Application(); oexcel.DisplayAlerts = false; obook = oexcel.Workbooks.Open(filename, 0, false, 5, "", "", true, System.Reflection.Missing.Value, "\t", false, false, 0, true, 1, 0); //ERROR : Sorry, we couldn't find ../../files/Test.xlsx. Is it possible it was moved, renamed or deleted? try { obook.SaveAs(filename, Password: password, ConflictResolution: Microsoft.Office.Interop.Excel.XlSaveConflictResolution.xlLocalSessionChanges); } finally { obook.Close(); oexcel.Quit(); } }

推荐答案

不要提及../ .. /files/Test.xlsx存储在项目目录中。 使用如下, Hi, don't mention like "../../files/Test.xlsx" to store in the directory of the project. Use like the following, string filename = Environment.CurrentDirectory + "\Test.xlsx"; //Environment.CurrentDirectory - represents the current working directory of the project.

或者您也可以使用它,

Or you can use this also,

string filename = AssemblyDirectory + "\Test.xlsx"; public static string AssemblyDirectory { get { string codeBase = Assembly.GetExecutingAssembly().CodeBase; UriBuilder uri = new UriBuilder(codeBase); string path = Uri.UnescapeDataString(uri.Path); return Path.GetDirectoryName(path); } } //Here AssemblyDirectory is a string property, which also represents the directory of the current project.

更多推荐

在项目C#WPF中找不到文件夹路径

本文发布于:2023-11-13 10:10:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1584101.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:中找   文件夹   路径   项目   WPF

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!