views:

519

answers:

9

Just that... I get a string which contains a path to a file plus some arguments. How can I recognize the path? I thought about the index of the '.' in the file... but I don't like it.
What about using regular expressions? Can anyone point me in the right direction?

Regards

Edit: Theses are valid entries...
somefile.msi /a
C:\MyFolder\SomeFile.exe -i -d

I don't care much about the arguments cause once I have the path I'll assume the rest are arguments

+1  A: 

Well, before you tackle finding the path in the string, you need to outline a set of rules for what a path in this context

This is, for the operating system, a valid filename: a

Since no directory information is specified, the current directory will be used. The file has no extension.

But is still a filename.

Is this a path in your context? Or do you mean something that has directory information as well?

Examples of what you need to handle would be useful.

Lasse V. Karlsen
A: 

If you have any control over the string I'd recommend you change the way it is represented. One way would be to use URL-style parameters - e.g. fileName=myFile;arg1=value, etc. Then it's trivial to parse on keys.

The problem with any sort of raw parsing scheme is that the trailing data is not necessarily going to be clean. Also your filename is not necessarily NOT going to have spaces, extraneous "."'s etc. So whatever you decide is liable to not be correct 100% of the time.

Cory
I don't have any control on the incoming strings
sebastian
+4  A: 

You can't unless you access the file system, because paths may contain spaces.

So you might test each possible "file" using File.Exists. A string.Split() will help you here.

Bender
Don't do this. Major pitfall in using this method: An unexpected name will cause your check to abort. Imagine (under Win32) creating a folder named `C:\Program`. That would cause your method to assume `C:\Program Files` does not exist. Extra credit: Create C:\Program.exe and watch the fun.
Mihai Limbășan
A: 

Have a look at SYstem.IO.Path, you probably want GetDirectoryName(string)

CheGueVerra
+6  A: 

You can use System.IO.Path, and it's static methods.

bool isPath = System.IO.Path.GetDirectoryName(@"C:\MyFolder\SomeFile.exe -i -d") != String.Empty;
if (isPath)
{
    Console.WriteLine("The string contains a path");
}

The static Path class has several other methods which are useful as well, like .GetFilename, .GetExtension and .GetPathRoot.

You can also probably use System.IO.Directory and System.IO.File for additional features.

Frode Lillerud
+1  A: 

The normal way in this case is to tokenise by spaces (and use quotes for a filename with spaces in). Then use / or - for arguments. I think you'll be better off using a standard, accepted format than working for only a subset of cases.

Mark
A: 

thanks all for the answers.. there are a couple of posts I'd like to mark as the answer... what do you do in these cases?

Just in case somebody wants to see what I'm working on, it's a small application used to uninstall software from my computer. I posted the code, so feel free to download it and take a look at it.
The reason to this post is a good way to implement the private ProcessStartInfo GetProcessInfo(string uninstallString) method.

The reason to this project is posted here...

sebastian
I guess I'll mark as the answer the most voted post
sebastian
+1  A: 

Mmm, I believe that the only reliable way to do that is to access the filesystem, supposing it is reachable.
I would cut the string at the spaces, starting from the end, and take the longest that exist on the filesystem.

For example:

C:\My Folder\Some File.exe -i -d
=>
C:\My Folder\Some File.exe -i -d (no, although it might exist!)
C:\My Folder\Some File.exe -i    (no)
C:\My Folder\Some File.exe       (yes => That's this one)

You must take in account relative paths, and files in PATH (like your first example, ie. all exe files - even worse, you can write foo.exe or foo on the command line!).

Plus you can often write stuff like notepad/p, which doesn't simplify the algorithm, knowing that C:/windows/notepad.exe is a valid path in XP! :-)

PhiLho
+1  A: 

For non-MSI programs, UninstallString is passed to CreateProcess, so you probably want to replicate its method of determining the filename. Read http://msdn.microsoft.com/en-us/library/ms682425.aspx, especially the notes for lpApplicationName and the second half of lpCommandLine.

Programs installed by MSI have a separate system (msi!MsiConfigureProduct or msi!MsiRemovePatches).

Mark