将文件保存在当前工作目录中 [英] Saving a file in current working directory

查看:133
本文介绍了将文件保存在当前工作目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用C#Compact Framework应用程序将文件保存在当前工作目录中,该文件位于我的.exe文件所在的位置,但是当我尝试这样保存时,它给我IO错误:

".. \\ .. \\ type1.txt"


我也尝试过:

字符串st1 = @"\\ type1.txt"

但这也给了我IO异常..

我尝试使用Directory.GetCurrentDirectory()来获取当前目录,但这给了我一个不受支持的异常.
请尽快帮我.

谢谢,
Jason

Hi all,

I am trying to save my file in current working directory using C# Compact Framework application, where my .exe file is located but it give me IO error when i try to save like this:

"..\\..\\type1.txt"


I also tried this:

string st1 = @"\\type1.txt"

but this also gave me an IO exception..

I tried to fetch the current directory using Directory.GetCurrentDirectory(), but this gave me a not supported exception..

Please help me up asap.

Thanks,
Jason

推荐答案

Jason,

除非您使用HTML,否则路径不是相对的.

如果要将文件名保存在当前工作文件夹"(Environment.CurrentDirectory)中,则应该使用该文件名,或者执行应用程序(Application.StartupPath)来保存从中启动应用程序的文件.

因此,要在打开应用程序的文件夹中创建用于打开/保存的完整路径/文件名,请使用...

Jason,

Paths aren''t relative unless you are dealing in HTML.

You should just use the file name if you want it saved in the "current working folder" (Environment.CurrentDirectory), or get the application execution (Application.StartupPath) to save the file where the application was launched from.

So, to create the full path/file name for opening/saving in the folder the application is launched from... use:

string fileName = String.Format(@"{0}\type1.txt", Application.StartupPath);



还有一个提示...在字符串前面使用@表示按字面意义" ...因此您的\\不会像您编写那样转换为单个\. @"\\..."将是网络UNC路径的开始.

希望有帮助,
弗兰克



One more tip... using the @ in front of a string means ''take this literally''... so your \\ is not translated to a single \ the way you wrote that. @"\\..." would be the beginning of a network UNC path.

Hope that helps,
Frank


嗨弗兰克,

感谢您的帮助,但是正如我提到的那样,我正在使用紧凑型框架,因此Environment.CurrentDirectory或获取应用程序执行(Application.StartupPath)均无效,因此我使用了以下字符串,为我完成了工作.

Hi Frank,

Thanks for the help but as I mentioned I am using compact framework so Environment.CurrentDirectory, or get the application execution (Application.StartupPath) did not worked so I used the below string and that did the job for me.

string fileName = String.Format(@"{0}\type1.txt", System.IO.Path.GetDirectoryName(
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);



非常感谢您的帮助.
Jason.



Thanks a lot for your help.
Jason.


字符串fileName = String.Format(@"{0} \ type1.txt",Application.StartupPath);
string fileName = String.Format(@"{0}\type1.txt", Application.StartupPath);


这篇关于将文件保存在当前工作目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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