22265

“ 'System.UnauthorizedAccessException' occurred in mscorlib.dll” while trying to create fi

Question:

I'm doing this project where I grab some information and save it to a file. While trying to create that file, I always get an System.UnauthorizedAccesssException error. I've tried adding a manifest file and requiring admin, still doesn't work. I've also released it and ran the program through the \bin folder, and it still didn't work.

Here is my code:

public static void CreateFile(string path, string name) { Debug.WriteLine("Starting to create file..."); string Name = name; string path2 = path; string fullPath = Path.Combine(path2, Name); using (FileStream fs = new FileStream(fullPath, FileMode.Create)) { Debug.WriteLine("File Created"); } }

I also tried going to the Microsoft website and using their method of creating a file, and it still didn't work, I got the same error!

Question is, how I give access to the program in order to write to the C:\ drive?

Answer1:

By default most user accounts lack the necessary access to write directly to the C:\ drive. The exception, UnauthorizedAccessException, indicates that this is the case here.

To make this work you're going to need to either

<ol><li>Pick a more standard location to write the file</li> <li>Give the account under which this program runs access to the C:\ drive. </li> </ol>

Of the two #1 is by far the more preferable option

Recommend

  • Python: Sort a Nested List With Specific Criteria
  • Calling python function with an unknown number of arguments [duplicate]
  • Setting link href from child image src using jQuery
  • How to create file with help of P/Invoke to read boot sector and display .MBR?
  • C++(Serial Communicatio using the ) - How can i find out before hand, how many characters will be re
  • OSX: Syntax for loading a single launchagent for current user
  • How does file creation work in Java
  • Should `System.IO.Path` be concrete?
  • c++ search a vector for element first seen position
  • Is there any purpose for h2-h6 headings in HTML5?
  • how do i write assembly code from c#?
  • nodemcu custom firmware build problems
  • Android Studio Can't Find tools.jar
  • Memory error in python- how to use more memory
  • Convert SQLite database to XML
  • Differences in dis-assembled C code of GCC and Borland?
  • Eclipse CDT error: Unable to compile
  • iOS: Detect app start via notification press
  • Algorithm for a smudge tool?
  • Paperclip, set path outside of rails root folder
  • formatting the colorbar ticklabels with SymLogNorm normalization in matplotlib
  • sending/ receiving email in Java
  • Eraser for UIBezierPath
  • How to delete a row from a dynamic generate table using jquery?
  • Proper folder structure for lots of source files
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • Acquiring multiple attributes from .xml file in c#
  • Suggestions to manage Login/Logout transitions
  • JTable with a ScrollPane misbehaving
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • How can I remove ASP.NET Designer.cs files?
  • unknown Exception android
  • EntityFramework adding new object to nested object collection
  • Append folder name and increment by 1 using batch script
  • Checking variable from a different class in C#
  • failed to connect to specific WiFi in android programmatically
  • java string with new operator and a literal
  • How can I use threading to 'tick' a timer to be accessed by other threads?