如何在 Visual Studio 项目中获取或设置动态绝对文件路径?
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
属性 更改为 始终复制 。
现在使用下面的相对路径访问这个文件。
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");
这样,即使您 运行 您的代码在另一台计算机上也能正常工作。
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
属性 更改为 始终复制 。
现在使用下面的相对路径访问这个文件。
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");
这样,即使您 运行 您的代码在另一台计算机上也能正常工作。