[英]Is there a way of dynamically locating a change of path of a sub-folder/sub-file of my current c# Winforms application?
Is it possible to dynamically locate a certain folder/file?是否可以动态定位某个文件夹/文件?
For example, let's say I have got a configuration file that my application obviously needs to be able to read.例如,假设我有一个配置文件,我的应用程序显然需要能够读取该文件。 Currently, the path to my configuration file is hardcoded.目前,我的配置文件的路径是硬编码的。 However, let's say when I deploy the application, the client decides to move that configuration file in some other random directory.但是,假设当我部署应用程序时,客户端决定将该配置文件移动到某个其他随机目录中。 Can I make it so my application will be able to locate that random directory without manually changing it in the source code?我可以这样做,以便我的应用程序能够找到该随机目录,而无需在源代码中手动更改它吗?
Another example, I also have a Logs folder which stores my log files to which I need to be writing constantly.另一个例子,我还有一个 Logs 文件夹,用于存储我需要不断写入的日志文件。 Same as the configuration file, if the client decides to change the Logs folder somewhere else, I need to be able to dynamically locate it from within my code.与配置文件相同,如果客户端决定在其他地方更改 Logs 文件夹,我需要能够从我的代码中动态定位它。
Thanks for any future suggestions.感谢您提供任何未来的建议。
A good way to approach this problem is to use relative paths in your code.解决此问题的一个好方法是在代码中使用相对路径。 Your application needs to somehow get paths to folders / files it calls for.您的应用程序需要以某种方式获取它所调用的文件夹/文件的路径。 In case that relocating folder with Logs is a priority for you and you are willing to have a file with path (which cannot be relocated):如果您优先考虑使用 Logs 重新定位文件夹,并且您愿意拥有一个带路径的文件(无法重新定位):
/filePaths.something
Then you can store (be it in plaintext, json, xml, binary or whatever) paths to the other files / folders.然后你可以存储(以纯文本、json、xml、二进制或其他方式)到其他文件/文件夹的路径。 Eg:例如:
{
"logsFolder": "C:/Some/Random/Folders",
"anotherFile": "D:/Client/Can/Edit/This"
}
You can also try scanning entire filesystem but there are several drawbacks:您也可以尝试扫描整个文件系统,但有几个缺点:
There are probably milion of milions files on your client PC, scanning recursively just for folders will speed this up a bit but still it's gonna be very lenghty.您的客户端 PC 上可能有数百万个文件,仅递归扫描文件夹会加快速度,但仍然会很长。
Another approach might be conneting your app to a database but that really doesn't differ from the first option described above.另一种方法可能是将您的应用程序连接到数据库,但这实际上与上述第一个选项没有区别。 All in all you'll probably need to go some mid way to satisfy both technical limitations and your clients.总而言之,您可能需要采取一些中间方式来满足技术限制和您的客户。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.