Reputation: 99
I try to write some data with streamwriter in a .txt file and than to use those as cookies
var writer = new System.IO.StreamWriter(Application.StartupPath + "\Cookies\cookies.txt");
But after I install application from .exe file and execute, it shows the error "Access is denied in path ..."
I am not sure if the problem is in path of file that I want to use as a file for cookies or in process of creating installation MSI/Setup of Application, even I include Cookies folder in application.
Do you have any suggestion which is the best practice to save "cookies" in win application?
Upvotes: 1
Views: 628
Reputation: 7352
Seems your source code is under some place where file writing is allowed, but when you running your exe then the location of the execution is not allowed for file write. Because you are writting your file in the exe execution path so file write permission is depend upon that execution location. If you execute exe from a location where file write alowed it won't throw exception and if a flace where file write is not allowed then it will throw exception. So my suggestion is use a allowed static place where user have file write permission, like AppData or Documents like below
var writer = new System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\Cookies\cookies.txt");
or
var writer = new System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\Cookies\cookies.txt");
Hope it will help
Upvotes: 2
Reputation: 508
irst are you sure that the needed folder is under the proper folder structure? Because StartupPath will return the path to your exe file ?
Upvotes: 0