文件路径存储在变量中 [英] File path stored in variable

查看:172
本文介绍了文件路径存储在变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Visual Studio 2008 Windows窗体.我目前正在将文件路径保存到字符串变量中.当我想在richtextbox.loadfile中使用变量时,出现以下异常:文件格式无效".

包含文件路径的变量在两个类的构造函数之间传递.

变量的值:字符串变量="C:\\ temp \\ filename

我知道它看起来应该类似于以下内容:RichTextBox1.LoadFile(@"C:\ temp \ filename"),但是如何获取变量以使RichTextBox加载文件所需的格式? class ="h2_lin">解决方案

问题基于我什至无法完全理解的巨大而根本的误解.变量不能有任何称为格式"的内容.多亏了Alan的回答,您现在有了一个实用的解决方案,但是您的想法似乎很困惑.另外,建议不要接受类似"C:\ temp"的路径名. Windows 7或Vista无法使用.并在所有其他使用类似目录的系统中都很脏.

首先,不存在在应用程序中可以使用硬编码的绝对或相对路径的情况.所有路径总是在运行时计算,或者至少可以基于安装过程中设置的配置文件来计算.您需要使用System.Environment.GetFolderPath(Environment.SpecialFolder)在特殊目录"之一中使用路径.参见:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx [ ^ ],
http://msdn.microsoft.com/en-us/library/system.environment. specialfolder.aspx [ ^ ].

—SA



文件名可以.该异常告诉您的是,RTF文本框无法解释文件内容.查看其他LoadFile方法的文档,特别是

 公共  void  LoadFile(字符串路径,RichTextBoxStreamType fileType)


这样您就可以指定文件内容的格式.

艾伦.


I''m using Visual Studio 2008 windows forms. I am currently saving a file path into a string variable. When I want to use the variable in the richtextbox.loadfile i get the following exception: "File format is not valid".

the variable containing the file path is passed between two classes constructors.

value of variable: string variable = "C:\\temp\\filename

I know that it should look something like the following: RichTextBox1.LoadFile(@"C:\temp\filename"), but how to get the variable to be in the format required in order for the RichTextBox to load the file?

解决方案

The question is based on huge and fundamental misconception I cannot even fully understand. A variable cannot have anything called "format". You have a practical solution now, thanks to the answer by Alan, but your thinking looks like very confused. Also, be advices that the path name like "C:\temp" is unacceptable. Windows 7 or Vista would not allow you to use it; and in all other system using directories like that is just dirty.

First of all, there is no such situations where a hard-coded absolute or relative path could be used in application. All paths are always calculated during run-time or at least can be based on configuration files set up during installation. You need to use a path in one of the "special directories" using System.Environment.GetFolderPath(Environment.SpecialFolder). See:
http://msdn.microsoft.com/en-us/library/14tx8hby.aspx[^],
http://msdn.microsoft.com/en-us/library/system.environment.specialfolder.aspx[^].

—SA


Hi,
The filename is ok. What that exception is telling you is that the file contents cannot be interpreted by the rich text box. Take a look at the documentation for the other LoadFile methods, especially

public void LoadFile (string path, RichTextBoxStreamType fileType)


as that will allow you to specify the format of the file contents.

Alan.


这篇关于文件路径存储在变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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