如何从随机图片框中获取文件名?

发布于 2025-02-14 02:16:02 字数 394 浏览 8 评论 0原文

我有一个问题,我正在制作一个猜测应用程序,然后单击按钮picture框以从目录中获取随机图像。

Random rnd = new Random();

private void random_Click(object sender, EventArgs e)
{
    string folder = @"D:\folder";
    string[] files = Directory.GetFiles(folder);

    pictureBox1.ImageLocation = files[rnd.Next(files.Length)];
}

现在我想做if语句。如果图片名称为“ apple.png”,则执行此操作,如果“橙色。我可以做这样的事情吗?

欢呼,祝你有美好的一天!

I have a question, I'm making a guess app and clicking the button PictureBox to get a random image from the directory.

Random rnd = new Random();

private void random_Click(object sender, EventArgs e)
{
    string folder = @"D:\folder";
    string[] files = Directory.GetFiles(folder);

    pictureBox1.ImageLocation = files[rnd.Next(files.Length)];
}

And now I want to make if statement. If the picture name is "apple.png" do this, if "orange.png" do this. Can I make something like this?

Cheers and have a good day!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〃温暖了心ぐ 2025-02-21 02:16:02

Okey,我一个人……

如果有人在寻找它:

string filePath = pictureBox1.ImageLocation;
string fileName = Path.GetFileName(filePath);

if (fileName == "yourfilename.png")
{
 do this
}

okey, i got it alone...

If someone is looking for it:

string filePath = pictureBox1.ImageLocation;
string fileName = Path.GetFileName(filePath);

if (fileName == "yourfilename.png")
{
 do this
}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文