Path.Combine的问题 [英] Problem with Path.Combine

查看:84
本文介绍了Path.Combine的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,如果客户端是新的,我需要根据客户端的名称创建目录(按预期工作),同时写入该目录中的文本文件。我需要以'dateBox',1/1 / 2015.txt中的日期来命名文本文件。 locals窗口显示'filePath'是正确的:

Hello, if the client is new, I need to Create a Directory based on the client's name(works as expected) and at the same time write to a text file in that directory. I need the text file to be named by the date taken from 'dateBox', 1/1/2015.txt for instance. The locals window shows that 'filePath' is correct:

"C:\\Libraries\\Documents\\CLIENTS\\Text\\Last First\\2/6/2015.txt"

但我仍然收到目录未找到的例外情况。我被困住了,希望得到一些帮助。



But I still am getting the Directory Not Found Exception. I'm stuck and would appreciate some help.

private void createNewClientFolder()
        {            
            if (rbNew.Checked)
            {
                
                string newClientName = rtbLast.Text + " " + rtbFirst.Text;
                string locationToCreateFolder = @"C:\Libraries\Documents\CLIENTS\Text\";
                string folder = locationToCreateFolder + newClientName;
                
                Directory.CreateDirectory(folder);

                // Date of scheduled visit.
                string date = dateBox.Text.ToString();
                
                string fileName = date + ".txt";

                string filePath = System.IO.Path.Combine(folder, fileName);

                // Directory Not Found Exception Here...
                FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);

                StreamWriter sw = new StreamWriter(fs);

                sw.WriteLine("DATE: " + dateBox.Text.ToString());
                sw.WriteLine("NAME: " + rtbLast.Text + ", " + rtbFirst.Text);
                sw.WriteLine("VISIT: " + typeOfVisit.Text);
                sw.WriteLine("CODE: " + codeBox.Text);
                sw.WriteLine("DESCRIPTION: " + rtbDesc.Text);
                sw.WriteLine("START: " + startTime.Text);
                sw.WriteLine("END: " + endTime.Text);
                sw.WriteLine("TOTAL: " + timeTotal.Text);

                sw.Flush();
                sw.Close();                                             
            }
        }

推荐答案

C:\\Libraries \\\ \\Documents\\CLIENTS\\Text \\Last First \\\\ / 2 / 2015.txt

这是一个正确的Windows路径?真的吗?

不是,因为/不是路径分隔符,但也不能在文件名中使用。
C:\\Libraries\\Documents\\CLIENTS\\Text\\Last First\\2/6/2015.txt
Is this a corrent Windows path? Really?
It is not, because "/" is not a path separator, but can't be used in file names either.


除了答案1:



请参阅并使用: https://msdn.microsoft.com/en-us/library/system.io.path.directoryseparatorchar%28v=vs.110%29.aspx [ ^ ]。



更重要的是,没有硬编码路径名有用的情况。路径名应来自已使用的输入,某些配置文件以及每个用户帐户或所有用户配置的特殊文件夹。您正在使用C:\ Library \ D \ ...但是您认为目标系统上始终可以使用此目录的原因是什么?我的一个Windows系统甚至没有卷C:......



-SA
In addition to Answer 1:

Please see and use: https://msdn.microsoft.com/en-us/library/system.io.path.directoryseparatorchar%28v=vs.110%29.aspx[^].

More importantly, there are no cases when hard-coded path names are useful. The path names should come from used input, some configuration files, and also "special folders" configured per user accounts or all users. You are using "C:\Libraries\D\…" but what make you thinking that such directory will always be available on the target system? One of my Windows systems did not even have the volume "C:"…

—SA


这篇关于Path.Combine的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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