[英]Custom setup project for “Setup and Deployment Visual Studio”- How to get the contents of the folder where the .msi file resides?
[英]How to get or setup dynamic absolute File Path in Visual Studio Project?
string path = @"c:\users\user\documents\visual studio 2015\Projects\PRACTICE\PRACTICE\Image\Boruto_logo.png";
if (File.Exists(path))
MessageBox.Show("Exists");
else
MessageBox.Show("Not Exist");
上面的代码是一个简单的程序,它检查完整的文件路径是否存在,但我想要的是而不是复制粘贴文件的完整路径,我希望它像这样的字符串 >> string path = "..\\Image\\Boruto_logo.png"
这样即使我将我的项目转移到另一台计算机它仍然可以正常运行
在您的项目文件夹中创建新的Images
文件夹(这是完全可选的)并将您的图像添加到其中。 Right click to folder
-> Add
-> Existing items
-> Select your image
现在将Copy to output directory
属性更改为Copy Always 。
现在使用下面的相对路径来访问这个文件。
using System.IO;
...
//GetCurrentDirectory() will give you current executable directory.
//Combine with current directory with your resource
string path = Path.Combine(Directory.GetCurrentDirectory(), @"Images\Boruto_logo.png");
if (File.Exists(path))
Console.WriteLine("Exists");
else
Console.WriteLine("Not Exist");
这样,即使您在另一台计算机上运行代码,它也能正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.