FileStream不允许我在C驱动器上创建文件 [英] FileStream not letting me create file on C drive

查看:69
本文介绍了FileStream不允许我在C驱动器上创建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FileStream创建具有给定Length的文件.OpenFileDialog打开文件,并使用FolderBrowserDialog获取位置.现在的问题是,当我在D:\或E:\驱动器上选择位置"时,它会成功创建文件.但是,当我选择C:\驱动器时,它会给出一个ExceptionUnauthorizedAccessException was unhandled.
C:\file.mp4 is denied. 当我选择桌面作为目的地时,它不提供任何Exception并且不创建文件.我正在使用此代码

I am using FileStream to create a file with givenLength.OpenFileDialog to open a file and FolderBrowserDialog to get the Location. Now the problem is when I choose Location on D:\ or E:\ drive it successfully creates the file. But when I choose C:\ drive it gives an Exception like UnauthorizedAccessException was unhandled.
C:\file.mp4 is denied. When I choose desktop as destination it gives no Exception and Does not create the file. I'm using this code

    private void createFile()
    {
        long size = fileInfo.Length;
        string name = file.FileName.Substring(file.FileName.LastIndexOf('\\') + 1, (file.FileName.Length - (file.FileName.LastIndexOf('\\') + 1)));
        string filename = "" + location.SelectedPath + name;

        FileStream outFile = new FileStream(filename, FileMode.Create);
        outFile.SetLength(size);
        outFile.Close();
    }

我的UAC被禁用,并且我也是管理员用户.我使用的是Windows 8专业版.任何人都可以解释解决方案吗?预先感谢.

My UAC is disabled and Also I am an Administrator user.I'm using windows 8 pro. Can anybody explain what can be the solution ? Thanks in advance.

推荐答案

权限问题.如果是Vista/Windows 7/8,则将C:\驱动器视为系统,并且需要在您的进程中具有提升的特权才能直接在其下创建文件.尝试以管理员身份运行您的进程或Visual Studio,它应该可以工作.

Permission issue. In case of Vista/Windows 7/8, C:\ drive is considered as system, and you'll need elevated privileges for your process in order to create files directly under it. Try running your process or Visual Studio as Administrator, and it should work.

希望有帮助.

这篇关于FileStream不允许我在C驱动器上创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆