Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add System.Io.Directory class #17

Merged
merged 13 commits into from
Apr 22, 2021
152 changes: 152 additions & 0 deletions System.IO.FileSystem/Directory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using System.Runtime.CompilerServices;

namespace System.IO
{
/// <summary>
/// Class for managing directories
/// </summary>
public static class Directory
{
#region Static Methods

/// <summary>
/// Determines a list of available logical drives.
/// </summary>
/// <returns>String[] of available drives, ex. "D:\\"</returns>
public static string[] GetLogicalDrives()
{
return GetLogicalDrivesNative();
}

/// <summary>
/// Creates directory with the provided path.
/// </summary>
/// <param name="path">Path and name of the directory to create.</param>
/// <exception cref="IOException">Path for creating the folder doesn't exist. This method does not create directories recursively.</exception>
public static void CreateDirectory(string path)
{
CreateNative(path);
}
/// <summary>
/// Deletes directory from storage.
/// </summary>
/// <param name="path">Path to the directory to be removed.</param>
/// <param name="recursive">Parameter to be implemented.</param>
/// <exception cref="IOException">This method will throw DirectoryNotEmpty exception if folder is not empty.</exception>
public static void Delete(string path, bool recursive = false)
{
DeleteNative(path);
}

/// <summary>
/// Determines whether the specified directory exists.
/// </summary>
/// <param name="path">Path to the directory.</param>
/// <returns>True if directory under given path exists, otherwise it returns false.</returns>
/// <exception cref="ArgumentNullException">Path must be defined.</exception>
/// <exception cref="IOException">Invalid drive or path to the parent folder doesn't exist.</exception>
public static bool Exists(string path)
{
return ExistsNative(path);
}

/// <summary>
/// Moves directory from specified path to a new location.
/// </summary>
/// <param name="sourcePath">Name of directory to move. Absolute path.</param>
/// <param name="destinationPath">New path and name for the directory.</param>
/// <exception cref="Exception">Source directory not existing or destination folder already existing.</exception>
public static void Move(string sourcePath, string destinationPath)
{
MoveNative(sourcePath, destinationPath);
}

/// <summary>
/// List files from the specified folder.
/// </summary>
/// <param name="path">Path to the directory to list files from.</param>
/// <returns>
/// When this method completes successfully, it returns a array of paths of the files in the given folder.
/// </returns>
/// <exception cref="IOException"> Logical drive or a directory under given path does not exist. </exception>
public static string[] GetFiles(string path)
{
return GetFilesNative(path);
}

/// <summary>
/// List directories from the specified folder.
/// </summary>
/// <param name="path"></param>
/// <returns>
/// When this method completes successfully, it returns an array of absolute paths to the subfolders in the specified directory.
/// </returns>
/// <exception cref="IOException"> Logical drive or a directory under given path does not exist. </exception>
public static string[] GetDirectories(string path)
{
return GetDirectoriesNative(path);
}

/// <summary>
/// Determines the time of the last write/modification to directory under given path.
/// </summary>
/// <param name="path"></param>
/// <returns>Time of the last write/modification.</returns>
/// <exception cref="IOException"> Logical drive or a directory under given path does not exist. </exception>
public static DateTime GetLastWriteTime(string path)
{
return GetLastWriteTimeNative(path);
}

#endregion

#region Stubs (Native Calls)

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern bool ExistsNative(string path);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void MoveNative(string pathSrc, string pathDest);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void DeleteNative(string path);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void CreateNative(string path);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern string[] GetFilesNative(string path);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern string[] GetDirectoriesNative(string path);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern string[] GetLogicalDrivesNative();

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern DateTime GetLastWriteTimeNative(string path);

#endregion
}
}
18 changes: 17 additions & 1 deletion System.IO.FileSystem/File.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public static bool Exists(string path)
/// </summary>
/// <param name="sourceFileName">The name of the file to move. Absolute path.</param>
/// <param name="destFileName">The new path and name for the file.</param>
/// /// <exception cref="Exception">Source File not existing or Destination File already existing.</exception>
/// <exception cref="Exception">Source File not existing or Destination File already existing.</exception>
public static void Move(
string sourceFileName,
string destFileName)
Expand Down Expand Up @@ -244,6 +244,17 @@ public static FileAttributes GetAttributes(string path)
public static void SetAttributes(string path, FileAttributes fileAttributes)
{
SetAttributesNative(path, (byte)fileAttributes);
}

/// <summary>
/// Determines the time of the last write/modification to file under given path.
/// </summary>
/// <param name="path"></param>
/// <returns>Time of the last write/modification.</returns>
/// <exception cref="IOException"> Logical drive or a file under given path does not exist. </exception>
public static DateTime GetLastWriteTime(string path)
{
return GetLastWriteTimeNative(path);
}

#endregion
Expand Down Expand Up @@ -275,6 +286,11 @@ public static void SetAttributes(string path, FileAttributes fileAttributes)
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern void SetAttributesNative(string path, byte attributes);

[Diagnostics.DebuggerStepThrough]
[Diagnostics.DebuggerHidden]
[MethodImpl(MethodImplOptions.InternalCall)]
private static extern DateTime GetLastWriteTimeNative(string path);

#endregion
}
}
169 changes: 85 additions & 84 deletions System.IO.FileSystem/System.IO.FileSystem.nfproj
Original file line number Diff line number Diff line change
@@ -1,85 +1,86 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>94c34547-1e2b-4967-8d1f-f3359460bb6d</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>System.IO.FileSystem</RootNamespace>
<AssemblyName>System.IO.FileSystem</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<NF_IsCoreLibrary>True</NF_IsCoreLibrary>
<DocumentationFile>bin\$(Configuration)\System.IO.FileSystem.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<DelaySign>false</DelaySign>
</PropertyGroup>
<ItemGroup>
<NFMDP_PE_ExcludeClassByName Include="ThisAssembly">
<InProject>false</InProject>
</NFMDP_PE_ExcludeClassByName>
</ItemGroup>
<PropertyGroup Label="nanoFramework">
<NF_GenerateStubsDirectory>bin\$(Configuration)\Stubs</NF_GenerateStubsDirectory>
<NF_GenerateSkeletonProjectName>nf_sys_io_filesystem</NF_GenerateSkeletonProjectName>
<Name>System.IO.FileSystem</Name>
</PropertyGroup>
<ItemGroup>
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.CoreLibrary.1.10.3-preview.7\lib\mscorlib.dll">
<InProject>false</InProject>
</NFMDP_PE_LoadHints>
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="File.cs" />
<Compile Include="FileAccess.cs" />
<Compile Include="FileAttributes.cs" />
<Compile Include="FileMode.cs" />
<Compile Include="FileShare.cs" />
<Compile Include="FileStream.cs" />
<Compile Include="MemoryStream.cs" />
<Compile Include="Path.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<NFMDP_PE_ExcludeClassByName Include="ThisAssembly">
<InProject>false</InProject>
</NFMDP_PE_ExcludeClassByName>
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib, Version=1.10.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.10.3-preview.7\lib\mscorlib.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<Import Project="..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets'))" />
</Target>
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup Label="Globals">
<NanoFrameworkProjectSystemPath>$(MSBuildToolsPath)..\..\..\nanoFramework\v1.0\</NanoFrameworkProjectSystemPath>
</PropertyGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.Default.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectTypeGuids>{11A8DD76-328B-46DF-9F39-F559912D0360};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<ProjectGuid>94c34547-1e2b-4967-8d1f-f3359460bb6d</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<FileAlignment>512</FileAlignment>
<RootNamespace>System.IO.FileSystem</RootNamespace>
<AssemblyName>System.IO.FileSystem</AssemblyName>
<TargetFrameworkVersion>v1.0</TargetFrameworkVersion>
<NF_IsCoreLibrary>True</NF_IsCoreLibrary>
<DocumentationFile>bin\$(Configuration)\System.IO.FileSystem.xml</DocumentationFile>
</PropertyGroup>
<PropertyGroup>
<SignAssembly>true</SignAssembly>
</PropertyGroup>
<PropertyGroup>
<AssemblyOriginatorKeyFile>key.snk</AssemblyOriginatorKeyFile>
</PropertyGroup>
<PropertyGroup>
<DelaySign>false</DelaySign>
</PropertyGroup>
<ItemGroup>
<NFMDP_PE_ExcludeClassByName Include="ThisAssembly">
<InProject>false</InProject>
</NFMDP_PE_ExcludeClassByName>
</ItemGroup>
<PropertyGroup Label="nanoFramework">
<NF_GenerateStubsDirectory>bin\$(Configuration)\Stubs</NF_GenerateStubsDirectory>
<NF_GenerateSkeletonProjectName>nf_sys_io_filesystem</NF_GenerateSkeletonProjectName>
<Name>System.IO.FileSystem</Name>
</PropertyGroup>
<ItemGroup>
<NFMDP_PE_LoadHints Include="..\packages\nanoFramework.CoreLibrary.1.10.3-preview.7\lib\mscorlib.dll">
<InProject>false</InProject>
</NFMDP_PE_LoadHints>
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="Directory.cs" />
<Compile Include="File.cs" />
<Compile Include="FileAccess.cs" />
<Compile Include="FileAttributes.cs" />
<Compile Include="FileMode.cs" />
<Compile Include="FileShare.cs" />
<Compile Include="FileStream.cs" />
<Compile Include="MemoryStream.cs" />
<Compile Include="Path.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
<NFMDP_PE_ExcludeClassByName Include="ThisAssembly">
<InProject>false</InProject>
</NFMDP_PE_ExcludeClassByName>
</ItemGroup>
<ItemGroup>
<Reference Include="mscorlib, Version=1.10.3.0, Culture=neutral, PublicKeyToken=c07d481e9758c731">
<HintPath>..\packages\nanoFramework.CoreLibrary.1.10.3-preview.7\lib\mscorlib.dll</HintPath>
<Private>True</Private>
<SpecificVersion>True</SpecificVersion>
</Reference>
</ItemGroup>
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<Import Project="..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.3.37\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>