如何通过提供文件名获取文件的路径? [英] How to get path of file by providing filename?

查看:129
本文介绍了如何通过提供文件名获取文件的路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取文件路径(C:\Program Files \ Company name \InventorySetup \ configSetting.ini)。

但我不允许在程序中编写硬编码的文件路径。如何通过提供文件名(co.ini)和驱动器名称(C:\)以编程方式获取此路径。实际上我的应用程序安装在服务器PC上,它的快捷方式在客户端PC桌面上创建,当客户端PC通过快捷方式使用应用程序时,它从服务器PC获取文件路径,但我想做客户端PC应该使用自己的文件路径作为应用程序也安装在客户端PC。



我怎样才能做到这一点?如果有人不理解我的情况,请告诉我。



目前我使用路径给出安装应用程序文件(exe)的路径,如下所示。



我尝试了什么:



I want to get file path which is (C:\Program Files\Company name\InventorySetup\configSetting.ini).
But i am not allowed to write file path hardcoded in program. how do i get this path programatically by providing filename(co.ini) and drive name (C:\). Actually i my application is installed on server PC and its shortcuts created on client PC desktop and when client PC uses application by shortcuts it gets file path from server PC but i want to do client pc should use its own file path as application also installed in client PC.

How can i achieve this? if anyone not understand my scenario please let me know.

Presently i uses path which gives path where application file (exe) installed, in program like below.

What I have tried:

protected string file = Path.GetDirectoryName(Application.ExecutablePath).Replace("bin\\Debug", "") + "\\configSetting.ini";

推荐答案

首先)如果您的配置文件位于您的exe文件旁边并且它是只读的而不是然后你可以使用:

First) If your config file is beside your exe file and it is read only and won't change then you can use:
protected string file = Path.GetDirectoryName(Application.ExecutablePath) + "\\configSetting.ini";



)如果您的配置文件发生了变化,那么您应该保存在用户数据文件夹中(因为Program Files受Windows保护并且需要管理员权限才能写入) :我应该在哪里存储数据? [ ^ ]


Second) If your config file changes then you should save in the users data folder instead (since "Program Files" is protected by Windows and requires admin permissions for writing to): Where should I store my data?[^]


这篇关于如何通过提供文件名获取文件的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆