文章存档
要进行“网络硬盘”功能设计,首先要熟悉.NET中处理文件和文件夹的操作。File类和Directory类是其中最主要的两个类。了解它们将对后面功能的实现提供很大的便利。

System.IO.File类和System.IO.FileInfo类

在设计和实现“网络硬盘”的过程中,将大量地使用和文件系统操作相关的内容。故本节先对和文件系统相关的两个.NET类进行简要介绍。

System.IO.File类和System.IO.FileInfo类主要提供有关文件的各种操作,在使用时需要引用System.IO命名空间。下面通过程序实例来介绍其主要属性和方法。

(1) 文件打开方法:File.Open

该方法的声明如下:

public static FileStream Open(string path,FileMode mode)

下面的代码打开存放在c:\tempuploads目录下名称为newFile.txt文件,并在该文件中写入hello。

private void OpenFile()
{
FileStream.TextFile
=File.Open(@"c:\tempuploads\newFile.txt",FileMode.Append);
byte [] Info = {(byte)'h',(byte)'e',(byte)'l',(byte)'l',(byte)'o'};
TextFile.Write(Info,
0,Info.Length);
TextFile.Close();
}


(
2) 文件创建方法:File.Create

该方法的声明如下:

public static FileStream Create(string path;)

下面的代码演示如何在c:\tempuploads下创建名为newFile.txt的文件。

由于File.Create方法默认向所有用户授予对新文件的完全读
/写访问权限,所以文件是用读/写访问权限打开的,必须关闭后才能由其他应用程序打开。为此,所以需要使用FileStream类的Close方法将所创建的文件关闭。

private void MakeFile()
{
FileStream NewText
=File.Create(@"c:\tempuploads\newFile.txt");
NewText.Close();
}


(
3) 文件删除方法:File.Delete

该方法声明如下:

public static void Delete(string path);

下面的代码演示如何删除c:\tempuploads目录下的newFile.txt文件。

private void DeleteFile()
{
File.Delete(
@"c:\tempuploads\newFile.txt");
}


(
4) 文件复制方法:File.Copy

该方法声明如下:

public static void Copy(string sourceFileName,string destFileName,bool overwrite);

下面的代码将c:\tempuploads\newFile.txt复制到c:\tempuploads\BackUp.txt。

由于Cope方法的OverWrite参数设为true,所以如果BackUp.txt文件已存在的话,将会被复制过去的文件所覆盖。

private void CopyFile()
{
File.Copy(
@"c:\tempuploads\newFile.txt",@"c:\tempuploads\BackUp.txt",true);
}


(
5) 文件移动方法:File.Move

该方法声明如下:

public static void Move(string sourceFileName,string destFileName);

下面的代码可以将c:\tempuploads下的BackUp.txt文件移动到c盘根目录下。

注意:

只能在同一个逻辑盘下进行文件转移。如果试图将c盘下的文件转移到d盘,将发生错误。

private void MoveFile()
{
File.Move(
@"c:\tempuploads\BackUp.txt",@"c:\BackUp.txt");
}


(
6) 设置文件属性方法:File.SetAttributes

该方法声明如下:

public static void SetAttributes(string path,FileAttributes fileAttributes);

下面的代码可以设置文件c:\tempuploads\newFile.txt的属性为只读、隐藏。

private void SetFile()
{
File.SetAttributes(
@"c:\tempuploads\newFile.txt",
FileAttributes.ReadOnly
|FileAttributes.Hidden);
}


文件除了常用的只读和隐藏属性外,还有Archive(文件存档状态),System(系统文件),Temporary(临时文件)等。关于文件属性的详细情况请参看MSDN中FileAttributes的描述。

(
7) 判断文件是否存在的方法:File.Exist

该方法声明如下:

public static bool Exists(string path);

下面的代码判断是否存在c:\tempuploads\newFile.txt文件。若存在,先复制该文件,然后其删除,最后将复制的文件移动;若不存在,则先创建该文件,然后打开该文件并进行写入操作,最后将文件属性设为只读、隐藏。

if(File.Exists(@"c:\tempuploads\newFile.txt")) //判断文件是否存在
{
CopyFile();
//复制文件
DeleteFile(); //删除文件
MoveFile(); //移动文件
}

else
{
MakeFile();
//生成文件
OpenFile(); //打开文件
SetFile(); //设置文件属性
}


此外,File类对于Text文本提供了更多的支持。

· AppendText:将文本追加到现有文件

· CreateText:为写入文本创建或打开新文件

· OpenText:打开现有文本文件以进行读取

但上述方法主要对UTF
-8的编码文本进行操作,从而显得不够灵活。在这里推荐读者使用下面的代码对txt文件进行操作。

· 对txt文件进行“读”操作,示例代码如下:

StreamReader TxtReader
= new StreamReader(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default);
string FileContent;
FileContent
= TxtReader.ReadEnd();
TxtReader.Close();

· 对txt文件进行“写”操作,示例代码如下:

StreamWriter
= new StreamWrite(@"c:\tempuploads\newFile.txt",System.Text.Encoding.Default);
string FileContent;
TxtWriter.Write(FileContent);
TxtWriter.Close();

System.IO.Directory类和System.DirectoryInfo类

主要提供关于目录的各种操作,使用时需要引用System.IO命名空间。

下面通过程序实例来介绍其主要属性和方法。

(1) 目录创建方法:Directory.CreateDirectory

该方法声明如下:

public static DirectoryInfo CreateDirectory(string path);

下面的代码演示在c:\tempuploads文件夹下创建名为NewDirectory的目录。

private void MakeDirectory()
{
Directory.CreateDirectory(
@"c:\tempuploads\NewDirectoty");
}


(
2) 目录属性设置方法:DirectoryInfo.Atttributes

下面的代码设置c:\tempuploads\NewDirectory目录为只读、隐藏。与文件属性相同,目录属性也是使用FileAttributes来进行设置的。

private void SetDirectory()
{
DirectoryInfo NewDirInfo
= new DirectoryInfo(@"c:\tempuploads\NewDirectoty");
NewDirInfo.Atttributes
= FileAttributes.ReadOnly|FileAttributes.Hidden;
}


(
3) 目录删除方法:Directory.Delete

该方法声明如下:

public static void Delete(string path,bool recursive);

下面的代码可以将c:\tempuploads\BackUp目录删除。Delete方法的第二个参数为bool类型,它可以决定是否删除非空目录。如果该参数值为true,将删除整个目录,即使该目录下有文件或子目录;若为false,则仅当目录为空时才可删除。

private void DeleteDirectory()
{
Directory.Delete(
@"c:\tempuploads\BackUp",true);
}


(
4) 目录移动方法:Directory.Move

该方法声明如下:

public static void Move(string sourceDirName,string destDirName);

下面的代码将目录c:\tempuploads\NewDirectory移动到c:\tempuploads\BackUp。

private void MoveDirectory()
{
File.Move(
@"c:\tempuploads\NewDirectory",@"c:\tempuploads\BackUp");
}


(
5) 获取当前目录下的所有子目录方法:Directory.GetDirectories

该方法声明如下:

public static string[] GetDirectories(string path;);

下面的代码读出c:\tempuploads\目录下的所有子目录,并将其存储到字符串数组中。

private void GetDirectory()
{
string [] Directorys;
Directorys
= Directory. GetDirectories (@"c:\tempuploads");
}


(
6) 获取当前目录下的所有文件方法:Directory.GetFiles

该方法声明如下:

public static string[] GetFiles(string path;);

下面的代码读出c:\tempuploads\目录下的所有文件,并将其存储到字符串数组中。

private void GetFile()
{
string [] Files;
Files
= Directory. GetFiles (@"c:\tempuploads",);
}


(
7) 判断目录是否存在方法:Directory.Exist

该方法声明如下:

public static bool Exists(
string path;
); 下面的代码判断是否存在c:\tempuploads\NewDirectory目录。若存在,先获取该目录下的子目录和文件,然后其移动,最后将移动后的目录删除。若不存在,则先创建该目录,然后将目录属性设为只读、隐藏。
[CODE_LITE]
if(File.Exists(@"c:\tempuploads\NewDirectory")) //判断目录是否存在
{
GetDirectory();
//获取子目录
GetFile(); //获取文件
MoveDirectory(); //移动目录
DeleteDirectory(); //删除目录
}

else
{
MakeDirectory();
//生成目录
SetDirectory(); //设置目录属性
}

注意:
路径有3种方式,当前目录下的相对路径、当前工作盘的相对路径、绝对路径。以C:\Tmp\Book为例(假定当前工作目录为C:\Tmp)。“Book”,“\Tmp\Book”,“C:\Tmp\Book”都表示C:\Tmp\Book。

另外,在C#中 “\”是特殊字符,要表示它的话需要使用“\\”。由于这种写法不方便,C#语言提供了@对其简化。只要在字符串前加上@即可直接使用“\”。所以上面的路径在C#中应该表示为“Book”,@“\Tmp\Book”,@“C:\Tmp\Book”。

推荐(0)
收藏

用ASP.NET开发,ASP.NET开发WAP站点,实在简单,就是一个移动控件的运用,根本不用懂WML语法.唯一不同的,就是一个ASPX文件可以支持多个FORM,不同FORM之间可以切换,
最后ASP.NET根据访问的设备,输出不同的内容,
如果用IE访问就输出HTML,手机访问,输出WML,
不过在开发过程中也遇到了很多问题,一些问题总结如下:
1.模拟器选择:
WINWAP,
M3Gate,
UP.SDK4.0,(推荐)
OPenWave 5.0 (测试时需要),
OPenWave6.2, (IIS 6.0应用,IIS5.0可能显示,但会有问题)
CheckCom WAPBrowser 3.2
模拟器问题:
ASP.NET把一些常有的模拟器,的配制信息用正则表达式写在Machine.config里
在上述的模拟器中,我只有UP.SDK4.0,OPenWave 5.0,
他们只是一个公司的产品:
需要下载,配制文件驱动更新:现在最新版本是:Device 4下载请到这里下载:
www.microsoft.com/downloads/details.aspx?FamilyId ... B1-4DD61A5B7CCB&displaylang=en
但是,即使驱动更新以后,还是一些设备不能访问:
这时需要强制输出WML:
在web.config
添加下面内容下

preferredRenderingType = "wml11"
preferredRenderingMime = "text/vnd.wap.wml"
preferredImageMime = "image/vnd.wap.wbmp"

如果定义相关其它属性可以完整的移动设置属性:

browser=Unknown
version=0.0
majorversion=0
minorversion=0
frames=false
tables=false
cookies=false
backgroundsounds=false
vbscript=false
javascript=false
javaapplets=false
activexcontrols=false
win16=false
win32=false
beta=false
ak=false
sk=false
aol=false
crawler=false
cdf=false
gold=false
authenticodeupdate=false
tagwriter=System.Web.UI.Html32TextWriter
ecmascriptversion=0.0
msdomversion=0.0
w3cdomversion=0.0
platform=Unknown
css1=false
css2=false
xml=false

mobileDeviceManufacturer = "Unknown"
mobileDeviceModel = "Unknown"

gatewayVersion = "None"
gatewayMajorVersion = "0"
gatewayMinorVersion = "0"

preferredRenderingType = "wml11"
preferredRenderingMime = "text/vnd.wap.wml"
preferredImageMime = "image/vnd.wap.wbmp"

defaultScreenCharactersWidth = "12"
defaultScreenCharactersHeight = "6"
defaultScreenPixelsWidth = "96"
defaultScreenPixelsHeight = "72"
defaultCharacterWidth = "8"
defaultCharacterHeight = "12"
screenBitDepth = "1"
isColor = "false"
inputType = "telephoneKeypad"

numberOfSoftkeys = "0"
maximumSoftkeyLabelLength = "5"

canInitiateVoiceCall = "false"

canSendMail = "true"
hasBackButton = "true"
rendersWmlDoAcceptsInline = "true"
rendersWmlSelectsAsMenuCards = "true"
rendersBreaksAfterWmlAnchor = "false"
rendersBreaksAfterWmlInput = "false"
rendersBreakBeforeWmlSelectAndInput = "true"
requiresAttributeColonSubstitution = "true"
requiresPhoneNumbersAsPlainText = "false"
requiresUrlEncodedPostfieldValues = "false"
requiredMetaTagNameValue = ""
rendersBreaksAfterHtmlLists = "true"
requiresUniqueHtmlCheckboxNames = "true"
requiresUniqueHtmlInputNames = "true"
requiresUniqueFilePathSuffix = "true"
supportsCss = "false"
hidesRightAlignedMultiselectScrollbars = "false"
canRenderAfterInputOrSelectElement = "true"
canRenderInputAndSelectElementsTogether = "true"
canRenderOneventAndPrevElementsTogether = "true"
canCombineFormsInDeck = "true"
canRenderMixedSelects = "true"
canRenderPostBackCards = "true"
canRenderSetvarZeroWithMultiSelectionList = "true"
supportsImageSubmit = "true"
supportsSelectMultiple = "true"
requiresHtmlAdaptiveErrorReporting = "false"
requiresContentTypeMetaTag = "false"
requiresDBCSCharacter = "false"
requiresOutputOptimization = "false"
supportsAccesskeyAttribute = "false"
supportsInputIStyle = "false"
supportsInputMode = "false"
supportsIModeSymbols = "false"
supportsJPhoneSymbols = "false"
supportsJPhoneMultiMediaAttributes = "false"
maximumRenderedPageSize = "2000"
requiresSpecialViewStateEncoding = "false"
requiresNoBreakInFormatting = "false"
requiresLeadingPageBreak = "false"
supportsQueryStringInFormAction = "true"
supportsCacheControlMetaTag = "true"
supportsUncheck = "true"
canRenderEmptySelects = "true"
supportsRedirectWithCookie = "true"
supportsEmptyStringInCookieValue = "true"
cachesAllResponsesWithExpires = "false"
requiresNoSoftkeyLabels = "false"
defaultSubmitButtonLimit = "1"

supportsBold = "false"
supportsItalic = "false"
supportsFontSize = "false"
supportsFontName = "false"
supportsFontColor = "true"
supportsBodyColor = "true"
supportsDivAlign = "true"
supportsDivNoWrap = "false"
supportsCharacterEntityEncoding = "true"

isMobileDevice="false"

具体属性的意思可以考试MSDN:设备功能列表
ms-help://MS.MSDNQTR.2003FEB.2052/mwsdk/html/mwlrfDeviceCapabilitiesTable.htm

2.链接:
在同一个aspx文件,使用Link控件的,#FORM来切换不同Form时在 OPenWave 5.0测试会出现错误!
建议在程序通过ActiveForm来做
3.乱码:
如果在开发过程出现乱码,通过修改Web.Config

4.取消移动设备缓存:
Page.Response.Expires = -1;
Response.CacheControl = "Public";
5.重定向
重定向通过
RedirectToMobilePage函数
不过使用过程中可能会出现一些问题
建议用Link控件来代替

推荐(0)
收藏