繁体   English   中英

以编程方式获取文件夹位置

[英]Programmatically get the folder location

我有此VB代码,在调试时会获得以下位置的代码

C:\\ Users \\ AUpadhyay \\ AppData \\ Roaming \\ Sterling Ultra Precision \\ Power Suite \\ 2.7.0.0

VB代码是

AppPath = System.IO.Directory.GetParent(My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData).ToString

我试图使用下面的C#代码访问同一目录,但输出为

C:\\ Users \\ AUpadhyay \\ AppData

C#代码是

AppPath = System.IO.Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).ToString();

获取相同目录地址的代码行是什么?

这将为您Roaming Folder that is present inside AppData folderRoaming Folder that is present inside AppData folder ,然后其余文件夹为已知,因此您可以使用Path.CombinePath.Combine它们; 考虑代码:

string AppPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

string requiredPath=Path.Combine(AppPath,@"Sterling Ultra Precision\Power Suite\2.7.0.0");

Application.UserAppDataPath将是您的解决方案。

可以从VB.NET和C#进行访问,并且返回:

C:\Users\[Your user name]\AppData\Roaming\[Your company name]\[Your product name]\[Your product version]

C#的结果:

MessageBox中的Application.UserAppDataPath

您可以使用以下代码在c#中获取当前目录

string AppPath = AppDomain.CurrentDomain.BaseDirectory;

希望这会有所帮助

var fileName = Path.Combine(Environment.GetFolderPath(
    Environment.SpecialFolder.ApplicationData), "FileYouWant.foo")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM