-2

I am trying to write the data of a heartrate puls belt to a file. Everytime I start the programm it says "Access to the path denied".

The file is not "read-only". I also tried this from another question: File.SetAttributes(file, FileAttributes.Normal);

Debug.WriteLine(data[1]);
string path = @"C:\Users\...\DatenTest.txt";

using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write))
{
    var writer = new StreamWriter(stream, Encoding.UTF8);
    writer.Write(data[1]);

}
return data[1];

Does anybody has an idea?

Ron Beyer
  • 11,003
  • 1
  • 19
  • 37
Leo Ge
  • 31
  • 2
  • 4
    What is the actual path you are writing? There is a lot of stuff under "Users" that may or may not be available to non-elevated processes... – Ron Beyer May 10 '19 at 18:42
  • I just tried it with C:\Users\Documents\DatenTest.txt - didn't change anything :/ – Leo Ge May 10 '19 at 19:06
  • Is the file already locked by some other process? Does your application user have rights on the file? – Giulio Caccin May 10 '19 at 20:38
  • No the file is not used by anything else. I'm using it with my admin profil so I guess yes.. – Leo Ge May 10 '19 at 20:46

2 Answers2

0

It's not the file you're being restricted from, it's the path (e.g. folder). The three easy fixes are:

1) Adding "Everyone" permissions to the target folder's security tab in properties

2) Running your app in Administrator Mode

3) Switching to a folder without such stringent security (like "Documents")

Louis Ingenthron
  • 1,267
  • 8
  • 21
0

I think the problem is, that the programm is in UWP.

Leo Ge
  • 31
  • 2