FileStream not letting me create file on C drive

2019-07-20 13:50发布

问题:

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.



标签: c# filestream