none
Access to filepath is denied when saving variable data to text file RRS feed

  • Question

  • Hi all,

    I'm trying to save variable data to a text file, but whenever I try to write the code, I get the error message "Access to the path [file location] is denied." How do I solve this problem?

    Here is my code used to save:

    string goalsFilePath = @"E:\Programs\NutrientTracker\NutritionGoals.txt";
    List<string> nutritionGoals = new List<string>();
                            nutritionGoals.Add(calorieMin.ToString());
                            nutritionGoals.Add(calorieMax.ToString());
                            nutritionGoals.Add(gramsFatMin.ToString());
                            nutritionGoals.Add(gramsFatMax.ToString());
                            nutritionGoals.Add(gramsCarbsMin.ToString());
                            nutritionGoals.Add(gramsCarbsMax.ToString());
                            nutritionGoals.Add(gramsProteinMin.ToString());
                            nutritionGoals.Add(gramsProteinMax.ToString());
    
    foreach (string line in nutritionGoals)
    {
        sw.WriteLine(nutritionGoals);
    }

    Any help would be appreciated.   I tried to use the File.WriteAllText method as well but with the same result.



    • Edited by CapnFedora Tuesday, July 9, 2019 8:16 PM
    Tuesday, July 9, 2019 7:54 PM

All replies

  • All Windows programs run under the context of a Windows O/S user account. If this is a Windows desktop program, then it is running under the context of the  user account the user has used to login on the Windows O/S, and those are the credentials that are being presented by the program to access a given resource controlled by the O/S.

    If the user account credentials do not have the permissions to access a given resource controlled by the O/S, then it is 'access denied'.

    Tuesday, July 9, 2019 8:39 PM
  • I was able to write the data in a test program I used to learn how to do this.  Then when I used a different program (with the code shown above) I got the error.  Same PC, same account.  
    Tuesday, July 9, 2019 8:42 PM
  • I was able to write the data in a test program I used to learn how to do this.  Then when I used a different program (with the code shown above) I got the error.  Same PC, same account.  

    Does the test program still write without errors or does it error out?

    Did you create a application manifest in the test program and not the current program?



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 9, 2019 8:53 PM
    Moderator
  • I was able to write the data in a test program I used to learn how to do this.  Then when I used a different program (with the code shown above) I got the error.  Same PC, same account.  

    What's the O/S, Windows 10? 
    Tuesday, July 9, 2019 9:57 PM
  • Actually, the tutorial I followed said to create an application manifest, but I didn't realize it at the time.  However, it still worked just fine in the test program.

    How should I use an application manifest?

    Tuesday, July 9, 2019 10:34 PM
  • Actually, the tutorial I followed said to create an application manifest, but I didn't realize it at the time.  However, it still worked just fine in the test program.

    How should I use an application manifest?


    Yash, it doesn't mean anything if running on Win 10 and rolling the dice. It's either 7 come 11 and it's good or you're rolling craps and scratching your head. :)
    Tuesday, July 9, 2019 11:48 PM
  • Hi CapFedora,

    Thank you for posting here.

    Based on your description, you want to solve the error that Access to filepath is denied.

    You could try File.ReadAllLines method. Here is a simple code for your reference.

    Code:

                string path = @"D:\test1.txt";
                List<string> nutritionGoals = new List<string>();
                nutritionGoals.Add("test1");
                nutritionGoals.Add("test2");
                nutritionGoals.Add("test3");
                nutritionGoals.Add("test4");
                File.WriteAllLines(path, nutritionGoals.ToArray());

    Result:

    >> How should I use an application manifest

    I want to know what the relation between manifest and your current problem is. If it is a another problem, you could create a new thread because one thread for one question.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 10, 2019 2:26 AM
    Moderator
  • Actually, the tutorial I followed said to create an application manifest, but I didn't realize it at the time.  However, it still worked just fine in the test program.

    How should I use an application manifest?


    Yash, it doesn't mean anything if running on Win 10 and rolling the dice. It's either 7 come 11 and it's good or you're rolling craps and scratching your head. :)
    Sorry, I'm not sure I understand what you're trying to say. What do you mean?
    Thursday, July 11, 2019 1:13 PM
  • I appreciate the advice, but I got the same error using that method.  'Access to the path (path) is denied.'

    Is there anything else aside from the code you gave me I need to add to my program, or any settings I need to change? I made sure that the text file is not Read Only.

    Thursday, July 11, 2019 1:18 PM
  • When you open the windows explorer and navigate to E:\Programs\NutrientTracker\ Can you create a file in this directory? (Right mouse button click -> New -> Textfile).
    Friday, July 12, 2019 5:38 AM
  • Actually, the tutorial I followed said to create an application manifest, but I didn't realize it at the time.  However, it still worked just fine in the test program.

    How should I use an application manifest?


    Yash, it doesn't mean anything if running on Win 10 and rolling the dice. It's either 7 come 11 and it's good or you're rolling craps and scratching your head. :)

    Sorry, I'm not sure I understand what you're trying to say. What do you mean?

    It means that if you're using Win 10, then something you think shouldn't be happening does. Win 10 is the most flakey of the more recent Windows O/S(s).

    They talked about Vista and how flakey it was? Win 10 is right there with Vista. :)

    Maybe you should consider going to the O/S and take OwnerShip of the folder and see if that stops the error.

    Friday, July 12, 2019 7:30 AM