Marcello Grechi Lins
Marcello Grechi Lins

Reputation: 3410

File Writing Permission

I am writting an app to dump content into files.

The flow, so far is this:

1 - User enters a Path

2 - App checks if the Directory Exists and if i have Writting Permissions

 2.a - If if exists, creates a empty file inside and closes it
 2.b - If it does not, creates the directory and the file inside it, and closes the file

The problem is, once i reach the point when i actually have to write content in this file i previously created i can't write a single line , it throws me a "Access to Path Denied".

Here is the code snippet related to the issue :

        // Writting ImgPixels to file

        if (BDC.BDCCommons.CommonUtils.DirectoryHasWritePermissions(folderPath))
        {
            using (StreamWriter filePointer = File.AppendText(folderPath))
            {
                filePointer.WriteLine(imgPixels);
            }
        }
        else
        {
            LogWriter.Error ("Permissão Negada", "Diretorio [ " + folderPath + "] nao tem permissao de escrita");
        }

What might be causing this ?! Thanks in advance Guys

Improving Question:

@500 - Internal Server Error : folderPath is the Path to the Folder,yes.

I Guess i see where is my mistake. There is no file to be written, just a folder. My Bad

Upvotes: 0

Views: 3272

Answers (2)

DanTheMan
DanTheMan

Reputation: 3277

Change:

using (StreamWriter filePointer = File.AppendText(folderPath))

to

using (StreamWriter filePointer = File.AppendText(Path.Combine(folderPath, fileName)))

Upvotes: 2

kevintodisco
kevintodisco

Reputation: 5191

Depending on where you're trying to write a file, you may need admin rights in order to do so. I worked on a project where I had to write a file to a subdirectory of the Program Files folder, but ran into the permissions issue.

I found this for a solution. It describes how to edit the manifest file for the project in order to grant access rights for the application on startup.

Upvotes: 1

Related Questions