eagle
eagle

Reputation: 461

How to find image path in c#?

I want to find image path but I didn't.

my image path : C:\Works\Web5.1.0\Src\Works.WebNext\Password\assets\img my image name : reset_password.jpg

I tried this : string path2 = Path.GetFullPath("reset_password.jpg"); but it's wrong path (output: C:\Windows\System32\inetsrv)

and tried this :

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
string a = Path.Combine(path, "reset_password.jpg");

output : (C:\Works\Web5.1.0\Src\Works.WebNext\bin )

I think, output should be like this : C:\Works\Web5.1.0\Src\Works.WebNext\Password\assets\img

one more thing : image path may not be same another computer so I think give a specific path is not right (for example : C:\Works\Web5.1.0\Src\Works.WebNext\Password\assets\img )

X computer : C:\Works\Web5.1.0\Src\Works.WebNext\Password\assets\img
Y computer : C:\Works\Web\Src\Works.WebNext\Password\assets\img

By the way, I am writing with c#.

How can I do ? Any ideas please.

Upvotes: 0

Views: 9029

Answers (3)

Pravin Kr. Mishra
Pravin Kr. Mishra

Reputation: 11

Try this for Web Applications.

string imagePath = "/Password/";  /* Your Image folder */
string path = Server.MapPath(@"ImagePath" + imagePath);

In Windows Application:

try
{
  System.IO.DirectoryInfo directory = new DirectoryInfo(@"Your local Image directory inside bin/debug");

  FileInfo result = null;
  var list = directory.GetFiles(); // Stackoverflow Exception occurs here
  
  if (list.Count() > 0)
  {
    result = list.OrderByDescending(f => f.LastWriteTime).First();
  }
                
  return result;
}
catch (Exception ex)
{
  throw ex;
}

Upvotes: 0

Julius A
Julius A

Reputation: 36

I'm not sure I've understood the issue but an idea of how to find the same file on many systems is:

AppDomain.CurrentDomain.BaseDirectory

This will give you the location of the folder that your running executable is in. The output is in the format of "C:\Folder\Folder\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\".

So, you can put the image in the same folder as the .exe, add "imagefilename.type" and you should find it.

Please clarify if this doesn't answer your question.

Upvotes: 2

Amey Kamat
Amey Kamat

Reputation: 181

Following code will give you your desired output

string folderPath = @"C:\Works\Web5.1.0\Src\Works.WebNext\Password\assets\img";
string imgFilePath = Path.Combine(folderPath, "reset_password.jpg")

Assembly.GetExecutingAssembly() gives the path of the current working directory i.e. the path from where your executables are being run (in your case its 'bin' folder)

Upvotes: 0

Related Questions