Share via


Why on FileStream File.Create i'm getting an exception and it's not creating the directory and the file ?

Question

Thursday, June 6, 2013 1:44 PM

This is the code :

FileStream fs=File.Create(settings);
fs.Close();

settings contain : 

"C:\\Users\\bout0_000\\AppData\\Local\\ScreenVideoRecorder\\ScreenVideoRecorder\\settings\\settings.txt"

But instead of creating it i'm getting an exception on the FileStream line :

The type initializer for 'ScreenVideoRecorder.Options_DB' threw an exception

The full exception :

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'ScreenVideoRecorder.Options_DB' threw an exception.
  Source=ScreenVideoRecorder
  TypeName=ScreenVideoRecorder.Options_DB
  StackTrace:
       at ScreenVideoRecorder.Options_DB.Get_Video_File()
       at ScreenVideoRecorder.Form1..ctor() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Form1.cs:line 54
       at ScreenVideoRecorder.Program.Main() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Program.cs:line 18
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.IO.DirectoryNotFoundException
       HResult=-2147024893
       Message=Could not find a part of the path 'C:\Users\bout0_000\AppData\Local\ScreenVideoRecorder\ScreenVideoRecorder\settings\settings.txt'.
       Source=mscorlib
       StackTrace:
            at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
            at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
            at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
            at System.IO.File.Create(String path)
            at DannyGeneral.OptionsFile..ctor(String settings) in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\OptionsFile.cs:line 73
            at ScreenVideoRecorder.Options_DB..cctor() in d:\C-Sharp\ScreenVideoRecorder\ScreenVideoRecorderWorkingVersion\Options_DB.cs:line 29
       InnerException: 

I understand that the exception say that the directory+file is not exist. The idea is that the FileStream will create it but it's not. And i want to use this FileStream so what's wrong ?

All replies (3)

Thursday, June 6, 2013 1:47 PM âś…Answered

The message inside the stacktrace says it:

Could not find a part of the path 'C:\Users\bout0_000\AppData\Local\ScreenVideoRecorder\ScreenVideoRecorder\settings\settings.txt'.

You need to create the directory path first


Thursday, June 6, 2013 2:00 PM

The message inside the stacktrace says it:

Could not find a part of the path 'C:\Users\bout0_000\AppData\Local\ScreenVideoRecorder\ScreenVideoRecorder\settings\settings.txt'.

You need to create the directory path first

I understand that so what is the line : 

FileStream fs=File.Create(settings);

Do ?

It's creating only the file without the directory ?


Thursday, June 6, 2013 2:14 PM

Hi,

Wrong assumption. The purpose of filestream is just to create/read files (it could be counter productive as if for some reason your path is wrong it could just end up with using this wrong location being used without noticing anything).

You can still "opt-in" for this by using explicitely http://msdn.microsoft.com/en-us/library/54a0at6s.aspx to create a Directory and all its subdirectories (but you'll have to exclude the file name from this path).

Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".