Click here to Skip to main content
15,896,497 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Preface:
This is my first winform application and I am aware of my ignorance on the issue. Please clarify any fundamental flaws I may be propagating.

I have an application that attempts to write a text file to the C: drive of the computer it is installed on. The purpose of this is to be able to read and write to the file at a later point. It works locally on my machine, but access is denied when the application first tries to write the file on a client machine and rightfully so I reckon. I understand that this is not a good idea.

So my question ultimately would be, where is a better place to try and write that file without experiencing permission issues? Or, is there a better way to store readable/writable string data when a database cannot be involved?

Code:
var path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

using (StreamWriter clientData = new StreamWriter(path, true))
                       {
                           clientData.Write(string.Format("{0}, {1},", clientFirstName.Text.ToUpper(), clientLastName.Text.ToUpper()));
                           clientData.Write(string.Format("{0},", clientAddress.Text.ToUpper()));
                           clientData.Write(string.Format("{0},", clientPhone.Text.ToUpper()));
                           clientData.Write(string.Format("{0},", clientEmail.Text.ToUpper()));
                           clientData.Write(string.Format("{0},", (cbMonth.Text + "/" + cbDay.Text + "/" + cbYear.Text)));
                           clientData.Write(string.Format("{0},", clientStartDate.Text.ToUpper()));
                           clientData.Write(string.Format("{0},", clientProducts.Text.ToUpper()));
                           clientData.Write(string.Format("{0},", clientWeight.Text.ToUpper()));
                           clientData.Write(string.Format("{0}, {1}, {2}, {3}, {4},", clientChest.Text.ToUpper(), clientBelly.Text.ToUpper(), clientHips.Text.ToUpper(), clientThigh.Text.ToUpper(), clientArm.Text.ToUpper()));
                           clientData.Write(string.Format("{0}, {1}, {2}, {3}, {4}, {5}, {6}, {7},", clientBodyFat.Text.ToUpper(), clientHydration.Text.ToUpper(), clientMuscleMass.Text.ToUpper(), clientPhysiqueRating.Text.ToUpper(), clientBMR.Text.ToUpper(), clientMetabolicAge.Text.ToUpper(), clientBoneMass.Text.ToUpper(), clientVisceralFat.Text.ToUpper()));
                           clientData.Write(string.Format("{0}", commentsNotes.Text.ToUpper()));
                           clientData.Write(clientData.NewLine);
                           success = true;
                       }
Posted
Updated 2-Oct-13 10:52am
v2
Comments
ZurdoDev 2-Oct-13 15:32pm    
If no db then all you have is a text file or the event viewer I suppose.
Richard C Bishop 2-Oct-13 15:38pm    
That is what I figured. Do you have a recommendation on a location to write the file to?
ZurdoDev 2-Oct-13 15:40pm    
I suppose you could write to the applications folder only because it seems reasonable, to me at least, that a program should have access to that folder.
Richard C Bishop 2-Oct-13 16:46pm    
Ok, thank you RyanDev.
Kornfeld Eliyahu Peter 2-Oct-13 17:10pm    
Access right does not depends no application but on login identity...

Some suitable locations to save application data are the AppData folder or the user's MyDocuments folder. Environment.GetFolderPath()[^] is your friend.

/ravi
 
Share this answer
 
Comments
ridoy 2-Oct-13 16:05pm    
i think it's fair enough,a 5.
Richard C Bishop 2-Oct-13 16:51pm    
Thank you Ravi, I tried that and am still getting permission error. I will post the code I am using above in my thread.
Richard C Bishop 2-Oct-13 17:16pm    
I got it to work. I was not specifying the file name at the end of the folder, de de deee.
Ravi Bhavnani 2-Oct-13 17:37pm    
I was just about to tell you that. :)

/ravi
Call following method before writing the file.
C#
private bool GrantAccess(string fullPath)
        {
            DirectoryInfo dInfo = new DirectoryInfo(fullPath);
            DirectorySecurity dSecurity = dInfo.GetAccessControl();
            dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.Write, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
            dInfo.SetAccessControl(dSecurity);
            return true;
        }

When using literal strings for the FileSystemAccessRule, it should be WellKnownSidType.WorldSid instead of "everyone".
The reason is because there are multiple Window languages and Everyone only applies to EN ones, so for Spanish, it might be "Todos" (or something else).
trukin wrote this in his solution and it works.
Following assemblies will be required:
System.IO
System.Security.AccessControl
System.Security.Principal
 
Share this answer
 
v2
Comments
Richard C Bishop 3-Oct-13 9:37am    
Thank you for that info, it was very helpful.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900