How is it possible that the program works in some computer and in some not?
Because the program depends on something specific to a computer, and this something is different between the two computers. For example, your error message says:
file not found 'C:UserseliAppDataRoamingfourmlinks.txt'
The program is looking for a file in the Users folder; that is specific to every user on every machine, different users and different machines will have different files.
will you put the file in a different place other then the program folder
Will I? No, I can't because I'm not that user on that machine. If you mean "can my program put the file in a different place ..." then yes, it can.
string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "fourmlinks.txt");
That will get a path to the logged-on user's private application data. If you have the "fourmlinks.txt" file in this folder on your machine but someone else doesn't have this file on their machine, then that will work on your machine but not on someone else's, as you asked.
string path = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
"fourmlinks.txt");
That will get the path to the installed file. If both you and someone else install the program, then both you and someone else will have the file. If either of you change the file, then the other gets the changes.
So should you always use GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
? Well, that depends. GetExecutingAssembly().Location
is typically a place standard users cannot write to, only read from. If you want users to only read and never write to your data, then that is a good place to put it. If you don't, then it isn't.
If you want to create a new file for users to write to and to read from, then write to Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
. If you want to install some starting data with your application but allow the user to change it, then you should include the starting data file in your application. It will get installed to System.Reflection.Assembly.GetExecutingAssembly().Location
. Then your application should do something like:
string fileName = "fourmlinks.txt";
string target = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), fileName);
if (!File.Exists(target))
{
string source = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), fileName);
File.Copy(source, target);
}
Another important difference between GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)
and Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
is that the former is part of your installer packages; if the user uninstalls or updates your application all their runtime changes will be deleted.
If you want each user to have their own private data that does not get deleted when the application is modified, then you want to use Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
.