项目存于Google Drive时Visual Studio 2022安装NuGet包失败
问题描述
项目文件夹位于Google Drive中时,无法通过Visual Studio 2022安装任何NuGet包。将项目移至本地文件夹(如文档)可正常安装,但*.nupkg文件会变为0字节,其他包文件大小正常,清理缓存无效。因工作需要必须使用Google Drive,每次来回拷贝项目过于繁琐。
错误日志
System.IO.InvalidDataException: Central Directory corrupt. ---> System.IO.IOException: An attempt was made to move the file pointer before the beginning of the file. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.SeekCore(Int64 offset, SeekOrigin origin) at System.IO.FileStream.Seek(Int64 offset, SeekOrigin origin) at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory() --- End of inner exception stack trace --- at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory() at System.IO.Compression.ZipArchive.Init(Stream stream, ZipArchiveMode mode, Boolean leaveOpen) at NuGet.Packaging.PackageArchiveReader..ctor(String filePath, IFrameworkNameProvider frameworkProvider, IFrameworkCompatibilityProvider compatibilityProvider) --- End of inner exception stack trace --- at NuGet.Packaging.PackageArchiveReader..ctor(String filePath, IFrameworkNameProvider frameworkProvider, IFrameworkCompatibilityProvider compatibilityProvider) at NuGet.ProjectManagement.FolderNuGetProject.<DeletePackage>d__24.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at NuGet.PackageManagement.NuGetPackageManager.<DeletePackageAsync>d__104.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__87.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__86.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at NuGet.PackageManagement.NuGetPackageManager.<ExecuteNuGetProjectActionsAsync>d__85.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at NuGet.PackageManagement.VisualStudio.NuGetProjectManagerService.<>c__DisplayClass20_0.<<ExecuteActionsAsync>b__0>d.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at NuGet.PackageManagement.VisualStudio.NuGetProjectManagerService.<CatchAndRethrowExceptionAsync>d__32.MoveNext()
可行解决方案
从错误日志分析,核心冲突是Google Drive的实时同步机制干扰了NuGet操作nupkg文件时的指针移动、写入流程,导致Zip包中央目录损坏。以下是验证有效的解决办法:
暂停Google Drive同步后安装包
安装NuGet包前,右键任务栏Google Drive图标,选择「暂停同步」(可选1小时、2小时或直到重启),完成安装后再恢复同步,直接规避同步机制的干扰。修改NuGet全局缓存到本地非同步目录
NuGet默认缓存路径可能被Google Drive同步波及,可手动指定本地独立缓存:- 打开
%appdata%\NuGet\NuGet.Config文件(无则新建) - 添加或修改配置节点:
<configuration> <config> <add key="globalPackagesFolder" value="C:\LocalNuGetCache" /> </config> </configuration> - 重启Visual Studio后尝试安装包。
- 打开
关闭Google Drive文件按需访问功能
若开启了「仅在线文件不占用本地空间」,NuGet操作未完全加载的文件会出错:- 打开Google Drive设置
- 取消勾选「仅在线文件不占用电脑空间」选项
- 等待项目文件完全同步到本地后再操作NuGet。
用Git隔离同步文件范围
项目放在本地,用Git管理版本,仅将代码、配置文件等核心内容同步到Google Drive的Git仓库,把packages、bin、obj等生成目录加入.gitignore,避免同步NuGet相关文件。
内容的提问来源于stack exchange,提问作者aleXela
相关产品推荐
相关产品推荐

