Solved

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

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

816 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

11 Experts available now in Live!

Get 1:1 Help Now