当前位置: 首页>>代码示例>>C#>>正文


C# FileInfo.CreateFileResourceInfo方法代码示例

本文整理汇总了C#中System.IO.FileInfo.CreateFileResourceInfo方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.CreateFileResourceInfo方法的具体用法?C# FileInfo.CreateFileResourceInfo怎么用?C# FileInfo.CreateFileResourceInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在System.IO.FileInfo的用法示例。


在下文中一共展示了FileInfo.CreateFileResourceInfo方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Init

    public void Init()
    {
      provider = new LocalFileSystemProvider();
      FileInfo fi = new FileInfo(FileUtil.CreateTempFilePath("txt"));
      var writer = fi.CreateText();
      writer.WriteLine("hello world.");
      writer.Close();

      sourceFile = fi.CreateFileResourceInfo();
      Assert.IsTrue(fi.Exists);

      targetFile = new FileInfo(FileUtil.CreateTempFilePath("txt"));
    }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:13,代码来源:Given_Local_File_When_Accessing_File_Contents.cs

示例2: Creating_Item_From_Existing_File_Should_Include_All_Information

    public void Creating_Item_From_Existing_File_Should_Include_All_Information()
    {
      FileInfo fi = new FileInfo(GetType().Assembly.Location);
      Assert.IsTrue(fi.Exists);

      VirtualFileInfo item = fi.CreateFileResourceInfo();
      Assert.AreEqual(fi.Name, item.Name);
      Assert.AreEqual(fi.FullName, item.FullName);

      //timestamps should match file info
      Assert.AreEqual((DateTimeOffset)fi.CreationTime, item.CreationTime);
      Assert.AreEqual((DateTimeOffset)fi.LastAccessTime, item.LastAccessTime);
      Assert.AreEqual((DateTimeOffset)fi.LastWriteTime, item.LastWriteTime);

      //length should match file     
      Assert.AreNotEqual(0, item.Length);
      Assert.AreEqual(fi.Length, item.Length);
    }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:18,代码来源:Given_Local_Resource_When_Mapping_To_VFS_Item.cs

示例3: Creating_Item_From_Inexisting_File_Should_Provide_Names_And_Default_Values

    public void Creating_Item_From_Inexisting_File_Should_Provide_Names_And_Default_Values()
    {
      FileInfo fi = new FileInfo("test.txt");
      Assert.IsFalse(fi.Exists);

      VirtualFileInfo item = fi.CreateFileResourceInfo();
      Assert.AreEqual("test.txt", item.Name);
      Assert.AreEqual(fi.FullName, item.FullName);

      //timestamps should be null
      Assert.IsNull(item.CreationTime);
      Assert.IsNull(item.LastAccessTime);
      Assert.IsNull(item.LastWriteTime);

      //the item should not be hidden/read-only
      Assert.IsFalse(item.IsHidden);
      Assert.IsFalse(item.IsReadOnly);

      //length should be zero
      Assert.AreEqual(0, item.Length);
    }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:21,代码来源:Given_Local_Resource_When_Mapping_To_VFS_Item.cs

示例4: WriteFile

    /// <summary>
    /// Creates or updates a given file resource in the file system.
    /// </summary>
    /// <param name="parentFolderPath">The qualified path of the parent folder that will
    /// contain the file.</param>
    /// <param name="fileName">The name of the file to be created.</param>
    /// <param name="input">A stream that provides the file's contents.</param>
    /// <param name="overwrite">Whether an existing file should be overwritten
    /// or not. If this parameter is false and the file already exists, a
    /// <see cref="ResourceOverwriteException"/> is thrown.</param>
    /// <exception cref="VirtualResourceNotFoundException">If the parent folder
    /// does not exist.</exception>
    /// <exception cref="ResourceAccessException">In case of invalid or prohibited
    /// resource access.</exception>
    /// <exception cref="ResourceOverwriteException">If a file already exists at the
    /// specified location, and the <paramref name="overwrite"/> flag was not set.</exception>
    /// <exception cref="ArgumentNullException">If any of the parameters is a null reference.</exception>
    public override VirtualFileInfo WriteFile(string parentFolderPath, string fileName, Stream input, bool overwrite)
    {
      if (parentFolderPath == null) throw new ArgumentNullException("parentFolderPath");
      if (fileName == null) throw new ArgumentNullException("fileName");
      if (input == null) throw new ArgumentNullException("input");


      //get the parent and make sure it exists
      string absoluteParentPath;
      var parent = GetFolderInfoInternal(parentFolderPath, true, out absoluteParentPath);

      if(RootDirectory == null && parent.IsRootFolder)
      {
        VfsLog.Debug("Blocked attempt to create a file '{0}' at system root (which is the machine itself - no root directory was set).", fileName);
        throw new ResourceAccessException("Files cannot be created at the system root.");        
      }

      //combine to file path and get virtual file (also makes sure we don't get out of scope)
      string absoluteFilePath = PathUtil.GetAbsolutePath(fileName, new DirectoryInfo(absoluteParentPath));

      FileInfo fi = new FileInfo(absoluteFilePath);
      if (fi.Exists && !overwrite)
      {
        VfsLog.Debug("Blocked attempt to overwrite file '{0}'", fi.FullName);
        string msg = String.Format("The file [{0}] already exists.", fileName);
        throw new ResourceOverwriteException(msg);
      }

      try
      {
        using (Stream writeStream = new FileStream(fi.FullName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
          input.WriteTo(writeStream);
        }
      }
      catch (Exception e)
      {
        //log exception with full path
        string msg = "Could not write write submitted content to file '{0}'.";
        VfsLog.Error(e, msg, fi.FullName);
        //generate exception with relative path
        msg = String.Format(msg, PathUtil.GetRelativePath(fi.FullName, RootDirectory));
        throw new ResourceAccessException(msg, e);
      }

      //return update file info
      var file = fi.CreateFileResourceInfo();

      //adjust and return
      if (UseRelativePaths) file.MakePathsRelativeTo(RootDirectory);
      return file;
    }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:69,代码来源:LocalFileSystemProvider.DataAccess.cs

示例5: Files_Should_Have_Correct_Hidden_And_Read_Only_Flags

    public void Files_Should_Have_Correct_Hidden_And_Read_Only_Flags()
    {
      FileInfo fi = new FileInfo(Path.GetTempFileName());

      File.SetAttributes(fi.FullName, FileAttributes.ReadOnly);
      Assert.IsTrue(fi.CreateFileResourceInfo().IsReadOnly);
      Assert.IsFalse(fi.CreateFileResourceInfo().IsHidden);

      File.SetAttributes(fi.FullName, fi.Attributes | FileAttributes.Hidden);
      Assert.IsTrue(fi.CreateFileResourceInfo().IsHidden);
      Assert.IsTrue(fi.CreateFileResourceInfo().IsReadOnly);

      //reset attribute so it can be deleted
      File.SetAttributes(fi.FullName, FileAttributes.Hidden);
      Assert.IsTrue(fi.CreateFileResourceInfo().IsHidden);
      Assert.IsFalse(fi.CreateFileResourceInfo().IsReadOnly);

      File.Delete(fi.FullName);
    }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:19,代码来源:Given_Local_Resource_When_Mapping_To_VFS_Item.cs

示例6: GetFileInfoInternal

    protected VirtualFileInfo GetFileInfoInternal(string virtualFilePath, bool mustExist, out string absolutePath)
    {
      try
      {
        if (String.IsNullOrEmpty(virtualFilePath))
        {
          VfsLog.Debug("File request without file path received.");
          throw new ResourceAccessException("An empty or null string is not a valid file path");
        }

        //make sure we operate on absolute paths
        absolutePath = PathUtil.GetAbsolutePath(virtualFilePath, RootDirectory);

        if (IsRootPath(absolutePath))
        {
          VfsLog.Debug("Blocked file request with path '{0}' (resolves to root directory).", virtualFilePath);
          throw new ResourceAccessException("Invalid path submitted: " + virtualFilePath);
        }
        
        var fi = new FileInfo(absolutePath);
        VirtualFileInfo fileInfo = fi.CreateFileResourceInfo();

        //convert to relative paths if required (also prevents qualified paths in validation exceptions)
        if (UseRelativePaths) fileInfo.MakePathsRelativeTo(RootDirectory);

        //make sure the user is allowed to access the resource
        ValidateResourceAccess(fileInfo);

        //verify file exists on FS
        if(mustExist) fileInfo.VerifyFileExists(RootDirectory);
        
        return fileInfo;
      }
      catch(VfsException)
      {
        //just bubble internal exceptions
        throw;
      }
      catch (Exception e)
      {
        VfsLog.Debug(e, "Could not create file based on path '{0}' with root '{1}'", virtualFilePath,
                           RootDirectory);
        throw new ResourceAccessException("Invalid path submitted: " + virtualFilePath);
      }
    }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:45,代码来源:LocalFileSystemProvider.cs

示例7: ResolveFileResourcePath2

        public  FileItem ResolveFileResourcePath2(string submittedFilePath, FileSystemTask context) {
            if (String.IsNullOrEmpty(submittedFilePath)) {
                throw new InvalidResourcePathException("An empty or null string is not a valid file path");
            }

            //make sure we operate on absolute paths
            var absolutePath = PathUtil.GetAbsolutePath(submittedFilePath, RootDirectory);

            if (IsRootPath(absolutePath)) {
                throw new InvalidResourcePathException("Invalid path submitted: " + submittedFilePath);
            }

            var localFile = new FileInfo(absolutePath);
            VirtualFileInfo virtualFile = localFile.CreateFileResourceInfo();

            var item = new FileItem(localFile, virtualFile);

            //convert to relative paths if required (also prevents qualified paths in validation exceptions)
            if (UseRelativePaths) item.MakePathsRelativeTo(RootDirectory);

            ValidateFileRequestAccess(item, submittedFilePath, context);
            return item;
        }
开发者ID:RainsSoft,项目名称:UJad-AI-VFS,代码行数:23,代码来源:LocalFileSystemProvider2.cs


注:本文中的System.IO.FileInfo.CreateFileResourceInfo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。