FileStream not letting me create file on C drive

霸气de小男生 提交于 2019-12-11 02:12:12

问题


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();
    }

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.


回答1:


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.

Hope it helps.




回答2:


Right Click on exe => Run as Administrator => Proceed => Check now. It should work now.



来源:https://stackoverflow.com/questions/14154151/filestream-not-letting-me-create-file-on-c-drive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!