Solved

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

Posted on 2014-09-30
5
989 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 34

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

737 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