I'm making an installer and I want it to extract to a specific user path like C:\Current User\Documents with current user being the current user installing it. Anyway to do this?
The most important thing is to never hard-code a path like C:\Current User\Documents or C:\Documents and Settings\username, because the actual folder names will vary depending on the Operating System and language of the user's computer.
In most installers, you can use a pre-defined command-line variable for various OS defined folders. For example, the variable %HOMEDRIVE% points to the default driver letter such as C:\, and %HOMEPATH% is usually the profile folder. So, on my computer,
%HOMEDRIVE%\%HOMEPATH% = C:\Users\username
The command-line variable %USERPROFILE% points to the same location. If you just need the username, use %USERNAME%.
If you are looking for the user's "My Documents" folder or other similar folders, you can get it from the registry:
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
Related
I want to store a JSON to the file system. Where should i store the files so that the user can't see/delete them and that the files won't be deleted after a phone reboot/restart?
ApplicationData The directory for application data specific to the user executing the program. On non-Windows operating systems, this
path is the value of the environment variable XDG_CONFIG_HOME if it is
set, otherwise the ".config" directory in the current user's home
directory.
CommonApplicationData The directory for data shared by applications. On non-Windows operating systems, the "/usr/share"
directory.
LocalApplicationData The directory for application data shared by users of the computer. On non-Windows operating systems, this path is
the value of the environment variable XDG_DATA_HOME if it is set,
otherwise the ".local/share" directory in the current user's home
directory.
Personal The personal or home directory for the current user. On non-Windows operating systems, this is the user's home directory.
Source: System.Environment.SpecialFolder
I don't understand what Personal is for exactly, but on most sites on the web i found people using it. Should i use one of these or am i looking at the wrong things?
And for storing at the moment (May 2017) my only two options are SQLite and PCLStorage.
You can use any of the below folders for your requirements. If I understood your questions correctly you want only the app to be able to access (create/updated/delete) certain files which should be preserved on phone reboot or restart and the user shouldn't be able to access the files. You can use any of the below special folders:
Environment.SpecialFolder.ApplicationData is $HOME/.config, which maps to /data/data/#PACKAGE_NAME#/files/.config.
Environment.SpecialFolder.LocalApplicationData is $HOME/.local/share, which maps to /data/data/#PACKAGE_NAME#/files/.local/share.
Environment.SpecialFolder.Personal is $HOME, which maps to /data/data/#PACKAGE_NAME#/files
Looking at the description and mapping to the file system, for saving a JSON file I would probably pick Environment.SpecialFolder.ApplicationData, but there seems to be nothing wrong in picking either of the three based on how they map in the file system.
Notice that all these paths are within the specific directory for your app package so no other apps will be able to access these files. And neither will you be able to access them from any file system (unless the phone is rooted).
If you want more information on any file path, you can iterate through them, like this:
static void PrintFolderPath(System.Environment.SpecialFolder folder)
{
Console.WriteLine ("{0}={1}", folder, System.Environment.GetFolderPath(folder));
}
More reading: https://forums.xamarin.com/discussion/3773/system-environment-specialfolder
The best way to find and get to AppData folder (specifically windows 7) is to type %AppData% in start command. But it takes me to C:\Users\user.name\AppData\Roaming
Why does it takes me to Roaming folder when the actual AppData Folder is one level up? It happens on both computers I have.
There is no environment variable for the root "AppData" folder because no one should ever put data there. Instead, applications put data into subfolders of the "AppData" folder depending on the nature of that data.
There are three subfolders:
Roaming
Local
LocalLow
The regular old %AppData% environment variable points to the path for the "Roaming" subfolder, which is where most applications should store their data, unless they have a specific reason that that data should not roam with the user's profile.
If you want the "Local" (non-roaming) sub-folder, use the %LocalAppData% environment variable instead.
As for typing this into the Start → Run dialog, that doesn't make a lot of sense to me. Data that is stored here is not intended for user consumption. It is private data stored by applications for their own use—things like configuration files, databases, etc. User-facing data should be in the Documents folder or at a path specified by the user. If you're a software developer accessing this folder for testing purposes, just go up one level.
Typing appdata (not %appdata%) in Run dialog (Win+R) takes you to %USERPROFILE%\AppData (i.e. %AppData%\..).
I was going through the source code of a rainmeter skin and i could not understand :
TextShortcut1=Computer
TextShortcut2=Libraries
TextShortcut3=Internet
TextShortcut4=Media Player
TextShortcut5=Control Panel
TextShortcut6=Trash
TextShortcut7=ShutDown
TextPath1=::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
TextPath2=shell:Libraries
TextPath3=http://google.com
TextPath4=shell:MusicLibrary
TextPath5=::{21EC2020-3AEA-1069-A2DD-08002b30309d}
TextPath6=::{645FF040-5081-101B-9F08-00AA002F954E}
TextPath7=rundll32.exe user32.dll LockWorkStation
Can anyone tell me what
::{20D04FE0-3AEA-1069-A2D8-08002B30309D}
::{21EC2020-3AEA-1069-A2DD-08002b30309d}
::{645FF040-5081-101B-9F08-00AA002F954E}
these are
and also how can we get one of these for a specific location from our computer.
Those are CLSID (Windows Class Identifiers). Certain special folders within the operating system are identified by unique strings.
20D04FE0-3AEA-1069-A2D8-08002B30309D is My Computer
21EC2020-3AEA-1069-A2DD-08002b30309d is Control Panel
645FF040-5081-101B-9F08-00AA002F954E is Recycle Bin
Source:
http://www.sevenforums.com/tutorials/110919-clsid-key-list-windows-7-a.html
In response to the comment:
can i have Class Identifiers for any folder on Computer or is it just
the bunch of those.
There isn't much reason for you to add more clsids, since you can just go to other locations by typing the normal path. This is a set list that is in the registry somewhere for special folders that don't really have "paths" like C:\windows does.
what is "shell:Something" is it a cmd command or location
shell: is similar to above. It is a convenient way of accessing special folders. Here is a good site for a list: http://smallvoid.com/article/winnt-shell-keyword.html . It is more of a shortcut for Windows Explorer to access a specific location than it is a command. You cant use them in batch files as far as I know (no command line stuff).
what is %something% like %temp%
Those are environment variables. You can usually count on certain ones existing, but the user can change these. Here is a list of some more: http://en.wikipedia.org/wiki/Environment_variables#Microsoft_Windows
how do they all differ?
Well, basically, they are just different ways of accessing the same thing. Some things are more backwards compatible than others, so you have to make that choice when the time comes. If you know your app is going to be on Windows 7 and above, you can make use of some of the more convenient shell:something ones. But if it needs to run on Windows 2000, you might have to rely more on older stuff like environment variables. Environment variables can also be customized by the user.
If I have a System and User environment variable with the same name, how are they processed? Are they concatenated? Does the user variable override the system variable? Taking that into account, if I need to add something to the Path variable, where is it more convenient to add it?
I think this article should answer you question: Environment variables in Windows NT
User environment variables
User environment variables can be
viewed from Control Panel as well. The
user may add, delete or modify the
environment variables in the User
Environment Variables for User field.
These variables take precedence over
system environment variables. The user
path is appended to the system path.
Everything splash says in their answer is correct. To be absolutely clear, there is a difference between how the user path environment variable is evaluated, and the other user environment variables are evaluated. A regular user environment variable overrides completely a system one with the same name if both exist, but only for the specific user it is specified for. However, the user path variables is treated differently. It is appended to the system path variable when evaluating, rather than completely replacing it. I believe splash states that, but they do it so concisely I think it needs spelling out.
Everything that splash and Simon say in their answers are correct. The idea that the user path variable is appended has been highlighted, and I believe the consequences of that difference require some additional treatment.
Path = %Path% (System) ; %Path% (User)
When you execute an executable program (or any executable script, such as .bat, .vbs, etc.) you do not need to provide the fully qualified path.
For instance, to run java, you can type in any of these:
C:/Program Files (x86)/Java/jre6/bin/java -version
java.exe -version
java -version
The first example uses a fully qualified path. This will always use the version of the Java at that exact path.
The second example will go through each of the directories in the %Path% environment variable, looking for an executable file named java.exe. It will run the very first one that is found, and stop searching. If there are two files named java.exe somewhere on the %Path%, only the first one found is used.
The third example, like the second, will iterate over the directories listed in the %Path%. In addition, because a file extension was not provided, a list of executable file extensions are appended to the name of the file, in the order specified in the %PATHEXT% environment variable. If there are several files named java.com, java.exe, java.bat, etc. somewhere on the %Path%, only the first one found is used.
You can see the list of executable path extensions on your system by creating the following batch file:
#echo off
echo %PATHEXT%
pause
On my machine, these are:
.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC;.PY
What does all this mean?
In stark contrast to other environment variable, the user path does not allow you to override the system path. The exact opposite is the case. From the examples above, there are many cases where you may with to change the default version of Java. However, if there is already a Java version listed in the system path, that is the version that will ALWAYS be found first, because the path is searched in order, from left-to-right, and the user path is appended on the right-hand side, with the system path on the left.
What can I do about it?
If you do not have access to system environment variables, you cannot override default programs on the system path by using the user path. (In fact, it must be this way, or certain programs would stop working correctly, and would open your system to tampering by malicious software. Nobody wants that.)
Instead, you must use a fully qualified path if you must use a specific version.
I need to create a directory inside a windows users 'home' directory (c:\Documents and Settings\someusername\ or c:\users\someusername\). This directory will exist permanently but will only contain temporary files.
What is is the best location for this directory within the users profile if I want to be a good citizen? I should note that my program will be run by (possibly) non-admin users and will only need access to their own profile, but they must have permission to create the folder.
Using My Documents\NameOfMyApp\ is possible I guess, but that seems intrusive.
Is there a better location for this type of data, and a specific MFC call to access it?
I'd consider using the AppData directory. You can get its location with SHGetSpecialFolderLocation passing it CSIDL_APPDATA; (or a number of alternatives -- nearly every version of Windows adds a new replacement for SHGetSpecialFolderLocation, SHGetSpecialFolderPath, or (often) both).
Take a look at the following win32 calls:
GetUserProfileDirectory
GetAllUsersProfileDirectory
GetDefaultUserProfileDirectory
You probably want to use GetUserProfileDirectory and put your data in a sub-directory with your appname.
You will definitely want to use the function because there is no "\documents and settings" folder on vista and up, they changed to it "\user".
Being a good application citizen, you should use:
[drive]:\Documents and Settings[username]\Application Data[AppName] or
[drive]:\Documents and Settings[username]\Local Settings\Application Data[AppName]
(On Vista and Win7, "Documents and Settings" is replaced, most sensibly with "Users")
The environment variable USERPROFILE will provide the, you guessed it, User Profile Path.
The TEMP path provides the path to the user's individual temp directory
If the temp files aren't user-specific, you could use C:\temp
EDIT: If you are to use a user-specific location, I highly recommend that you use the environment variables (USERPATH on XP and 2000) rather than hard-coding the paths.
-Waldo
P.S. Thank you for asking this. I see bad behaviour from Waaaay too many applications. The root of the C: drive is not where you should be dumping things! At the very least, (test for the presence of, creating if necessary, and) use C:\Temp.