Xamarin Forms无法使用System.IO.File时,如何获取视频大小及解决AWS分片上传长度问题?
Great question! You’re absolutely right that System.IO.File and FileInfo aren’t fully cross-platform compatible in Xamarin.Forms—file system access works differently across iOS, Android, and UWP, which is why those APIs can cause issues. Let’s break down how to get your video file size and adapt that AWS multipart upload code properly:
The best approach here is to use Dependency Service, Xamarin.Forms’ built-in way to handle platform-specific logic. This lets you write native code for each platform to fetch the file size, then call it from your shared project.
Step 1: Define a Shared Interface
In your shared Xamarin.Forms project, create an interface to standardize the file size method:
public interface IFileService { long GetFileSize(string filePath); }
Step 2: Implement Platform-Specific Services
You’ll need to write a concrete implementation for each platform you support:
Android Implementation
Add this class to your Android project (don’t forget the assembly attribute to register it with Dependency Service):
using System.IO; using Xamarin.Forms; [assembly: Dependency(typeof(AndroidFileService))] namespace YourAppNamespace.Droid { public class AndroidFileService : IFileService { public long GetFileSize(string filePath) { return File.Exists(filePath) ? new FileInfo(filePath).Length : 0; } } }
iOS Implementation
Add this class to your iOS project:
using System.IO; using Xamarin.Forms; [assembly: Dependency(typeof(IOSFileService))] namespace YourAppNamespace.iOS { public class IOSFileService : IFileService { public long GetFileSize(string filePath) { return File.Exists(filePath) ? new FileInfo(filePath).Length : 0; } } }
UWP Implementation
Add this class to your UWP project:
using System.IO; using Xamarin.Forms; [assembly: Dependency(typeof(UWPFileService))] namespace YourAppNamespace.UWP { public class UWPFileService : IFileService { public long GetFileSize(string filePath) { return File.Exists(filePath) ? new FileInfo(filePath).Length : 0; } } }
Step 3: Call the Service from Your Shared Code
Now you can replace the problematic FileInfo line with this cross-platform compatible code:
// Get the platform-specific file service var fileService = DependencyService.Get<IFileService>(); // Fetch the file size long contentLength = fileService.GetFileSize(filePath);
Bonus: If You’re Using MediaPicker
If you’re selecting the video via Xamarin.Essentials.MediaPicker, you can skip the dependency service entirely—FileResult has a built-in FileSize property:
var pickedVideo = await MediaPicker.PickVideoAsync(); if (pickedVideo != null) { long contentLength = pickedVideo.FileSize; }
Once you have contentLength from either method above, you can plug it directly into your AWS multipart upload code. Just replace the line:
// Old code that doesn't work // long contentLength = new FileInfo(filePath).Length;
With the code we just wrote. The rest of the AWS upload logic should work exactly as intended—you’re just swapping out how you retrieve the file size.
A quick note: Make sure your file paths are platform-correct (e.g., using Environment.GetFolderPath or platform-specific storage directories) to avoid "file not found" errors.
内容的提问来源于stack exchange,提问作者Nithin Thampi

