查找路径并创建文件。 [英] Finding path and creating a file.

查看:104
本文介绍了查找路径并创建文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + @"\keyfile\EmailbodyorFile.txt");





我用它来获取路径,但我的问题是'EmailbodyorFile.txt'必须在运行时自动创建并且上面的代码没有创建文件。那该怎么办?





之前我使用过这段代码,



I used this to get the path,but my problem is 'EmailbodyorFile.txt' must be created automatically during runtime and the above code do not create the file.So what to do?


Earlier I used this code ,

string path = Path.GetFullPath(System.AppDomain.CurrentDomain.BaseDirectory + @"\keyfile\EmailbodyorFile.txt");



并且运行正常,但在创建项目设置后,它显示错误'文件未找到错误'。



请帮忙。


and it run correctly,but after creating setup of project, it shows error 'File not found error'.

please help.

推荐答案

你可以使用 File.Create 在所需路径创建/覆盖文件的方法。
You can make use of File.Create method to create/overwrite the file at desired path.


实际上你的代码有点问题,我想想,你错放了')'。你在整个字符串的末尾使用它而不是在'Location'之后使用它像这样

Actually your code has a bit of a problem, I think, you misplaced ')'. You used it at the end of the whole string instead of using it after 'Location' like this
System.Reflection.Assembly.GetExecutingAssembly().Location + @"\keyfile\EmailbodyorFile.txt");



结果为您提供了位置,但是您的 \ keyfile



我尝试过并运行代码以查看它返回的路径,我通过运行此代码得到了这个:


which in result gives you the location, but with your \keyfile.

I tried and run the code to see what path does it return and i got this by running this code:

Console.WriteLine(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location + @"\keyfile\EmailbodyorFile.txt"))



输出为:

c:\ usersrs \ USERNAME \documents\visual studio 2015 \Projects\CODEPROJECT\CODEPROJECT\bin\Debug\CODEPROJECT.exe \ keyfile



所以,正确的方法是:


the output was:
c:\users\USERNAME\documents\visual studio 2015\Projects\CODEPROJECT\CODEPROJECT\bin\Debug\CODEPROJECT.exe\keyfile

So, The right way to do it is this:

System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\keyfile\EmailbodyorFile.txt";



哪个将返回此路径:

c:\ usersrs \USERNAME \documents\visual studio 2015 \Projects\CODEPROJECT\CODEPROJECT\bin\Debug \keyfile\EmailbodyorFile.txt





并且为了检查目录和文件的存在,你可以使用此代码:


Which will return this path:
c:\users\USERNAME\documents\visual studio 2015\Projects\CODEPROJECT\CODEPROJECT\bin\Debug\keyfile\EmailbodyorFile.txt


And for checking the existence of directory and file you can use this code:

string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\keyfile";
string filePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + @"\keyfile\EmailbodyorFile.txt";

Start:
  if (System.IO.Directory.Exists(dir)) {
    if (System.IO.File.Exists(filePath)) {
      //DoStuff();
    } else {
      System.IO.File.WriteAllText(filePath, "/*your data*/");
      goto Start;
    }
  } else { System.IO.Directory.CreateDirectory(dir); goto Start; }





我希望你能理解我写的内容:)。



I hope you understood what I wrote :) .

-Muhammad Muzzammil<br />
-www.facebook.com/muhammadmuzzammil1998<br />
-muhammadmuzzammil.cs@gmail.com


这篇关于查找路径并创建文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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