-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Егор Желудков
committed
Aug 24, 2024
1 parent
522e6fd
commit 82e5eb0
Showing
13 changed files
with
282 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,33 @@ | ||
<Project> | ||
<PropertyGroup> | ||
<Nullable>Enable</Nullable> | ||
|
||
<Version>8.0.0-preview2</Version> | ||
<PropertyGroup> | ||
<Nullable>Enable</Nullable> | ||
|
||
<Company>Egorozh</Company> | ||
<Product>CrossPlatform File Operations</Product> | ||
<Authors>Egorozh</Authors> | ||
<PackageProjectUrl>https://github.com/egorozh/WindowsOperations</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/egorozh/WindowsOperations</RepositoryUrl> | ||
<PackageTags>Windows, MacOs, Files, FileOperations, File operations</PackageTags> | ||
<IsAotCompatible>true</IsAotCompatible> | ||
|
||
<Version>8.0.0-preview4</Version> | ||
|
||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
<Company>Egorozh</Company> | ||
<Product>CrossPlatform File Operations</Product> | ||
<Authors>Egorozh</Authors> | ||
<PackageProjectUrl>https://github.com/egorozh/FileOperations</PackageProjectUrl> | ||
<RepositoryUrl>https://github.com/egorozh/FileOperations</RepositoryUrl> | ||
<PackageTags>Windows, MacOs, Files, FileOperations, File operations</PackageTags> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</ItemGroup> | ||
<PackageLicenseFile>LICENSE</PackageLicenseFile> | ||
<PackageReadmeFile>README.md</PackageReadmeFile> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath></PackagePath> | ||
</None> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<None Include="..\..\LICENSE"> | ||
<Pack>True</Pack> | ||
<PackagePath /> | ||
</None> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<None Include="..\..\README.md"> | ||
<Pack>True</Pack> | ||
<PackagePath /> | ||
</None> | ||
</ItemGroup> | ||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
namespace FileOperations.Abstractions.Enums; | ||
|
||
/// <summary> | ||
/// Type of notification about adding or removing a logical disk in the system | ||
/// </summary> | ||
public enum DriveDetectType | ||
{ | ||
Unknown, | ||
|
||
Added, | ||
|
||
Removed | ||
} |
6 changes: 2 additions & 4 deletions
6
src/FileOperations.Abstractions/FileOperations.Abstractions.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using FileOperations.Abstractions.Enums; | ||
|
||
namespace FileOperations.Abstractions; | ||
|
||
public interface IDriveDetector | ||
{ | ||
event Action<DriveDetectType, string> DriveChanged; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/FileOperations.Abstractions/IPlatformFoldersProvider.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace FileOperations.Abstractions; | ||
|
||
public interface IPlatformFoldersProvider | ||
{ | ||
/// <summary> | ||
/// <para> | ||
/// Windows: %APPDATA%\ | ||
/// </para> | ||
/// <para> | ||
/// macOS: $HOME/Library/Application Support/ | ||
/// </para> | ||
/// <para> | ||
/// Linux: $HOME/.config/ | ||
/// </para> | ||
/// </summary> | ||
string GetAppDataPath(); | ||
|
||
|
||
string? GetDesktopFolderPath(); | ||
|
||
string? GetDownloadFolderPath(); | ||
|
||
string? GetDocumentsFolderPath(); | ||
|
||
string? GetMoviesFolderPath(); | ||
|
||
string? GetMusicFolderPath(); | ||
|
||
string? GetPicturesFolderPath(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System; | ||
using FileOperations.Abstractions; | ||
using FileOperations.Abstractions.Enums; | ||
|
||
namespace FileOperations.MacOs; | ||
|
||
public class MacOsDriveDetector : IDriveDetector | ||
{ | ||
public event Action<DriveDetectType, string>? DriveChanged; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using System; | ||
using System.IO; | ||
using FileOperations.Abstractions; | ||
|
||
namespace FileOperations.MacOs; | ||
|
||
public class MacOsPlatformFoldersProvider : IPlatformFoldersProvider | ||
{ | ||
public string GetAppDataPath() | ||
{ | ||
string home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); | ||
string appDataPath = Path.Combine(home, "Library", "Application Support"); | ||
|
||
return appDataPath; | ||
} | ||
|
||
public string? GetDesktopFolderPath() => Environment.GetFolderPath(Environment.SpecialFolder.Desktop); | ||
|
||
public string? GetDownloadFolderPath() | ||
{ | ||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), | ||
"Downloads"); | ||
} | ||
|
||
public string? GetDocumentsFolderPath() => Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); | ||
|
||
|
||
public string? GetMoviesFolderPath() | ||
{ | ||
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), | ||
"Movies"); | ||
} | ||
|
||
public string? GetMusicFolderPath() => Environment.GetFolderPath(Environment.SpecialFolder.MyMusic); | ||
|
||
public string? GetPicturesFolderPath() => Environment.GetFolderPath(Environment.SpecialFolder.MyPictures); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,35 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
|
||
<Description>File operations for Windows</Description> | ||
<PackageId>Egorozh.FileOperations.Windows</PackageId> | ||
<RootNamespace>FileOperations.Windows</RootNamespace> | ||
|
||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Compile Update="Resources\SR.Designer.cs"> | ||
<DependentUpon>SR.resx</DependentUpon> | ||
<DesignTime>True</DesignTime> | ||
<AutoGen>True</AutoGen> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Update="Resources\SR.resx"> | ||
<SubType>Designer</SubType> | ||
<LastGenOutput>SR.Designer.cs</LastGenOutput> | ||
<Generator>PublicResXFileCodeGenerator</Generator> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FileOperations.Abstractions\FileOperations.Abstractions.csproj" /> | ||
</ItemGroup> | ||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
|
||
<Description>File operations for Windows</Description> | ||
<PackageId>Egorozh.FileOperations.Windows</PackageId> | ||
<RootNamespace>FileOperations.Windows</RootNamespace> | ||
|
||
</PropertyGroup> | ||
|
||
|
||
<ItemGroup> | ||
<Compile Update="Resources\SR.Designer.cs"> | ||
<DependentUpon>SR.resx</DependentUpon> | ||
<DesignTime>True</DesignTime> | ||
<AutoGen>True</AutoGen> | ||
</Compile> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<EmbeddedResource Update="Resources\SR.resx"> | ||
<SubType>Designer</SubType> | ||
<LastGenOutput>SR.Designer.cs</LastGenOutput> | ||
<Generator>PublicResXFileCodeGenerator</Generator> | ||
</EmbeddedResource> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\FileOperations.Abstractions\FileOperations.Abstractions.csproj" /> | ||
|
||
<PackageReference Include="System.Management" Version="8.0.0" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
using System.Management; | ||
using FileOperations.Abstractions; | ||
using FileOperations.Abstractions.Enums; | ||
|
||
namespace FileOperations.Windows; | ||
|
||
public class WindowsDriveDetector : IDriveDetector | ||
{ | ||
#region Private Fields | ||
|
||
private const string QUERY = "SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2 or EventType = 3"; | ||
|
||
#endregion | ||
|
||
|
||
#region Events | ||
|
||
public event Action<DriveDetectType, string>? DriveChanged; | ||
|
||
#endregion | ||
|
||
|
||
#region Constructor | ||
|
||
public WindowsDriveDetector() | ||
{ | ||
ManagementEventWatcher watcher = new() | ||
{ | ||
Query = new WqlEventQuery(QUERY) | ||
}; | ||
|
||
watcher.EventArrived += WatcherOnEventArrived; | ||
|
||
watcher.Start(); | ||
} | ||
|
||
#endregion | ||
|
||
|
||
#region Private Methods | ||
|
||
private void WatcherOnEventArrived(object sender, EventArrivedEventArgs e) | ||
{ | ||
string? driveName = e.NewEvent.Properties["DriveName"].Value.ToString(); | ||
|
||
bool res = int.TryParse(e.NewEvent.Properties["EventType"].Value.ToString(), out int eventTypeValue); | ||
|
||
if (res && driveName != null) | ||
RaiseDriveChanged(driveName, eventTypeValue); | ||
} | ||
|
||
|
||
private void RaiseDriveChanged(string driveName, int eventType) | ||
{ | ||
DriveDetectType type = eventType switch | ||
{ | ||
2 => DriveDetectType.Added, | ||
3 => DriveDetectType.Removed, | ||
|
||
_ => DriveDetectType.Unknown | ||
}; | ||
|
||
DriveChanged?.Invoke(type, driveName); | ||
} | ||
|
||
#endregion | ||
} |
Oops, something went wrong.