C# 为什么程序指向错误的路径?
我正在使用C# 为什么程序指向错误的路径?,c#,wpf,C#,Wpf,我正在使用WPF+MVVMlight。 我创建了一个名为“JSON”的类,并在SimpleIOC中创建了一个实例 使用“JSON”构造函数中的path.GetFullPath获取路径 程序运行良好。但是,App.xaml中出现intellisense错误 “vm:ViewModelLocator x:Key=“Locator”d:IsDataSource=“True”带下划线,出现以下错误 “找不到文件'C:\Users\john\Desktop\Count\Setting.json'。” Pa
WPF+MVVMlight。
我创建了一个名为“JSON”的类,并在SimpleIOC中创建了一个实例
使用“JSON”构造函数中的path.GetFullPath获取路径
程序运行良好。但是,App.xaml中出现intellisense错误
“vm:ViewModelLocator x:Key=“Locator”d:IsDataSource=“True”
带下划线,出现以下错误
“找不到文件'C:\Users\john\Desktop\Count\Setting.json'。”
Path.GetFullPath不是获取当前可执行文件路径的函数吗?
为什么路径“C:\Users\john\Desktop\Count\Setting.json”中应该有一个文件 当我删除“Path.GetFullPath(“Setting.json”);“从JSON类中,IntelliSense错误在App.xaml中消失 哦!还有一件事,即使打开了
MainWindow.xaml
,App.xaml中也会出现同样的错误
我的解决方案文件夹路径是“C:\Users\john\Desktop\Count”
我的执行文件路径是“C:\Users\john\Desktop\Count\Count\bin”
在App.xaml中
<Application x:Class="Count.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:Count"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
StartupUri="3.Driverlayer/MainWindow.xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
d1p1:Ignorable="d" xmlns:d1p1="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Count">
<Application.Resources>
<ResourceDictionary>
<vm:ViewModelLocator x:Key="Locator" d:IsDataSource="True"/>
</ResourceDictionary>
</Application.Resources>
</Application>
或
AppDomain.CurrentDomain.BaseDirectory
(与前者相同)或Path.GetDirectoryName(Assembly.GetExecutionGassembly().Location)
。这取决于应用程序的启动方式。如果不知道,请避免使用Environment.CurrentDirectory
等。”为什么路径“C:\Users\john\Desktop\Count\Setting.json”路径中应该有一个文件。GetFullPath(字符串)返回相对于当前进程的工作目录。您的sln文件或项目文件夹可能位于桌面“Count”上,因此VS的工作文件夹就是这个位置(尽管出乎意料)intellisense故障如果包含sln的文件夹与包含可执行文件的文件夹不同,是否有方法获取包含可执行文件的文件夹?或AppDomain.CurrentDomain.BaseDirectory
(与前者相同)或Path.GetDirectoryName(Assembly.GetExecutionGasEmbly().Location)
。这取决于应用程序的启动方式。如果您不知道,请避免使用Environment.CurrentDirectory
等。“为什么路径“C:\Users\john\Desktop\Count\Setting.json”中应该有文件?”路径。GetFullPath(字符串)相对于当前进程的工作目录返回。您的sln文件或项目文件夹可能位于桌面“Count”上,因此VS的工作文件夹位于此位置(尽管意外)intellisense故障如果包含sln的文件夹与包含可执行文件的文件夹不同,是否有方法获取包含可执行文件的文件夹?
public class ViewModelLocator
{
/// <summary>
/// Initializes a new instance of the ViewModelLocator class.
/// </summary>
public ViewModelLocator()
{
ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);
////if (ViewModelBase.IsInDesignModeStatic)
////{
//// // Create design time view services and models
//// SimpleIoc.Default.Register<IDataService, DesignDataService>();
////}
////else
////{
//// // Create run time view services and models
//// SimpleIoc.Default.Register<IDataService, DataService>();
////}
SimpleIoc.Default.Register<ISettingRead, JSON>(true);
SimpleIoc.Default.Register<MainViewModel>();
}
public MainViewModel Main
{
get
{
return ServiceLocator.Current.GetInstance<MainViewModel>();
}
}
public static void Cleanup()
{
// TODO Clear the ViewModels
}
}
class JSON : ISettingRead
{
string _path;
string _jsonString;
JObject jsonobj;
public JSON()
{
_path = Path.GetFullPath("Setting.json");
using (StreamReader sr = new StreamReader(_path))
{
_jsonString = sr.ReadToEnd();
}
jsonobj = JObject.Parse(_jsonString);
}
}