简体   繁体   English

c#相对路径

[英]c# Relative path

Hello I have a program made in Visual Studio in C#. 您好,我在C#中使用Visual Studio制作了一个程序。 In this program I have some pictures that need to be called. 在这个程序中,我有一些需要调用的图片。 And some html files that need to be saved. 还有一些需要保存的html文件。 All these files are saved in the map 'mails'. 所有这些文件都保存在地图“邮件”中。 At the moment I have the path: 目前我有路径:

 String pad = @"C:\Users\Charlotte\Desktop\proof of concept\mails\";

I need this path to be relative so the user of the program can just copy the map mails to wherever he/she wants it and execute the exe of my program and just work with it. 我需要这个路径是相对的,所以程序的用户可以将地图邮件复制到他/她想要的任何地方并执行我的程序的exe并使用它。

Can anyone help me? 谁能帮我?

您可以使用函数: Directory.GetCurrentDirectory()来获取当前目录的路径,而不是告诉程序完整路径。

If the user places a file wherever he wants, you need to tell your program where it is aomehow. 如果用户将文件放在他想要的任何地方,您需要告诉您的程序它在哪里是aomehow。 So if you use OpenFile dialog your path would be: 因此,如果您使用OpenFile对话框,您的路径将是:

string pad = openFileDialog1.FileName;

On the other hand if you mean that a path should be relative to your exe file you would use: 另一方面,如果您的意思是路径应该相对于您的exe文件,您将使用:

string pad = Application.StartupPath + "\\mails\\";

This will mean that a path is a directory of your exe \\mails, but without a name of the file. 这意味着路径是exe \\ mails的目录,但没有文件名。

试试吧

string path = @"..\..\mails\";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM