System.Exception(不支持给定格式的路径)

本文指导如何在C#中正确处理文件路径,避免因编码格式导致的错误,重点讲解了从文件根目录获取路径并演示了正确打开文件的方法,适合开发者解决实际问题。

c#中,读取文件夹路径下的文件

调用以下方法时,不支持给定格式的路径

FileStream fs = new FileStream(excelPath, FileMode.Open)

错误原因:编码格式不同(90%可能)

错误方式:右键文件,在弹出的属性列选择的这里的路径

正确方式:打开文件根目录,在目录文件夹下复制路径即可

 

 

using NauraLibrary; using NauraLibrary.Logging; using System; using System.IO; using System.Runtime.InteropServices; using System.Windows; namespace DriverManagementTool { /// <summary> /// App.xaml 的交互逻辑 /// </summary> public partial class App : Application { //强制释放进程 [DllImport("kernel32.dll", EntryPoint = "SetProcessWorkingSetSize")] public static extern int SetProcessWorkingSetSize(IntPtr process, int minSize, int maxSize); public App() { StartLogging(); this.DispatcherUnhandledException += App_DispatcherUnhandledException; } private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { Log.WriteExceptionCatch("Error", e.Exception); //MessageBox.Show(string.Format("{0}", e.Exception.Message), "Fatal error", MessageBoxButton.OK); e.Handled = true; } private static System.Threading.Mutex mutex; protected override void OnStartup(StartupEventArgs e) { mutex = new System.Threading.Mutex(true, "OnlyRun"); if (mutex.WaitOne(0, false)) { base.OnStartup(e); } else { MessageBox.Show("程序已经在运行!", "提示"); this.Shutdown(); } } public static void FlushMemory() { try { GC.Collect(); GC.WaitForPendingFinalizers(); if (Environment.OSVersion.Platform == PlatformID.Win32NT) { SetProcessWorkingSetSize(System.Diagnostics.Process.GetCurrentProcess().Handle, -1, -1); } } catch (Exception ex) { Log.WriteExceptionCatch("FlushMemory", ex); } } protected static void StartLogging() { try { string logPath = NaAppSettingsReader.GetValue("LogFilePath", "./"); LogCategory fileLogCategories = NaAppSettingsReader.GetValue("FileLogSinkCategories", LogCategory.None); if (fileLogCategories != LogCategory.None) { FileLogSink localFileLogSink; string fileLogSinkDateSubDirectory = NaAppSettingsReader.GetValue("FileLogSinkDateSubDirectory", ""); if (string.IsNullOrEmpty(fileLogSinkDateSubDirectory)) { localFileLogSink = new FileLogSink(Path.Combine(logPath, "Debugger." + Environment.MachineName)); } else { localFileLogSink = new FileLogSink(logPath, fileLogSinkDateSubDirectory, "Debugger." + Environment.MachineName); } localFileLogSink.AutoFlush = true; localFileLogSink.MaxNumFiles = NaAppSettingsReader.GetValue("MaximumNumberOfLogFiles", 6); localFileLogSink.MaxDaysOld = NaAppSettingsReader.GetValue("FileLogSinkMaxDaysOld", 30); localFileLogSink.MaxFileSize = NaAppSettingsReader.GetValue<long>("MaximumLogFileSize", 1024 * 1024 * 1024); localFileLogSink.EnabledCategories = fileLogCategories; Log.Sinks.Add(localFileLogSink); } } catch (Exception ex) { Log.WriteExceptionCatch("APP", ex); } } } } 请给代码逐行加上注释解释一下
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值