代码之家  ›  专栏  ›  技术社区  ›  The King

在客户端系统中,将在何处创建没有路径的文件

  •  3
  • The King  · 技术社区  · 15 年前

    我在WinForm应用程序中有以下代码

     String[] lines = { "LAST MESSAGE", "101" };
     File.WriteAllLines("MPP_Config.txt", lines);
    

    在我的开发系统中,文件在bin\debug下创建…

    一旦文件安装到客户机系统中,它将在哪里创建?

    我使用Click Once部署部署到网站…

    3 回复  |  直到 15 年前
        1
  •  4
  •   Sam Holder Brian Adams    15 年前

    我相信它将在应用程序的当前工作目录中创建。应用程序可能无法访问此目录,特别是在具有 UAC ,Vista和Windows 7。你应该考虑使用 application data directory 相反。

    String[] lines = { "LAST MESSAGE", "101" };
    String fileName = Path.combine(System.Deployment.Application.ApplicationDeployment.CurrentDeployment.DataDirectory,"MPP_Config.txt");
    File.WriteAllLines(fileName, lines);
    
        2
  •  2
  •   Mutation Person    15 年前

    我猜它是在debug文件夹中创建的,因为您一直在以debug模式运行它。如果您在释放模式下运行它,那么它将保存在bin/release文件夹中。

    换句话说,它将在应用程序所在的目录中创建。为什么不试试呢?也就是说,把你的exe文件复制到…

        3
  •  1
  •   Fredrik Mörk    15 年前

    current directory . 这意味着很难100%确定地预测。如果你想控制它,你可以使用 Environment.CurrentDirectory 但是(正如Sam在评论中指出的那样),这可能不是一个好主意,因为其他代码也可能出于其他目的依赖于当前目录。

    例如,以下程序将创建两个名为“somefile.txt”的不同文件(假定该exe不是从c:\temp目录运行的):

    static void Main(string[] args)
    {
        File.WriteAllText("somefile.txt", "some text");
        Environment.CurrentDirectory = @"c:\temp";
        File.WriteAllText("somefile.txt", "some text");
    }
    
    推荐文章