| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748 |
- //
- // FileTest.cs: Test cases for System.IO.File
- //
- // Author:
- // Duncan Mak ([email protected])
- // Ville Palo ([email protected])
- //
- // (C) 2002 Ximian, Inc. http://www.ximian.com
- //
- // TODO: Find out why ArgumentOutOfRangeExceptions does not manage to close streams properly
- //
- using System;
- using System.IO;
- using System.Globalization;
- using System.Threading;
- using System.Runtime.InteropServices;
- using NUnit.Framework;
- namespace MonoTests.System.IO
- {
- [TestFixture]
- public class FileTest
- {
- CultureInfo old_culture;
- string tmpFolder;
- [SetUp]
- public void SetUp ()
- {
- tmpFolder = Path.GetTempFileName ();
- if (File.Exists (tmpFolder))
- File.Delete (tmpFolder);
- DeleteDirectory (tmpFolder);
- Directory.CreateDirectory (tmpFolder);
- old_culture = Thread.CurrentThread.CurrentCulture;
- Thread.CurrentThread.CurrentCulture = new CultureInfo ("en-US", false);
- }
- [TearDown]
- public void TearDown ()
- {
- DeleteDirectory (tmpFolder);
- Thread.CurrentThread.CurrentCulture = old_culture;
- }
- string path;
- string testfile;
- [TestFixtureSetUp]
- public void FixtureSetUp ()
- {
- path = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
- testfile = Path.Combine (path, "FileStreamTest.dat");
- File.WriteAllText (testfile, "1");
- }
- [TestFixtureTearDown]
- public void FixtureTearDown ()
- {
- if (File.Exists (testfile))
- File.Delete (testfile);
- }
- [Test]
- public void TestExists ()
- {
- FileStream s = null;
- string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- try {
- Assert.IsFalse (File.Exists (null), "#1");
- Assert.IsFalse (File.Exists (string.Empty), "#2");
- Assert.IsFalse (File.Exists (" \t\t \t \n\t\n \n"), "#3");
- DeleteFile (path);
- s = File.Create (path);
- s.Close ();
- Assert.IsTrue (File.Exists (path), "#4");
- Assert.IsFalse (File.Exists (tmpFolder + Path.DirectorySeparatorChar + "doesnotexist"), "#5");
- } finally {
- if (s != null)
- s.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void Exists_InvalidFileName ()
- {
- Assert.IsFalse (File.Exists ("><|"), "#1");
- Assert.IsFalse (File.Exists ("?*"), "#2");
- }
- [Test]
- public void Exists_InvalidDirectory ()
- {
- Assert.IsFalse (File.Exists (Path.Combine ("does not exist", "file.txt")));
- }
- [Test]
- public void Create_Path_Null ()
- {
- try {
- File.Create (null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Create_Path_Directory ()
- {
- string path = Path.Combine (tmpFolder, "foo");
- Directory.CreateDirectory (path);
- try {
- File.Create (path);
- Assert.Fail ("#1");
- } catch (UnauthorizedAccessException ex) {
- // Access to the path '...' is denied
- Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- } finally {
- DeleteDirectory (path);
- }
- }
- [Test]
- public void Create_Path_Empty ()
- {
- try {
- File.Create (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Create_Path_ReadOnly ()
- {
- string path = Path.Combine (tmpFolder, "foo");
- File.Create (path).Close ();
- File.SetAttributes (path, FileAttributes.ReadOnly);
- try {
- File.Create (path);
- Assert.Fail ("#1");
- } catch (UnauthorizedAccessException ex) {
- // Access to the path '...' is denied
- Assert.AreEqual (typeof (UnauthorizedAccessException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- } finally {
- File.SetAttributes (path, FileAttributes.Normal);
- }
- }
- [Test]
- public void Create_Path_Whitespace ()
- {
- try {
- File.Create (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Create_Directory_DoesNotExist ()
- {
- FileStream stream = null;
- string path = tmpFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
-
- try {
- stream = File.Create (path);
- Assert.Fail ("#1");
- } catch (DirectoryNotFoundException ex) {
- // Could not find a part of the path "..."
- Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void Create ()
- {
- FileStream stream = null;
- string path = null;
- /* positive test: create resources/foo */
- path = tmpFolder + Path.DirectorySeparatorChar + "foo";
- try {
- stream = File.Create (path);
- Assert.IsTrue (File.Exists (path), "#1");
- stream.Close ();
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- stream = null;
- /* positive test: repeat test above again to test for overwriting file */
- path = tmpFolder + Path.DirectorySeparatorChar + "foo";
- try {
- stream = File.Create (path);
- Assert.IsTrue (File.Exists (path), "#2");
- stream.Close ();
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void Copy_SourceFileName_Null ()
- {
- try {
- File.Copy (null, "b");
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Copy_DestFileName_Null ()
- {
- try {
- File.Copy ("a", null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("destFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Copy_SourceFileName_Empty ()
- {
- try {
- File.Copy (string.Empty, "b");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Copy_DestFileName_Empty ()
- {
- try {
- File.Copy ("a", string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("destFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Copy_SourceFileName_Whitespace ()
- {
- try {
- File.Copy (" ", "b");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Copy_DestFileName_Whitespace ()
- {
- try {
- File.Copy ("a", " ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Copy_SourceFileName_DoesNotExist ()
- {
- try {
- File.Copy ("doesnotexist", "b");
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual ("doesnotexist", ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- [Test]
- public void Copy_DestFileName_AlreadyExists ()
- {
- string source = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- string dest = tmpFolder + Path.DirectorySeparatorChar + "bar";
- DeleteFile (source);
- DeleteFile (dest);
- try {
- File.Create (source).Close ();
- File.Copy (source, dest);
- try {
- File.Copy (source, dest);
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The file '...' already exists.
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (dest) != -1, "#5");
- }
- } finally {
- DeleteFile (dest);
- DeleteFile (source);
- }
- }
- [Test]
- public void Copy_SourceFileName_DestFileName_Same ()
- {
- string source = tmpFolder + Path.DirectorySeparatorChar + "SameFile.txt";
- DeleteFile (source);
- try {
- // new empty file
- File.Create (source).Close ();
- try {
- File.Copy (source, source, true);
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // process cannot access file ... because it is being used by another process
- Assert.IsNull (ex.InnerException, "#2");
- Assert.IsTrue (ex.Message.IndexOf (source) != -1, "#3");
- }
- } finally {
- DeleteFile (source);
- }
- }
- [Test]
- public void Copy ()
- {
- string path1 = tmpFolder + Path.DirectorySeparatorChar + "bar";
- string path2 = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- /* positive test: copy resources/AFile.txt to resources/bar */
- try {
- DeleteFile (path1);
- DeleteFile (path2);
- File.Create (path2).Close ();
- File.Copy (path2, path1);
- Assert.IsTrue (File.Exists (path2), "#A1");
- Assert.IsTrue (File.Exists (path1), "#A2");
- Assert.IsTrue (File.Exists (path1), "#B1");
- File.Copy (path2, path1, true);
- Assert.IsTrue (File.Exists (path2), "#B2");
- Assert.IsTrue (File.Exists (path1), "#B3");
- } finally {
- DeleteFile (path1);
- DeleteFile (path2);
- }
- }
- [Test]
- public void Delete_Path_Null ()
- {
- try {
- File.Delete (null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Delete_Path_Empty ()
- {
- try {
- File.Delete (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Delete_Path_Whitespace ()
- {
- try {
- File.Delete (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Delete_Directory_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "directory_does_not_exist" + Path.DirectorySeparatorChar + "foo";
- if (Directory.Exists (path))
- Directory.Delete (path, true);
- try {
- File.Delete (path);
- Assert.Fail ("#1");
- } catch (DirectoryNotFoundException ex) {
- // Could not find a part of the path "..."
- Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- }
- [Test]
- public void Delete ()
- {
- string foopath = tmpFolder + Path.DirectorySeparatorChar + "foo";
- DeleteFile (foopath);
- try {
- File.Create (foopath).Close ();
- File.Delete (foopath);
- Assert.IsFalse (File.Exists (foopath));
- File.Delete (foopath);
- } finally {
- DeleteFile (foopath);
- }
- }
- [Test] // bug #323389
- [Category ("NotWorking")]
- public void Delete_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "DeleteOpenStreamException";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = new FileStream (path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
- try {
- File.Delete (path);
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file '...'
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- [ExpectedException (typeof(UnauthorizedAccessException))]
- public void Delete_File_ReadOnly ()
- {
- if (RunningOnUnix)
- Assert.Ignore ("ReadOnly files can be deleted on unix since fdef50957f508627928c7876a905d5584da45748.");
- string path = tmpFolder + Path.DirectorySeparatorChar + "DeleteReadOnly";
- DeleteFile (path);
- try {
- File.Create (path).Close ();
- File.SetAttributes (path, FileAttributes.ReadOnly);
- File.Delete (path);
- } finally {
- File.SetAttributes (path, FileAttributes.Normal);
- DeleteFile (path);
- }
- }
- [Test]
- public void Delete_NonExisting_NoException ()
- {
- File.Delete (Path.Combine (Directory.GetDirectoryRoot (Directory.GetCurrentDirectory ()), "monononexistingfile.dat"));
- }
- [Test]
- public void GetAttributes_Archive ()
- {
- if (RunningOnUnix)
- Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
- FileAttributes attrs;
- string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
- File.Create (path).Close ();
- attrs = File.GetAttributes (path);
- Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
- attrs &= ~FileAttributes.Archive;
- File.SetAttributes (path, attrs);
- attrs = File.GetAttributes (path);
- Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#2");
- }
- [Test]
- public void GetAttributes_Default_File ()
- {
- if (RunningOnUnix)
- Assert.Ignore ("bug #325181: FileAttributes.Archive has no effect on Unix.");
- string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
- File.Create (path).Close ();
- FileAttributes attrs = File.GetAttributes (path);
- Assert.IsTrue ((attrs & FileAttributes.Archive) != 0, "#1");
- Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
- Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
- Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
- Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
- Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
- }
- [Test]
- public void GetAttributes_Default_Directory ()
- {
- FileAttributes attrs = File.GetAttributes (tmpFolder);
- Assert.IsFalse ((attrs & FileAttributes.Archive) != 0, "#1");
- Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#2");
- Assert.IsFalse ((attrs & FileAttributes.Hidden) != 0, "#3");
- Assert.IsFalse ((attrs & FileAttributes.Normal) != 0, "#4");
- Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#5");
- Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#6");
- }
- [Test]
- public void GetAttributes_Directory ()
- {
- FileAttributes attrs = File.GetAttributes (tmpFolder);
- Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#1");
- attrs &= ~FileAttributes.Directory;
- File.SetAttributes (tmpFolder, attrs);
- Assert.IsFalse ((attrs & FileAttributes.Directory) != 0, "#2");
- string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
- File.Create (path).Close ();
- attrs = File.GetAttributes (path);
- attrs |= FileAttributes.Directory;
- File.SetAttributes (path, attrs);
- Assert.IsTrue ((attrs & FileAttributes.Directory) != 0, "#3");
- }
- [Test]
- public void GetAttributes_ReadOnly ()
- {
- FileAttributes attrs;
- string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
- File.Create (path).Close ();
- attrs = File.GetAttributes (path);
- Assert.IsFalse ((attrs & FileAttributes.ReadOnly) != 0, "#1");
- try {
- attrs |= FileAttributes.ReadOnly;
- File.SetAttributes (path, attrs);
- attrs = File.GetAttributes (path);
- Assert.IsTrue ((attrs & FileAttributes.ReadOnly) != 0, "#2");
- } finally {
- File.SetAttributes (path, FileAttributes.Normal);
- }
- }
- [Test]
- public void GetAttributes_System ()
- {
- if (RunningOnUnix)
- Assert.Ignore ("FileAttributes.System is not supported on Unix.");
- FileAttributes attrs;
- string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
- File.Create (path).Close ();
- attrs = File.GetAttributes (path);
- Assert.IsFalse ((attrs & FileAttributes.System) != 0, "#1");
- attrs |= FileAttributes.System;
- File.SetAttributes (path, FileAttributes.System);
- attrs = File.GetAttributes (path);
- Assert.IsTrue ((attrs & FileAttributes.System) != 0, "#2");
- }
- [Test]
- public void GetAttributes_Path_DoesNotExist ()
- {
- string path = Path.Combine (tmpFolder, "GetAttributes.tmp");
- try {
- File.GetAttributes (path);
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- [Test]
- public void GetAttributes_Path_Empty ()
- {
- try {
- File.GetAttributes (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetAttributes_Path_Null ()
- {
- try {
- File.GetAttributes (null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_SourceFileName_Null ()
- {
- try {
- File.Move (null, "b");
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_DestFileName_Null ()
- {
- try {
- File.Move ("a", null);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("destFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_SourceFileName_Empty ()
- {
- try {
- File.Move (string.Empty, "b");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("sourceFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_DestFileName_Empty ()
- {
- try {
- File.Move ("a", string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("destFileName", ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_SourceFileName_Whitespace ()
- {
- try {
- File.Move (" ", "b");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_DestFileName_Whitespace ()
- {
- try {
- File.Move ("a", " ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void Move_SourceFileName_DoesNotExist ()
- {
- string file = tmpFolder + Path.DirectorySeparatorChar + "doesnotexist";
- DeleteFile (file);
- try {
- File.Move (file, "b");
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (file, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- [Test]
- public void Move_DestFileName_DirectoryDoesNotExist ()
- {
- string sourceFile = tmpFolder + Path.DirectorySeparatorChar + "foo";
- string destFile = Path.Combine (Path.Combine (tmpFolder, "doesnotexist"), "b");
- DeleteFile (sourceFile);
- try {
- File.Create (sourceFile).Close ();
- try {
- File.Move (sourceFile, destFile);
- Assert.Fail ("#1");
- } catch (DirectoryNotFoundException ex) {
- // Could not find a part of the path
- Assert.AreEqual (typeof (DirectoryNotFoundException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#5");
- }
- } finally {
- DeleteFile (sourceFile);
- }
- }
- [Test]
- public void Move_DestFileName_AlreadyExists ()
- {
- string sourceFile = tmpFolder + Path.DirectorySeparatorChar + "foo";
- string destFile;
- // move to same directory
- File.Create (sourceFile).Close ();
- try {
- File.Move (sourceFile, tmpFolder);
- Assert.Fail ("#A1");
- } catch (IOException ex) {
- // Cannot create a file when that file already exists
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#A5");
- Assert.IsFalse (ex.Message.IndexOf (tmpFolder) != -1, "#A6");
- } finally {
- DeleteFile (sourceFile);
- }
- // move to exist file
- File.Create (sourceFile).Close ();
- destFile = tmpFolder + Path.DirectorySeparatorChar + "bar";
- File.Create (destFile).Close ();
- try {
- File.Move (sourceFile, destFile);
- Assert.Fail ("#B1");
- } catch (IOException ex) {
- // Cannot create a file when that file already exists
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#B5");
- Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#B6");
- } finally {
- DeleteFile (sourceFile);
- DeleteFile (destFile);
- }
- // move to existing directory
- File.Create (sourceFile).Close ();
- destFile = tmpFolder + Path.DirectorySeparatorChar + "bar";
- Directory.CreateDirectory (destFile);
- try {
- File.Move (sourceFile, destFile);
- Assert.Fail ("#C1");
- } catch (IOException ex) {
- // Cannot create a file when that file already exists
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#C2");
- Assert.IsNull (ex.InnerException, "#C3");
- Assert.IsNotNull (ex.Message, "#C4");
- Assert.IsFalse (ex.Message.IndexOf (sourceFile) != -1, "#C5");
- Assert.IsFalse (ex.Message.IndexOf (destFile) != -1, "#C6");
- } finally {
- DeleteFile (sourceFile);
- DeleteDirectory (destFile);
- }
- }
- [Test]
- public void Move ()
- {
- string bar = tmpFolder + Path.DirectorySeparatorChar + "bar";
- string baz = tmpFolder + Path.DirectorySeparatorChar + "baz";
- if (!File.Exists (bar)) {
- FileStream f = File.Create(bar);
- f.Close();
- }
-
- Assert.IsTrue (File.Exists (bar), "#1");
- File.Move (bar, baz);
- Assert.IsFalse (File.Exists (bar), "#2");
- Assert.IsTrue (File.Exists (baz), "#3");
- // Test moving of directories
- string dir = Path.Combine (tmpFolder, "dir");
- string dir2 = Path.Combine (tmpFolder, "dir2");
- string dir_foo = Path.Combine (dir, "foo");
- string dir2_foo = Path.Combine (dir2, "foo");
- if (Directory.Exists (dir))
- Directory.Delete (dir, true);
- Directory.CreateDirectory (dir);
- Directory.CreateDirectory (dir2);
- File.Create (dir_foo).Close ();
- File.Move (dir_foo, dir2_foo);
- Assert.IsTrue (File.Exists (dir2_foo), "#4");
-
- Directory.Delete (dir, true);
- Directory.Delete (dir2, true);
- DeleteFile (dir_foo);
- DeleteFile (dir2_foo);
- }
- [Test]
- public void Move_FileLock ()
- {
- string sourceFile = Path.GetTempFileName ();
- string destFile = Path.GetTempFileName ();
- // source file locked
- using (File.Open (sourceFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
- try {
- File.Move (sourceFile, destFile);
- Assert.Fail ("#A1");
- } catch (IOException ex) {
- // The process cannot access the file because
- // it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#A2");
- Assert.IsNull (ex.InnerException, "#A3");
- Assert.IsNotNull (ex.Message, "#A4");
- }
- }
- // destination file locked
- using (File.Open (destFile, FileMode.Open, FileAccess.ReadWrite, FileShare.None)) {
- try {
- File.Move (sourceFile, destFile);
- Assert.Fail ("#B1");
- } catch (IOException ex) {
- // The process cannot access the file because
- // it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#B2");
- Assert.IsNull (ex.InnerException, "#B3");
- Assert.IsNotNull (ex.Message, "#B4");
- }
- }
- }
- [Test]
- public void Open ()
- {
- string path = null;
- FileStream stream = null;
- path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- try {
- if (!File.Exists (path))
- stream = File.Create (path);
- stream.Close ();
- stream = File.Open (path, FileMode.Open);
- stream.Close ();
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- stream = null;
- if (!File.Exists (path))
- File.Create (path).Close ();
- try {
- stream = File.Open (path, FileMode.Open);
- Assert.IsTrue (stream.CanRead, "#A1");
- Assert.IsTrue (stream.CanSeek, "#A2");
- Assert.IsTrue (stream.CanWrite, "#A3");
- stream.Close ();
- stream = File.Open (path, FileMode.Open, FileAccess.Write);
- Assert.IsFalse (stream.CanRead, "#B1");
- Assert.IsTrue (stream.CanSeek, "#B2");
- Assert.IsTrue (stream.CanWrite, "#B3");
- stream.Close ();
- stream = File.Open (path, FileMode.Open, FileAccess.Read);
- Assert.IsTrue (stream.CanRead, "#C1");
- Assert.IsTrue (stream.CanSeek, "#C2");
- Assert.IsFalse (stream.CanWrite, "#C3");
- stream.Close ();
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- stream = null;
- /* Exception tests */
- path = tmpFolder + Path.DirectorySeparatorChar + "filedoesnotexist";
- try {
- stream = File.Open (path, FileMode.Open);
- Assert.Fail ("#D1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#D2");
- Assert.AreEqual (path, ex.FileName, "#D3");
- Assert.IsNull (ex.InnerException, "#D4");
- Assert.IsNotNull (ex.Message, "#D5");
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void Open_CreateNewMode_ReadAccess ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- FileStream stream = null;
- try {
- stream = File.Open (tmpFolder + Path.DirectorySeparatorChar + "AFile.txt", FileMode.CreateNew, FileAccess.Read);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Combining FileMode: CreateNew with FileAccess: Read is invalid
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void Open_AppendMode_ReadAccess ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- FileStream s = null;
- if (!File.Exists (path))
- File.Create (path).Close ();
- try {
- s = File.Open (path, FileMode.Append, FileAccess.Read);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Combining FileMode: Append with FileAccess: Read is invalid
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- } finally {
- if (s != null)
- s.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void OpenRead ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- if (!File.Exists (path))
- File.Create (path).Close ();
- FileStream stream = null;
-
- try {
- stream = File.OpenRead (path);
- Assert.IsTrue (stream.CanRead, "#1");
- Assert.IsTrue (stream.CanSeek, "#2");
- Assert.IsFalse (stream.CanWrite, "#3");
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void OpenWrite ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "AFile.txt";
- if (!File.Exists (path))
- File.Create (path).Close ();
- FileStream stream = null;
- try {
- stream = File.OpenWrite (path);
- Assert.IsFalse (stream.CanRead, "#1");
- Assert.IsTrue (stream.CanSeek, "#2");
- Assert.IsTrue (stream.CanWrite, "#3");
- stream.Close ();
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void TestGetCreationTime ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "baz";
- DeleteFile (path);
- try {
- File.Create (path).Close();
- DateTime time = File.GetCreationTime (path);
- Assert.IsTrue ((DateTime.Now - time).TotalSeconds < 10);
- } finally {
- DeleteFile (path);
- }
- }
- [Test]
- public void CreationTime ()
- {
- if (RunningOnUnix)
- Assert.Ignore ("Setting the creation time on Unix is not possible.");
- string path = Path.GetTempFileName ();
- try {
- File.SetCreationTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
- DateTime time = File.GetCreationTime (path);
- Assert.AreEqual (2002, time.Year, "#A1");
- Assert.AreEqual (4, time.Month, "#A2");
- Assert.AreEqual (6, time.Day, "#A3");
- Assert.AreEqual (4, time.Hour, "#A4");
- Assert.AreEqual (4, time.Second, "#A5");
- time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetCreationTimeUtc (path));
- Assert.AreEqual (2002, time.Year, "#B1");
- Assert.AreEqual (4, time.Month, "#B2");
- Assert.AreEqual (6, time.Day, "#B3");
- Assert.AreEqual (4, time.Hour, "#B4");
- Assert.AreEqual (4, time.Second, "#B5");
- File.SetCreationTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
- time = File.GetCreationTimeUtc (path);
- Assert.AreEqual (2002, time.Year, "#C1");
- Assert.AreEqual (4, time.Month, "#C2");
- Assert.AreEqual (6, time.Day, "#C3");
- Assert.AreEqual (4, time.Hour, "#C4");
- Assert.AreEqual (4, time.Second, "#C5");
- time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetCreationTime (path));
- Assert.AreEqual (2002, time.Year, "#D1");
- Assert.AreEqual (4, time.Month, "#D2");
- Assert.AreEqual (6, time.Day, "#D3");
- Assert.AreEqual (4, time.Hour, "#D4");
- Assert.AreEqual (4, time.Second, "#D5");
- } finally {
- DeleteFile (path);
- }
- }
- [Test]
- public void LastAccessTime ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "lastAccessTime";
- if (File.Exists (path))
- File.Delete (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- stream.Close ();
- File.SetLastAccessTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
- DateTime time = File.GetLastAccessTime (path);
- Assert.AreEqual (2002, time.Year, "#A1");
- Assert.AreEqual (4, time.Month, "#A2");
- Assert.AreEqual (6, time.Day, "#A3");
- Assert.AreEqual (4, time.Hour, "#A4");
- Assert.AreEqual (4, time.Second, "#A5");
- time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastAccessTimeUtc (path));
- Assert.AreEqual (2002, time.Year, "#B1");
- Assert.AreEqual (4, time.Month, "#B2");
- Assert.AreEqual (6, time.Day, "#B3");
- Assert.AreEqual (4, time.Hour, "#B4");
- Assert.AreEqual (4, time.Second, "#B5");
- File.SetLastAccessTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
- time = File.GetLastAccessTimeUtc (path);
- Assert.AreEqual (2002, time.Year, "#C1");
- Assert.AreEqual (4, time.Month, "#C2");
- Assert.AreEqual (6, time.Day, "#C3");
- Assert.AreEqual (4, time.Hour, "#C4");
- Assert.AreEqual (4, time.Second, "#C5");
- time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastAccessTime (path));
- Assert.AreEqual (2002, time.Year, "#D1");
- Assert.AreEqual (4, time.Month, "#D2");
- Assert.AreEqual (6, time.Day, "#D3");
- Assert.AreEqual (4, time.Hour, "#D4");
- Assert.AreEqual (4, time.Second, "#D5");
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void LastWriteTime ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "lastWriteTime";
- if (File.Exists (path))
- File.Delete (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- stream.Close ();
- File.SetLastWriteTime (path, new DateTime (2002, 4, 6, 4, 6, 4));
- DateTime time = File.GetLastWriteTime (path);
- Assert.AreEqual (2002, time.Year, "#A1");
- Assert.AreEqual (4, time.Month, "#A2");
- Assert.AreEqual (6, time.Day, "#A3");
- Assert.AreEqual (4, time.Hour, "#A4");
- Assert.AreEqual (4, time.Second, "#A5");
- time = TimeZone.CurrentTimeZone.ToLocalTime (File.GetLastWriteTimeUtc (path));
- Assert.AreEqual (2002, time.Year, "#B1");
- Assert.AreEqual (4, time.Month, "#B2");
- Assert.AreEqual (6, time.Day, "#B3");
- Assert.AreEqual (4, time.Hour, "#B4");
- Assert.AreEqual (4, time.Second, "#B5");
- File.SetLastWriteTimeUtc (path, new DateTime (2002, 4, 6, 4, 6, 4));
- time = File.GetLastWriteTimeUtc (path);
- Assert.AreEqual (2002, time.Year, "#C1");
- Assert.AreEqual (4, time.Month, "#C2");
- Assert.AreEqual (6, time.Day, "#C3");
- Assert.AreEqual (4, time.Hour, "#C4");
- Assert.AreEqual (4, time.Second, "#C5");
- time = TimeZone.CurrentTimeZone.ToUniversalTime (File.GetLastWriteTime (path));
- Assert.AreEqual (2002, time.Year, "#D1");
- Assert.AreEqual (4, time.Month, "#D2");
- Assert.AreEqual (6, time.Day, "#D3");
- Assert.AreEqual (4, time.Hour, "#D4");
- Assert.AreEqual (4, time.Second, "#D5");
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void GetCreationTime_Path_Null ()
- {
- try {
- File.GetCreationTime (null as string);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetCreationTime_Path_Empty ()
- {
- try {
- File.GetCreationTime (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
-
- [Test]
- public void GetCreationTime_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "GetCreationTimeException3";
- DeleteFile (path);
- DateTime time = File.GetCreationTime (path);
- DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
- Assert.AreEqual (expectedTime.Year, time.Year, "#1");
- Assert.AreEqual (expectedTime.Month, time.Month, "#2");
- Assert.AreEqual (expectedTime.Day, time.Day, "#3");
- Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
- Assert.AreEqual (expectedTime.Second, time.Second, "#5");
- Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
- }
- [Test]
- public void GetCreationTime_Path_Whitespace ()
- {
- try {
- File.GetCreationTime (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetCreationTime_Path_InvalidPathChars ()
- {
- try {
- File.GetCreationTime (Path.InvalidPathChars [0].ToString ());
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetCreationTimeUtc_Path_Null ()
- {
- try {
- File.GetCreationTimeUtc (null as string);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetCreationTimeUtc_Path_Empty ()
- {
- try {
- File.GetCreationTimeUtc (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
-
- [Test]
- public void GetCreationTimeUtc_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "GetCreationTimeUtcException3";
- DeleteFile (path);
- DateTime time = File.GetCreationTimeUtc (path);
- Assert.AreEqual (1601, time.Year, "#1");
- Assert.AreEqual (1, time.Month, "#2");
- Assert.AreEqual (1, time.Day, "#3");
- Assert.AreEqual (0, time.Hour, "#4");
- Assert.AreEqual (0, time.Second, "#5");
- Assert.AreEqual (0, time.Millisecond, "#6");
- }
- [Test]
- public void GetCreationTimeUtc_Path_Whitespace ()
- {
- try {
- File.GetCreationTimeUtc (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetCreationTimeUtc_Path_InvalidPathChars ()
- {
- try {
- File.GetCreationTimeUtc (Path.InvalidPathChars [0].ToString ());
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastAccessTime_Path_Null ()
- {
- try {
- File.GetLastAccessTime (null as string);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastAccessTime_Path_Empty ()
- {
- try {
- File.GetLastAccessTime (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
-
- [Test]
- public void GetLastAccessTime_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeException3";
- DeleteFile (path);
- DateTime time = File.GetLastAccessTime (path);
- DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
- Assert.AreEqual (expectedTime.Year, time.Year, "#1");
- Assert.AreEqual (expectedTime.Month, time.Month, "#2");
- Assert.AreEqual (expectedTime.Day, time.Day, "#3");
- Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
- Assert.AreEqual (expectedTime.Second, time.Second, "#5");
- Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
- }
- [Test]
- public void GetLastAccessTime_Path_Whitespace ()
- {
- try {
- File.GetLastAccessTime (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastAccessTime_Path_InvalidPathChars ()
- {
- try {
- File.GetLastAccessTime (Path.InvalidPathChars [0].ToString ());
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastAccessTimeUtc_Path_Null ()
- {
- try {
- File.GetLastAccessTimeUtc (null as string);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastAccessTimeUtc_Path_Empty ()
- {
- try {
- File.GetLastAccessTimeUtc (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
-
- [Test]
- public void GetLastAccessTimeUtc_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
- DeleteFile (path);
- DateTime time = File.GetLastAccessTimeUtc (path);
- Assert.AreEqual (1601, time.Year, "#1");
- Assert.AreEqual (1, time.Month, "#2");
- Assert.AreEqual (1, time.Day, "#3");
- Assert.AreEqual (0, time.Hour, "#4");
- Assert.AreEqual (0, time.Second, "#5");
- Assert.AreEqual (0, time.Millisecond, "#6");
- }
- [Test]
- public void GetLastAccessTimeUtc_Path_Whitespace ()
- {
- try {
- File.GetLastAccessTimeUtc (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastAccessTimeUtc_Path_InvalidPathChars ()
- {
- try {
- File.GetLastAccessTimeUtc (Path.InvalidPathChars [0].ToString ());
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastWriteTime_Path_Null ()
- {
- try {
- File.GetLastWriteTime (null as string);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastWriteTime_Path_Empty ()
- {
- try {
- File.GetLastWriteTime (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
-
- [Test]
- public void GetLastWriteTime_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastAccessTimeUtcException3";
- DeleteFile (path);
- DateTime time = File.GetLastWriteTime (path);
- DateTime expectedTime = (new DateTime (1601, 1, 1)).ToLocalTime ();
- Assert.AreEqual (expectedTime.Year, time.Year, "#1");
- Assert.AreEqual (expectedTime.Month, time.Month, "#2");
- Assert.AreEqual (expectedTime.Day, time.Day, "#3");
- Assert.AreEqual (expectedTime.Hour, time.Hour, "#4");
- Assert.AreEqual (expectedTime.Second, time.Second, "#5");
- Assert.AreEqual (expectedTime.Millisecond, time.Millisecond, "#6");
- }
- [Test]
- public void GetLastWriteTime_Path_Whitespace ()
- {
- try {
- File.GetLastWriteTime (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastWriteTime_Path_InvalidPathChars ()
- {
- try {
- File.GetLastWriteTime (Path.InvalidPathChars [0].ToString ());
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastWriteTimeUtc_Path_Null ()
- {
- try {
- File.GetLastWriteTimeUtc (null as string);
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastWriteTimeUtc_Path_Empty ()
- {
- try {
- File.GetLastWriteTimeUtc (string.Empty);
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
-
- [Test]
- public void GetLastWriteTimeUtc_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "GetLastWriteTimeUtcException3";
- DeleteFile (path);
- DateTime time = File.GetLastWriteTimeUtc (path);
- Assert.AreEqual (1601, time.Year, "#1");
- Assert.AreEqual (1, time.Month, "#2");
- Assert.AreEqual (1, time.Day, "#3");
- Assert.AreEqual (0, time.Hour, "#4");
- Assert.AreEqual (0, time.Second, "#5");
- Assert.AreEqual (0, time.Millisecond, "#6");
- }
- [Test]
- public void GetLastWriteTimeUtc_Path_Whitespace ()
- {
- try {
- File.GetLastWriteTimeUtc (" ");
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void GetLastWriteTimeUtc_Path_InvalidPathChars ()
- {
- try {
- File.GetLastWriteTimeUtc (Path.InvalidPathChars [0].ToString ());
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void FileStreamClose ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "FileStreamClose";
- FileStream stream = null;
- try {
- stream = File.Create (path);
- stream.Close ();
- File.Delete (path);
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
-
- // SetCreationTime and SetCreationTimeUtc exceptions
- [Test]
- public void SetCreationTime_Path_Null ()
- {
- try {
- File.SetCreationTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCreationTime_Path_Empty ()
- {
- try {
- File.SetCreationTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCreationTime_Path_Whitespace ()
- {
- try {
- File.SetCreationTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCreationTime_Path_InvalidPathChars ()
- {
- // On Unix there are no invalid path chars.
- if (Path.InvalidPathChars.Length > 1) {
- try {
- File.SetCreationTime (Path.InvalidPathChars [1].ToString (),
- new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- }
- [Test]
- public void SetCreationTime_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeFileNotFoundException1";
- DeleteFile (path);
-
- try {
- File.SetCreationTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- // [Test]
- // [ExpectedException(typeof (ArgumentOutOfRangeException))]
- // public void SetCreationTimeArgumentOutOfRangeException1 ()
- // {
- // string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeArgumentOutOfRangeException1";
- // FileStream stream = null;
- // DeleteFile (path);
- // try {
- // stream = File.Create (path);
- // stream.Close ();
- // File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
- // } finally {
- // if (stream != null)
- // stream.Close ();
- // DeleteFile (path);
- // }
- // }
- [Test]
- public void SetCreationTime_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "CreationTimeIOException1";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- try {
- File.SetCreationTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file '...'
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void SetCreationTimeUtc_Path_Null ()
- {
- try {
- File.SetCreationTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCreationTimeUtc_Path_Empty ()
- {
- try {
- File.SetCreationTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCreationTimeUtc_Path_Whitespace ()
- {
- try {
- File.SetCreationTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCreationTimeUtc_Path_InvalidPathChars ()
- {
- // On Unix there are no invalid path chars.
- if (Path.InvalidPathChars.Length > 1) {
- try {
- File.SetCreationTimeUtc (Path.InvalidPathChars [1].ToString (),
- new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- }
- [Test]
- public void SetCreationTimeUtc_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcFileNotFoundException1";
- DeleteFile (path);
- try {
- File.SetCreationTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- }
- }
- // [Test]
- // [ExpectedException(typeof (ArgumentOutOfRangeException))]
- // public void SetCreationTimeUtcArgumentOutOfRangeException1 ()
- // {
- // string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcArgumentOutOfRangeException1";
- // DeleteFile (path);
- // FileStream stream = null;
- // try {
- // stream = File.Create (path);
- // stream.Close ();
- // File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
- // } finally {
- // if (stream != null)
- // stream.Close();
- // DeleteFile (path);
- // }
- // }
- [Test]
- public void SetCreationTimeUtc_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetCreationTimeUtcIOException1";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- try {
- File.SetCreationTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file "..."
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- // SetLastAccessTime and SetLastAccessTimeUtc exceptions
- [Test]
- public void SetLastAccessTime_Path_Null ()
- {
- try {
- File.SetLastAccessTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastAccessTime_Path_Empty ()
- {
- try {
- File.SetLastAccessTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastAccessTime_Path_Whitespace ()
- {
- try {
- File.SetLastAccessTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastAccessTime_Path_InvalidPathChars ()
- {
- // On Unix there are no invalid path chars.
- if (Path.InvalidPathChars.Length > 1) {
- try {
- File.SetLastAccessTime (Path.InvalidPathChars [1].ToString (),
- new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- }
- [Test]
- public void SetLastAccessTime_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeFileNotFoundException1";
- DeleteFile (path);
- try {
- File.SetLastAccessTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- // [Test]
- // [ExpectedException(typeof (ArgumentOutOfRangeException))]
- // public void SetLastAccessTimeArgumentOutOfRangeException1 ()
- // {
- // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastTimeArgumentOutOfRangeException1";
- // DeleteFile (path);
- // FileStream stream = null;
- // try {
- // stream = File.Create (path);
- // stream.Close ();
- // File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
- // } finally {
- // if (stream != null)
- // stream.Close ();
- // DeleteFile (path);
- // }
- // }
- [Test]
- public void SetLastAccessTime_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "LastAccessIOException1";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- try {
- File.SetLastAccessTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file "..."
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void SetLastAccessTimeUtc_Path_Null ()
- {
- try {
- File.SetLastAccessTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetCLastAccessTimeUtc_Path_Empty ()
- {
- try {
- File.SetLastAccessTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastAccessTimeUtc_Path_Whitespace ()
- {
- try {
- File.SetLastAccessTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastAccessTimeUtc_Path_InvalidPathChars ()
- {
- // On Unix there are no invalid path chars.
- if (Path.InvalidPathChars.Length > 1) {
- try {
- File.SetLastAccessTimeUtc (Path.InvalidPathChars [1].ToString (),
- new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- }
- [Test]
- public void SetLastAccessTimeUtc_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcFileNotFoundException1";
- DeleteFile (path);
- try {
- File.SetLastAccessTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- // [Test]
- // [ExpectedException(typeof (ArgumentOutOfRangeException))]
- // public void SetLastAccessTimeUtcArgumentOutOfRangeException1 ()
- // {
- // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcArgumentOutOfRangeException1";
- // DeleteFile (path);
- // FileStream stream = null;
- // try {
- // stream = File.Create (path);
- // stream.Close ();
- // File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
- // } finally {
- // if (stream != null)
- // stream.Close ();
- // DeleteFile (path);
- // }
- // }
- [Test]
- public void SetLastAccessTimeUtc_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastAccessTimeUtcIOException1";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- try {
- File.SetLastAccessTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file "..."
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- // SetLastWriteTime and SetLastWriteTimeUtc exceptions
- [Test]
- public void SetLastWriteTime_Path_Null ()
- {
- try {
- File.SetLastWriteTime (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastWriteTime_Path_Empty ()
- {
- try {
- File.SetLastWriteTime (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastWriteTime_Path_Whitespace ()
- {
- try {
- File.SetLastWriteTime (" ", new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastWriteTime_Path_InvalidPathChars ()
- {
- // On Unix there are no invalid path chars.
- if (Path.InvalidPathChars.Length > 1) {
- try {
- File.SetLastWriteTime (Path.InvalidPathChars [1].ToString (),
- new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- }
- [Test]
- public void SetLastWriteTime_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeFileNotFoundException1";
- DeleteFile (path);
- try {
- File.SetLastWriteTime (path, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- // [Test]
- // [ExpectedException(typeof (ArgumentOutOfRangeException))]
- // public void SetLastWriteTimeArgumentOutOfRangeException1 ()
- // {
- // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeArgumentOutOfRangeException1";
- // DeleteFile (path);
- // FileStream stream = null;
- // try {
- // stream = File.Create (path);
- // stream.Close ();
- // File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
- // } finally {
- // if (stream != null)
- // stream.Close ();
- // DeleteFile (path);
- // }
- // }
- [Test]
- public void SetLastWriteTime_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "LastWriteTimeIOException1";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- try {
- File.SetLastWriteTime (path, new DateTime (1000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file '...'
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
- [Test]
- public void SetLastWriteTimeUtc_Path_Null ()
- {
- try {
- File.SetLastWriteTimeUtc (null as string, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentNullException ex) {
- Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.AreEqual ("path", ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastWriteTimeUtc_Path_Empty ()
- {
- try {
- File.SetLastWriteTimeUtc (string.Empty, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Empty file name is not legal
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastWriteTimeUtc_Path_Whitespace ()
- {
- try {
- File.SetLastWriteTimeUtc (" ", new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // The path is not of a legal form
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- [Test]
- public void SetLastWriteTimeUtc_Path_InvalidPathChars ()
- {
- // On Unix there are no invalid path chars.
- if (Path.InvalidPathChars.Length > 1) {
- try {
- File.SetLastWriteTimeUtc (Path.InvalidPathChars [1].ToString (),
- new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (ArgumentException ex) {
- // Illegal characters in path
- Assert.AreEqual (typeof (ArgumentException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsNull (ex.ParamName, "#5");
- }
- }
- }
- [Test]
- public void SetLastWriteTimeUtc_Path_DoesNotExist ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcFileNotFoundException1";
- DeleteFile (path);
- try {
- File.SetLastWriteTimeUtc (path, new DateTime (2000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (FileNotFoundException ex) {
- Assert.AreEqual (typeof (FileNotFoundException), ex.GetType (), "#2");
- Assert.AreEqual (path, ex.FileName, "#3");
- Assert.IsNull (ex.InnerException, "#4");
- Assert.IsNotNull (ex.Message, "#5");
- }
- }
- // [Test]
- // [ExpectedException(typeof (ArgumentOutOfRangeException))]
- // public void SetLastWriteTimeUtcArgumentOutOfRangeException1 ()
- // {
- // string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcArgumentOutOfRangeException1";
- // DeleteFile (path);
- // FileStream stream = null;
- // try {
- // stream = File.Create (path);
- // stream.Close ();
- // File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
- // } finally {
- // if (stream != null)
- // stream.Close ();
- // DeleteFile (path);
- // }
- // }
- [Test]
- public void SetLastWriteTimeUtc_FileLock ()
- {
- string path = tmpFolder + Path.DirectorySeparatorChar + "SetLastWriteTimeUtcIOException1";
- DeleteFile (path);
- FileStream stream = null;
- try {
- stream = File.Create (path);
- try {
- File.SetLastWriteTimeUtc (path, new DateTime (1000, 12, 12, 11, 59, 59));
- Assert.Fail ("#1");
- } catch (IOException ex) {
- // The process cannot access the file '...'
- // because it is being used by another process
- Assert.AreEqual (typeof (IOException), ex.GetType (), "#2");
- Assert.IsNull (ex.InnerException, "#3");
- Assert.IsNotNull (ex.Message, "#4");
- Assert.IsTrue (ex.Message.IndexOf (path) != -1, "#5");
- }
- } finally {
- if (stream != null)
- stream.Close ();
- DeleteFile (path);
- }
- }
-
- [Test]
- public void OpenAppend ()
- {
- string fn = Path.GetTempFileName ();
- using (FileStream s = File.Open (fn, FileMode.Append)) {
- }
- DeleteFile (fn);
- }
- void Position (long value)
- {
- using (FileStream fs = File.OpenRead (testfile)) {
- fs.Position = value;
- Assert.AreEqual (value, fs.Position, "Position");
- Assert.AreEqual (1, fs.Length, "Length");
- }
- }
-
- [Test]
- public void Position_Small ()
- {
- Position (Int32.MaxValue);
- }
- [Test]
- [Category ("LargeFileSupport")]
- public void Position_Large ()
- {
- // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
- Position ((long) Int32.MaxValue + 1);
- }
-
- void Seek (long value)
- {
- using (FileStream fs = File.OpenRead (testfile)) {
- fs.Seek (value, SeekOrigin.Begin);
- Assert.AreEqual (value, fs.Position, "Position");
- Assert.AreEqual (1, fs.Length, "Length");
- }
- }
-
- [Test]
- public void Seek_Small ()
- {
- Seek (Int32.MaxValue);
- }
- [Test]
- [Category ("LargeFileSupport")]
- public void Seek_Large ()
- {
- // fails if HAVE_LARGE_FILE_SUPPORT is not enabled in device builds
- Seek ((long) Int32.MaxValue + 1);
- }
-
- void LockUnlock (long value)
- {
- using (FileStream fs = new FileStream (testfile, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)) {
- fs.Lock (value - 1, 1);
- fs.Unlock (value - 1, 1);
-
- fs.Lock (0, value);
- fs.Unlock (0, value);
- }
- }
-
- [Test]
- public void Lock_Small ()
- {
- LockUnlock ((long) Int32.MaxValue);
- }
- [Test]
- [Category ("LargeFileSupport")]
- public void Lock_Large ()
- {
- // note: already worked without HAVE_LARGE_FILE_SUPPORT
- LockUnlock ((long) Int32.MaxValue + 1);
- }
-
- [Test]
- public void ReadWriteAllText ()
- {
- // The MSDN docs said something about
- // not including a final new line. it looks
- // like that was not true. I'm not sure what
- // that was talking about
- read_all (string.Empty);
- read_all ("\r");
- read_all ("\n");
- read_all ("\r\n");
- read_all ("a\r");
- read_all ("a\n");
- read_all ("a\r\n");
- read_all ("a\ra");
- read_all ("a\na");
- read_all ("a\r\na");
- read_all ("a");
- read_all ("\r\r");
- read_all ("\n\n");
- read_all ("\r\n\r\n");
- }
- [Test]
- public void ReplaceTest ()
- {
- string tmp = Path.Combine (tmpFolder, "ReplaceTest");
- Directory.CreateDirectory (tmp);
- string origFile = Path.Combine (tmp, "origFile");
- string replaceFile = Path.Combine (tmp, "replaceFile");
- string backupFile = Path.Combine (tmp, "backupFile");
- using (StreamWriter sw = File.CreateText (origFile)) {
- sw.WriteLine ("origFile");
- }
- using (StreamWriter sw = File.CreateText (replaceFile)) {
- sw.WriteLine ("replaceFile");
- }
- using (StreamWriter sw = File.CreateText (backupFile)) {
- sw.WriteLine ("backupFile");
- }
- File.Replace (origFile, replaceFile, backupFile);
- Assert.IsFalse (File.Exists (origFile), "#1");
- using (StreamReader sr = File.OpenText (replaceFile)) {
- string txt = sr.ReadLine ();
- Assert.AreEqual ("origFile", txt, "#2");
- }
- using (StreamReader sr = File.OpenText (backupFile)) {
- string txt = sr.ReadLine ();
- Assert.AreEqual ("replaceFile", txt, "#3");
- }
- }
- static bool RunningOnUnix {
- get {
- int p = (int) Environment.OSVersion.Platform;
- return ((p == 4) || (p == 128) || (p == 6));
- }
- }
- void DeleteFile (string path)
- {
- if (File.Exists (path))
- File.Delete (path);
- }
- void DeleteDirectory (string path)
- {
- if (Directory.Exists (path))
- Directory.Delete (path, true);
- }
- void read_all (string s)
- {
- string f = Path.GetTempFileName ();
- try {
- File.WriteAllText (f, s);
- string r = File.ReadAllText (f);
- Assert.AreEqual (s, r);
- } finally {
- DeleteFile (f);
- }
- }
- void MoveTest (FileAccess acc, FileShare share, bool works)
- {
- // use TEMP so since the default location (right along with the assemblies)
- // will get access denied when running under some environment (e.g. iOS devices)
- var file = Path.Combine (Path.GetTempPath (), "kk597rfdnllh89");
- File.Delete (file + ".old");
- using (var v = File.Create (file)) { }
- using (var stream = new FileStream(file, FileMode.Open, acc, share, 4096, FileOptions.SequentialScan)) {
- try {
- File.Move(file, file + ".old");
- if (!works)
- Assert.Fail ("Move with ({0}) and ({1}) did not fail", acc, share);
- } catch (IOException) {
- if (works)
- Assert.Fail ("Move with ({0}) and ({1}) did fail", acc, share);
- }
- }
- }
- [Test]
- public void MoveTest ()
- {
- MoveTest (FileAccess.Read, FileShare.None, false);
- MoveTest (FileAccess.Read, FileShare.Read, false);
- MoveTest (FileAccess.Read, FileShare.Write, false);
- MoveTest (FileAccess.Read, FileShare.ReadWrite, false);
- MoveTest (FileAccess.Read, FileShare.Delete, true);
- MoveTest (FileAccess.Read, FileShare.Read | FileShare.Delete, true);
- MoveTest (FileAccess.Read, FileShare.Write | FileShare.Delete, true);
- MoveTest (FileAccess.Read, FileShare.ReadWrite | FileShare.Delete, true);
- MoveTest (FileAccess.Write, FileShare.None, false);
- MoveTest (FileAccess.Write, FileShare.Read, false);
- MoveTest (FileAccess.Write, FileShare.Write, false);
- MoveTest (FileAccess.Write, FileShare.ReadWrite, false);
- MoveTest (FileAccess.Write, FileShare.Delete, true);
- MoveTest (FileAccess.Write, FileShare.Read | FileShare.Delete, true);
- MoveTest (FileAccess.Write, FileShare.Write | FileShare.Delete, true);
- MoveTest (FileAccess.Write, FileShare.ReadWrite | FileShare.Delete, true);
- MoveTest (FileAccess.ReadWrite, FileShare.None, false);
- MoveTest (FileAccess.ReadWrite, FileShare.Read, false);
- MoveTest (FileAccess.ReadWrite, FileShare.Write, false);
- MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite, false);
- MoveTest (FileAccess.ReadWrite, FileShare.Delete, true);
- MoveTest (FileAccess.ReadWrite, FileShare.Read | FileShare.Delete, true);
- MoveTest (FileAccess.ReadWrite, FileShare.Write | FileShare.Delete, true);
- MoveTest (FileAccess.ReadWrite, FileShare.ReadWrite | FileShare.Delete, true);
- }
- [DllImport ("libc", SetLastError=true)]
- public static extern int symlink (string oldpath, string newpath);
- [Test]
- #if __TVOS__
- [Ignore ("See bug #59239")]
- #endif
- public void SymLinkLoop ()
- {
- if (!RunningOnUnix)
- Assert.Ignore ("Symlink are hard on windows");
- var name1 = Path.GetRandomFileName ();
- var name2 = Path.GetRandomFileName ();
- var path1 = Path.Combine (Path.GetTempPath (), name1);
- var path2 = Path.Combine (Path.GetTempPath (), name2);
- File.Delete (path1);
- File.Delete (path2);
- try {
- if (symlink (path1, path2) != 0)
- Assert.Fail ("symlink #1 failed with errno={0}", Marshal.GetLastWin32Error ());
- if (symlink (path2, path1) != 0)
- Assert.Fail ("symlink #2 failed with errno={0}", Marshal.GetLastWin32Error ());
- Assert.IsTrue (File.Exists (path1), "File.Exists must return true for path1 symlink loop");
- Assert.IsTrue (File.Exists (path2), "File.Exists must return true for path2 symlink loop");
- try {
- using (var f = File.Open (path1, FileMode.Open, FileAccess.Read)) {
- Assert.Fail ("File.Open must throw for symlink loops");
- }
- } catch (IOException ex) {
- Assert.AreEqual (0x80070781u, (uint)ex.HResult, "Ensure HRESULT is correct");
- }
- File.Delete (path1); //Delete must not throw and must work
- Assert.IsFalse (File.Exists (path1), "File.Delete must delete symlink loops");
- } finally {
- try {
- File.Delete (path1);
- File.Delete (path2);
- } catch (IOException) {
- //Don't double fault any exception from the tests.
- }
- }
- }
- }
- }
|