简体   繁体   English

有没有办法动态定位我当前 c# Winforms 应用程序的子文件夹/子文件的路径更改?

[英]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:您也可以尝试扫描整个文件系统,但有几个缺点:

  • time consuming process耗时的过程
  • probably will return multiple hits when scanning for a folder named Logs (non unique name)扫描名为 Logs(非唯一名称)的文件夹时,可能会返回多个命中

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.

相关问题 解决方案子文件夹中的C#资源文件导致MissingManifestResourceException - C# Resource files in solution sub-folder causing MissingManifestResourceException 将文本文件写入子文件夹 - Write a text file to a sub-folder 有什么更好的方法来删除C#2.0中文件路径中附加的子文件夹 - What's a better way to remove a sub folder that attached in the file path in C# 2.0 ASP.NET C#:在同一服务器上的另一个网站中创建子文件夹 - ASP.NET C#: Create sub-folder in another website on same server 在网站的子文件夹中托管mvc web api应用程序 - Host a mvc web api application in the sub-folder of a website 用于在特定文件夹C#中查找特定文件的自定义路径 - Custom Path for locating a specific file in specific folder c# 在(子文件夹)上找不到MasterPage的图像 - Image of MasterPage not found on (sub-folder) 正则表达式通过子文件夹匹配URL - Regex matching URL's by sub-folder TreeView其中的根和所有节点都来自第一个子文件夹 - 如何指定正确的路径? - TreeView Where Root And All Nodes Thereafter Are Derived From First Sub-Folder - How To Specify Correct Path? 在Area下添加子文件夹并配置View Engine搜索子文件夹中的视图 - Adding a sub-folder under Area and configuring View Engine to search the views in the sub-folder
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM