Solved

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

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

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 32

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 74

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

743 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

16 Experts available now in Live!

Get 1:1 Help Now