C# System.IO.WriteAllBytes - 访问路径被拒绝错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10350467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:30:14  来源:igfitidea点击:

System.IO.WriteAllBytes - Access to path denied error

c#winforms

提问by user1097734

Currently developing a C# WinForms application in Visual Studio 2010 .NET 4 on Windows 7.

目前正在 Windows 7 上的 Visual Studio 2010 .NET 4 中开发 C# WinForms 应用程序。

Firstly I am reading a stream of bytes from a file using the File.ReadAllBytes() method. Then when attempting to write the file back, I am getting a access to path denied error when using the WriteAllBytes method.

首先,我使用 File.ReadAllBytes() 方法从文件中读取字节流。然后,当尝试将文件写回时,我在使用 WriteAllBytes 方法时访问路径被拒绝错误。

I have tried passing in literal paths, Environment.SpecialFolder.ApplicationData, Path.GetTempPath(), but all are providing me with the same error.

我试过传入文字路径,Environment.SpecialFolder.ApplicationData,Path.GetTempPath(),但都给我提供了相同的错误。

I have checked permissions on these folders and also attempted to start the program in administrator mode with no luck.

我已经检查了这些文件夹的权限,并尝试以管理员模式启动程序,但没有成功。

回答by Styxxy

Are you sure the file isn't still locked? If you are planning to read + write bytes from a file, you might want to consider using a Stream class (for example the FileStream), the advantage is that you will lock the file and that no other application can access the file in the meantime.

你确定文件没有被锁定吗?如果您打算从文件中读取 + 写入字节,您可能需要考虑使用 Stream 类(例如FileStream),优点是您将锁定文件并且在此期间没有其他应用程序可以访问该文件.

Code example from this topic:

本主题的代码示例:

FileStream fileStream = new FileStream(
  @"c:\words.txt", FileMode.OpenOrCreate, 
  FileAccess.ReadWrite, FileShare.None);

回答by Romil Kumar Jain

In windows7 there are security issues on c:. If you modified the path to D: then no access denied issue will be there.

在 windows7 中 c: 存在安全问题。如果您将路径修改为 D:,则不会出现拒绝访问的问题。

Try following sample code with Path.GetTempPath(), it will execute successfully.

尝试使用 Path.GetTempPath() 执行以下示例代码,它将成功执行。

    static void Main(string[] args)
    {
        string path = Path.GetTempPath();
        byte[] binaryData;
        string text = "romil123456";
        using (MemoryStream memStream = new MemoryStream(Encoding.ASCII.GetBytes(text)))
            {
                binaryData = memStream.ToArray();
            }
            System.IO.File.WriteAllBytes(@path + "\words123.txt"    , binaryData);
        }
    }

Environment.SpecialFolder.ApplicationData provides the folder name, not provides the full path to that folder. so when you use this in path defined to write file, this folder is searched under in local application path.

Environment.SpecialFolder.ApplicationData 提供文件夹名称,而不提供该文件夹的完整路径。因此,当您在定义写入文件的路径中使用它时,会在本地应用程序路径下搜索此文件夹。

回答by VentingOlive

Make sure that you specify the entire path when using File.WriteAllBytes()including file name.

确保在使用File.WriteAllBytes()包含文件名时指定整个路径。

File.WriteAllBytes()cannot write to a general directory, it has to write to a specific file.

File.WriteAllBytes()不能写入一般目录,它必须写入特定文件。

Hope this helps.

希望这可以帮助。