c# – 如何获取设置winforms应用程序的工作目录?

c# – 如何获取设置winforms应用程序的工作目录?,第1张

概述要获取应用程序的根目录,我目前正在使用: Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase).Substring(6) 但这让我觉得草率.有没有更好的方法来获取应用程序的根目录并将其设置为工作目录? 因此,您只需使用Envrionment.CurrentDirectory =(sum目录)即可更改目录. 要获取应用程序的根目录,我目前正在使用:

Path.GetDirectoryname(Assembly.GetExecutingAssembly().Getname().CodeBase).Substring(6)

但这让我觉得草率.有没有更好的方法来获取应用程序的根目录并将其设置为工作目录?

解决方法 因此,您只需使用Envrionment.CurrentDirectory =(sum目录)即可更改目录.有许多方法可以获得原始执行的directoy,一种方式基本上是您描述的方式,另一种方法是通过Directory.GetCurrentDirectory(),如果您还没有更改目录.

using System;using System.IO;class Test {    public static voID Main()     {        try         {            // Get the current directory.            string path = Directory.GetCurrentDirectory();            string target = @"c:\temp";            Console.Writeline("The current directory is {0}",path);            if (!Directory.Exists(target))             {                Directory.CreateDirectory(target);            }            // Change the current directory.            Environment.CurrentDirectory = (target);            if (path.Equals(Directory.GetCurrentDirectory()))             {                Console.Writeline("You are in the temp directory.");            }             else             {                Console.Writeline("You are not in the temp directory.");            }        }         catch (Exception e)         {            Console.Writeline("The process Failed: {0}",e.ToString());        }    }

ref

总结

以上是内存溢出为你收集整理的c# – 如何获取/设置winforms应用程序的工作目录?全部内容,希望文章能够帮你解决c# – 如何获取/设置winforms应用程序的工作目录?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1221458.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-05
下一篇2022-06-05

发表评论

登录后才能评论

评论列表(0条)

    保存