gpt4 book ai didi

C#获取文件大小

转载 作者:行者123 更新时间:2023-12-02 14:56:51 32 4
gpt4 key购买 nike

问题: 我想获取文件大小,但文件没有扩展名示例 testfile.txt 是普通文件,这里是示例(不带引号) "testfile”,我还有没有扩展名但名称中有空格的文件示例(不带引号)“我的测试文件”,使用File.Exist它读取为文件夹,我使用的当前函数用于文件大小

    using System.IO;
static long GetFileSize(string FilePath)
{
if(File.Exists(FilePath))
{
return new FileInfo(FilePath).Length;
}
return 0;
}

当前使用情况:

GetFileSize("C:\\Users\\TestUser\\Appdata\\Local\\Data Manager\\Super User\\User data controller");

它抛出错误:System.IO.IOException: 'The directory name is invalid

文件不违反无效路径字符规则,路径仅包含数字、字母和/或空格。

应用程序以管理员身份启动,Visual Studio 也以管理员身份启动

完整错误:

    System.IO.IOException
HResult=0x8007010B
Message=The directory name is invalid.

Source=mscorlib
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)

at System.IO.FileSystemEnumerableIterator`1.CommonInit()

at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)

at System.IO.Directory.GetFiles(String path, String searchPattern, SearchOption searchOption)

at test.SuperUserControlEngine.GetFileSize(String FilePath)
at test.systems.superuser.HoloPart.Control1.ControllerSize()
at test.systems.superuser.HoloPart.Control1.GetAllControllersSize()
at test.SuperUserControlEngine.ScanHolos()
at test.ManagerEngine.Button_Click(Object sender, RoutedEventArgs e)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs e)
at System.Windows.Controls.Primitives.ButtonBase.OnClick()
at System.Windows.Controls.Button.OnClick()
at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
at System.Windows.UIElement.RaiseTrustedEvent(RoutedEventArgs args)
at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
at System.Windows.Input.InputManager.ProcessStagingArea()
at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, WindowMessage msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs)
at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window)
at System.Windows.Application.Run(Window window)
at test.App.Main()

图片: test image

解决方案: 如果有人能指出我在代码中做错了什么并帮助解决它。

谢谢。

最佳答案

首先检查您对该文件夹的权限。如果您没有该文件夹的权限,则文件操作将不起作用:

using System.IO;
static long GetFileSize(string FilePath)
{
//if you don't have permission to the folder, Directory.Exists will return False
if(!Directory.Exists(Path.GetDirectoryName(FilePath))
{
//if you land here, it means you don't have permission to the folder
Debug.Write("Permission denied");
return -1;
}
else if(File.Exists(FilePath))
{
return new FileInfo(FilePath).Length;
}
return 0;
}

阅读更多:Read Permissions to a directory in C#

关于C#获取文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52500681/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com