Shell Best Practices in Windows 10

I’m quite familiar with using the Shell class on the Mac, but moving to Windows 10 (which I know little about) has raised some issues.

  1. A command prompt window pops up when executing a Shell command. When I run Command Prompt from the start menu, if I do not run as administrator, then running my utility in Command causes another Command window to pop up. But if I run as administrator, then it runs the utility in the current Command window. I am guessing that if my Xojo app were to execute the shell command as admin, then perhaps that Command window wouldn’t pop up? What if the user running my software is not an administrator? Any other way to get rid of that window that pops up?

  2. It seems that there can be cases where one needs admin privileges when dealing with files

For instance, when I copied my application up to root ie c:\, my Xojo app was made read-only. I am imagining that the program needs to have admin privileges to write files to the root directory? That seems reasonable. But I think I need a folder where I can write files temporarily, with normal user privileges. And due to problem 3. below, I want a folder that has no spaces in the path. That cannot be guaranteed if the folder is in a user folder, because the user name could have spaces in it. Is there a tmp folder in Windows 10?

  1. If there is a space in the path, this is a problem for the utility I am running…if that path is one of the arguments. Even if I put it in quotes, the utility doesn’t deal with it properly. Now, I know that SpecialFolder.UserHome will modify the directory with a space…eg “Joe Blow” becomes “JOEBL~1”. I think my utility is happy with that for its arguments. But I don’t know how to convert the path with spaces into that modified version. What is the algorithm for that conversion? Or is there a way to perform that conversion, using say FolderItem?

TIA
Tom

  1. The Windows shell should not pop the command prompt window.

Could you post your code ?

  1. Indeed, the root needs administrator privileges. At any rate, using it for scratchpad is bad form.

Use Specialfolder.Temporary.

  1. Use ShellPath.

You should be warned, there’s not always a JOEBLO~1 conversion. If the user has a certain registry setting enabled, NTFS will not generate any 8.3 names and the long names will be all there are. Instead of short names, enclose the full pathname, spaces and all, inside double quotes. That should suffice as a command-line argument for any utility expecting a filename as a parameter.

Thanks for these tips.

Thanks for that detail Christian. Navigating all the ifs, ands, and buts can be tricky.

The gotcha with the quotes (which I tried) was that the utility being run would not accept a path with a space, even if in quotes, as a command line argument. I imagine its written in C, and taking the arguments as the array of strings passed into main().

In that case, I would use Xojo code to make a copy of the file to an 8.3 name, perform your command-line operation on the copy, then go from there.

ShellPath should always be in 8.3 style. Have you even tried, Tom ?

Shellpath does not return 8.3 names on my system, it returns the long path as I don’t have 8.3 enabled.

From what I read, it takes hacking the system to get that result. The immense majority of users won’t do what you did.

Besides, Microsoft does warn about this manipulation’s side effects:

Michel, I did try using ShellPath, and with success. Thanks.

Somehow those command windows are no longer popping up. I have no idea what changed.