动态文件路径和图像命令



我正在尝试创建一个文件路径,该路径的末尾将是一个"images"文件夹,程序将使用该文件夹与Image命令一起加载jpeg文件。我想让程序动态地知道从启动可执行文件的图像文件夹加载jpeg文件。我能够在这个网站上找到这个,我添加了2个底部代码行:

public static string AssemblyDirectory
{
get
{
string codeBase = Assembly.GetExecutingAssembly().CodeBase;
UriBuilder uri = new UriBuilder(codeBase);
string path = Uri.UnescapeDataString(uri.Path);
return System.IO.Path.GetDirectoryName(path);
}
}
public static string imagePath = @"images";
public static string finalImagePath = AssemblyDirectory + imagePath;

如果我设置了一个断点,"finalImagePath"是:"C:\Users\My Name\Documents\Visual Studio 2010\Projects\Universal Serial Diagnostics\bin\Debug\images">

这是正确的,但我如何将其与结合起来

Image image = Image.FromFile(@"C:UsersMy NameDesktopDipENV500008.jpg");

将硬编码路径替换为动态路径。ENV500008.jpg将存储在images文件夹中。非常感谢。

string path = AppDomain.CurrentDomain.BaseDirectory + "ENV500008.jpg";
if (System.IO.File.Exists(path))
{
Image image = Image.FromFile(path);
// .. the rest of the code that uses the image .. 
}
Image image = Image.FromFile(@finalImagePath + "\ENV500008.jpg");
Image image5 = Image.FromFile(@finalImagePath + "\ENV510829-2.jpg");

public static string GetAnyPath(string fileName)
{
//my path where i want my file to be created is : "C:\Users\{my-system-name}\Desktop\Me\create-file\CreateFile\CreateFile\FilesPosition\firstjson.json"
var basePath = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath.Split(new string[] { "\CreateFile" }, StringSplitOptions.None)[0];
var filePath = Path.Combine(basePath, $"CreateFile\CreateFile\FilesPosition\{fileName}.json");
return filePath;
}

根据需要将.json更改为任何类型参考:https://github.com/swinalkm/create-file/tree/main/CreateFile/CreateFile

最新更新