locked
FileStream "access is denied" error all of a sudden RRS feed

  • Question

  • So I built this little console application that simply streams files from one place on your local disk to somewhere else. the program worked great in debug so I decided to publish it. After I published it, I started getting an "access is denied" error popping up on the second line of code:

    FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read);
    FileStream destinationStream = new FileStream(destinationPath, FileMode.CreateNew, FileAccess.Write);

    Weird, it ran perfectly fine up until I decided to publish it. Now both the debug and published version will not work because of this error.

    So I checked the directory that I am writing to, the directory is read-only. I then look around my pc and find out that every folder is read-only. Should every folder on my pc be read-only? it doesn't seem right but I never noticed before.

    Does anyone have any clue whats going on?

    Thanks

    Monday, June 11, 2012 11:26 PM

Answers

  • I tested publishing (in Visual Studio 2008) by setting the "Publishing Folder Location" to C:\temp\TestPublish\ and clicked "Publish Now". Everything was left to default except that under Options\Manifests, I checked "Create desktop shortcut".

    I installed on a different Windows 7 system and everything worked as expected.

    Here is my entire source:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace TestPublish
    {
      class Program
      {
        static void Main(string[] args)
        {
          try
          {
            string sourcePath = @"C:\temp\streets.txt";
            string destPath = @"C:\test\streets.txt";
    
            FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destStream = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
    
            using (StreamReader sr = new StreamReader(sourceStream))
            {
              using (StreamWriter sw = new StreamWriter(destStream))
              {
                string sourceContents = sr.ReadToEnd();
                sw.Write(sourceContents);
              }
            }
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
          }
        }
      }
    }

    Note that for destStream, I set the FileMode to "OpenOrCreate". CreateNew will throw an exception if the file exists. The FileShare property is only used to allow other applications to have access to the file at the same time you have it opened. I think the default is to deny any access to other processes.

    If you put the using block around the file streams, they will be closed automatically. If you forget to close file streams, very bad thing will happen, and you won't notice it right away. (Similar to C++ bugs.)

    Hope this helps.


    Dan Randolph

    • Marked as answer by Magic Jesus Thursday, June 14, 2012 11:12 PM
    Thursday, June 14, 2012 4:40 PM
  • first off, thank you very much for the code sample! So its turns out that it was the path I was using. I was setting destinationPath to a directory path instead of a file path.

    Before:

    sourcePath = @"C:\Users\Ian\Documents\TEST\COVER.pdf";
    destinationPath = @"C:\Users\Ian\Desktop\TEST";

    After:

    sourcePath = @"C:\Users\Ian\Documents\TEST\COVER.pdf";
    destinationPath = @"C:\Users\Ian\Desktop\TEST\COVER.pdf";

    So it now seems to work perfectly fine. This is still confusing me though, I remember seeing a directory path being passed in during some tutorial I read. and it friggin WORKED before I published it.

    Black magic or something ha

    Thanks a lot man!


    • Marked as answer by Lisa Zhu Saturday, June 16, 2012 7:05 AM
    Thursday, June 14, 2012 11:12 PM

All replies

  • It's normal, I seen the same thing on many PC's. 

    Note: It's not every folder on your PC being read-only btw, what you are seeing is a half checked state which does NOT mean they are all classified as read-only. (It is a square filled in, and not a symbolic checkmark if i'm correct.)

    Some locations on a Windows filesystem do not allow you the required permissions needed to operate without elevated set's of permisisons.

    You may want to look at these:

    http://msdn.microsoft.com/en-us/library/system.io.fileshare.aspx
    http://msdn.microsoft.com/en-us/library/system.io.fileshare(v=vs.71).aspx
    http://msdn.microsoft.com/en-us/library/y973b725.aspx
    Cheers
    ~Ace


    If a post helps you in any way or solves your particular issue, please remember to use the Propose As Answer option or Vote As Helpful
    Visit the Forum: TechLifeForum

    Tuesday, June 12, 2012 12:42 AM
  • ahhh ok, yeah it is just a squared in box. You've just brightened my day :)

    So I checked out the links and changed my code to this:

    FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read);
    FileStream destinationStream = new FileStream(destinationPath, FileMode.CreateNew, FileAccess.Write, FileShare.Write);

    Still no luck, The fileshare enumeration seems to be regarding other applications still being able to work with the file at the same time as the streaming. Something I should have had in there, but seems unrelated to permissions. I also found the following in the description for FileShare.Write:

    "However, if this flag is specified additional permissions might still be needed to access the file."

    so I believe there is more to it. I looked into FileIOPermissions briefly but I couldn't seem to get anything going there either. Perhaps I should be pursuing that route further.

    Wednesday, June 13, 2012 12:58 AM
  • How did you publish your application?

    Is your sourcePath set to a full path or a relative path?


    Dan Randolph

    Wednesday, June 13, 2012 2:00 AM
  • I published by clicking "Build"->"Publish whatever" then just clicking finish. It was the first thing I ever published so I may have missed something.

    sourcePath and destinationPath are a full paths (starts at C:\)

    The totally weird thing is that it worked fine in debug right up until I published it.

    Wednesday, June 13, 2012 10:39 PM
  • I tested publishing (in Visual Studio 2008) by setting the "Publishing Folder Location" to C:\temp\TestPublish\ and clicked "Publish Now". Everything was left to default except that under Options\Manifests, I checked "Create desktop shortcut".

    I installed on a different Windows 7 system and everything worked as expected.

    Here is my entire source:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace TestPublish
    {
      class Program
      {
        static void Main(string[] args)
        {
          try
          {
            string sourcePath = @"C:\temp\streets.txt";
            string destPath = @"C:\test\streets.txt";
    
            FileStream sourceStream = new FileStream(sourcePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            FileStream destStream = new FileStream(destPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
    
            using (StreamReader sr = new StreamReader(sourceStream))
            {
              using (StreamWriter sw = new StreamWriter(destStream))
              {
                string sourceContents = sr.ReadToEnd();
                sw.Write(sourceContents);
              }
            }
          }
          catch (Exception ex)
          {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.StackTrace);
          }
        }
      }
    }

    Note that for destStream, I set the FileMode to "OpenOrCreate". CreateNew will throw an exception if the file exists. The FileShare property is only used to allow other applications to have access to the file at the same time you have it opened. I think the default is to deny any access to other processes.

    If you put the using block around the file streams, they will be closed automatically. If you forget to close file streams, very bad thing will happen, and you won't notice it right away. (Similar to C++ bugs.)

    Hope this helps.


    Dan Randolph

    • Marked as answer by Magic Jesus Thursday, June 14, 2012 11:12 PM
    Thursday, June 14, 2012 4:40 PM
  • first off, thank you very much for the code sample! So its turns out that it was the path I was using. I was setting destinationPath to a directory path instead of a file path.

    Before:

    sourcePath = @"C:\Users\Ian\Documents\TEST\COVER.pdf";
    destinationPath = @"C:\Users\Ian\Desktop\TEST";

    After:

    sourcePath = @"C:\Users\Ian\Documents\TEST\COVER.pdf";
    destinationPath = @"C:\Users\Ian\Desktop\TEST\COVER.pdf";

    So it now seems to work perfectly fine. This is still confusing me though, I remember seeing a directory path being passed in during some tutorial I read. and it friggin WORKED before I published it.

    Black magic or something ha

    Thanks a lot man!


    • Marked as answer by Lisa Zhu Saturday, June 16, 2012 7:05 AM
    Thursday, June 14, 2012 11:12 PM
  • Good work !

    And thanks for your sharing. This is helpful for other developers in the forum who have interests in this problem.

    Have a nice day!


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us


    • Edited by Lisa Zhu Friday, June 15, 2012 2:13 AM
    Friday, June 15, 2012 2:13 AM