Solved

How to get the file path by file name in C# or Vb.Net?

Posted on 2014-09-30
5
961 Views
Last Modified: 2014-10-01
I am calling an .exe file from my .Net Application which is located in the Program Files Folder.

Dim strBatchFile As String = String.Empty
strBatchFile = "C:\Program Files (x86)\VanDyke Software\Clients\sfxcl.exe"

sfxcl.exe is located in the
C:\Program Files (x86)\VanDyke Software\Clients

I assume some of us have installed 64 bit version and I guess file might have located in this folder path.

C:\Program Files\VanDyke Software

How to get the file path dynamically, by passing file name in .Net ?

Cheat code would be, if sfxcl.exe is not located in the C:\Program Files (x86)\ , then look at C:\Program Files\
0
Comment
Question by:chokka
  • 2
  • 2
5 Comments
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40352824
You could invoke the command line utility find from within your .NET, and while it would work, 1) It could be slow and 2) you'd have to parse the strings that get printed back to STDOUT. I would say to just guess at one path and see if it exists; if it does not, try the other path; if you can't find either, raise an exception or modify your location algorithm a bit. Not finding the file in either place means that the user installed to some exotic directory.

e.g.

strBatchFile = "C:\Program Files (x86)\VanDyke Software\Clients\sfxcl.exe";

if (!File.Exists(strBatchFile))
{
    strBatchFile = "C:\Program Files\VanDyke Software\Clients\sfxcl.exe";

    if (!File.Exists(strBatchFile))
    {
        throw new FileNotFoundException("Could not locate batch utility.");
    }
}

// Do something with strBatchFile

Open in new window

0
 
LVL 33

Accepted Solution

by:
it_saige earned 500 total points
ID: 40352831
System.Environment to the rescue.

C# Method:
	static string ProgramFilesx86()
	{
		if( 8 == IntPtr.Size || (!String.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
		{
			return System.Environment.GetEnvironmentVariable("ProgramFiles(x86)");
		}
		return System.Environment.GetEnvironmentVariable("ProgramFiles");
	}

Open in new window


VB.NET Method:
	Private Shared Function ProgramFilesx86() As String
		If 8 = IntPtr.Size OrElse (Not [String].IsNullOrEmpty(System.Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))) Then
			Return System.Environment.GetEnvironmentVariable("ProgramFiles(x86)")
		End If
		Return System.Environment.GetEnvironmentVariable("ProgramFiles")
	End Function

Open in new window


Used like:
Dim strBatchFile As String = String.Empty
strBatchFile = String.Format("{0}\VanDyke Software\Clients\sfxcl.exe", ProgramFilesx86())

Open in new window


You could also use the SpecialFolder Enumeration:

http://msdn.microsoft.com/en-us/library/system.environment.specialfolder(v=vs.100).aspx

More about System.Environment:

http://msdn.microsoft.com/en-us/library/system.environment(v=vs.100).aspx

-saige-
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 40352897
If you decide to use it_saige's suggestion, I highly advise you to comment why you are checking for the size of the IntPtr. He, you, and I may understand what's going on with that check, but the junior developers coming behind you probably won't.
0
 

Author Comment

by:chokka
ID: 40352934
it_saige, Thank you !!

Kaufmed, I will clarify your question , sometime later today. Let me finish this work & test the function
0
 

Author Closing Comment

by:chokka
ID: 40355438
Thanks
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question