Solved

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

Posted on 2014-09-30
5
891 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now