我正在尝试使用FileStream创建一个文件,但为什么会出现异常?

时间:2013-06-06 13:00:54

标签: c# winforms

这是代码:

public OptionsFile(string settings)
    {

        if (File.Exists(settings))
        {

        }
        else
        {
        FileStream fs=File.Create(settings);

        fs.Close();
        }

        path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
        Options_File = settings;

    }

在线:FileStream fs = File.Create(settings);

由于某种原因,它无法创建文件。 在Form1中,我在构造函数中做了:

Options_DB.Get_Video_File();

现在在Options_DB类中使用它正在执行的断点:

namespace test
{

    static class Options_DB
    {
        static string settings_dir;
        static string settings_file;
        static OptionsFile setting_file;
        static string path_settings;
        static string path_exe;
        static string outPutVideoFileDirectory;

        static Options_DB()
        {
            // ---  O P E N N I N G   S E T T I N G   F I L E   ;
            path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            path_settings = Path.GetDirectoryName(Application.LocalUserAppDataPath);
            settings_file = "\\settings.txt";
            settings_dir = path_settings + @"\settings";
            setting_file = new OptionsFile(settings_dir + settings_file);

然后在OptionsFile中它正在做:

public OptionsFile(string settings)
    {

        if (File.Exists(settings))
        {

        }
        else
        {
        FileStream fs=File.Create(settings);

        fs.Close();
        }

        path_exe = Path.GetDirectoryName(Application.LocalUserAppDataPath);
        Options_File = settings;

    }

我在OptionsFile中使用了断点,并看到设置包含带有settings.txt文件的用户目录,但由于某种原因,它无法创建该行:

FileStream fs=File.Create(settings);

它停在Form1的行上:Options_DB.Get_Video_File();

'ScreenVideoRecorder.Options_DB'的类型初始化程序引发了异常

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: 

由于某些原因,在遵循断点步骤后,它无法创建FileStream。 我想这行有问题:FileStream fs = File.Create(settings);

尝试创建时设置为contils:

  

“C:\ Users \用户名\ AppData \ Local \ project dir \ project   DIR \设置\ SETTINGS.TXT“

但它永远不会创造它。

3 个答案:

答案 0 :(得分:2)

例外情况很明显:路径“C:\ Users \ bout0_000 \ AppData \ Local \ ScreenVideoRecorder \ ScreenVideoRecorder \ settings \”不存在。 请注意,此路径不同于:“C:\ Users \ user name \ AppData \ Local \ project dir \ project \ nir \ settings \”

答案 1 :(得分:2)

如果您阅读内部异常,则可以看到问题是settings目录不存在。您可以通过调用Directory.CreateDirectory来解决此问题,Path.Combine创建指定路径中尚不存在的所有目录和子目录。 (如果目录已存在,则不执行任何操作。)

我还建议您使用{{3}}来代替手动路径连接。

settings_dir = Path.Combine(path_settings, "settings");
Directory.CreateDirectory(settings_dir);  // create directory

settings_file = "settings.txt";
setting_file = new OptionsFile(Path.Combine(settings_dir, settings_file));

答案 2 :(得分:0)

例外情况:

无法找到路径'C:\ Users \ bout0_000 \ AppData \ Local \ ScreenVideoRecorder \ ScreenVideoRecorder \ settings \ settings.txt'

的一部分

因此,您需要检查文件或文件夹是否存在,或者您是否有权写入该特定文件夹。