本文整理汇总了C#中FileInfo.OpenWrite方法的典型用法代码示例。如果您正苦于以下问题:C# FileInfo.OpenWrite方法的具体用法?C# FileInfo.OpenWrite怎么用?C# FileInfo.OpenWrite使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类FileInfo
的用法示例。
在下文中一共展示了FileInfo.OpenWrite方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Start
// Use this for initialization
void Start()
{
FileStream file = null;
FileInfo fileInfo = null;
try
{
fileInfo = new FileInfo("file.txt");
file = fileInfo.OpenWrite();
for(int i = 0; i < 255; i++)
{
file.WriteByte((byte)i);
}
throw new ArgumentNullException("Something bad happened.");
}
catch(UnauthorizedAccessException e)
{
Debug.LogWarning(e.Message);
}
catch (ArgumentNullException e)
{
//Debug.LogWarning(e.Message);
Debug.LogWarning("BAD!!!");
}
finally
{
if (file != null)
file.Close();
}
}
示例2: WriteLogFile
public static void WriteLogFile(String input, string filePath)
{
//定义文件信息对象
FileInfo finfo = new FileInfo(filePath);
//判断文件是否存在以及是否大于2M
if (finfo.Exists && finfo.Length > 2047500)
{
//删除该文件
finfo.Delete();
}
//创建只写文件流
using (FileStream fs = finfo.OpenWrite())
{
//根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
//设置写数据流的起始位置为文件流的末尾
w.BaseStream.Seek(0, SeekOrigin.End);
//写入“Log Entry : ”
w.Write("\r\nLog Entry : ");
//写入当前系统时间并换行
w.Write("{0} {1} \r\n", DateTime.Now.ToLongTimeString(),
DateTime.Now.ToLongDateString());
//写入日志内容并换行
w.Write(input + "\r\n");
//写入------------------------------------“并换行
w.Write("------------------------------------\r\n");
//清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush();
//关闭写数据流
w.Close();
}
}
示例3: Export
public virtual bool Export(string key, FileInfo fileInfo)
{
var callingAssembly = Assembly.GetCallingAssembly();
fileInfo.Directory.Create();
try
{
if (fileInfo.Exists)
{
fileInfo.IsReadOnly = false;
fileInfo.Delete();
}
using (var resource = callingAssembly.GetManifestResourceStream(string.Format("{0}.Resources.{1}", callingAssembly.GetName().Name, key)))
using (var stream = fileInfo.OpenWrite())
{
resource.CopyTo(stream);
}
return true;
}
catch (IOException)
{
return false;
}
catch (UnauthorizedAccessException)
{
return false;
}
catch (Exception)
{
//TODO: log
return false;
}
}
示例4: Start
//////////////////////////////////////////////////////
///// TRY and CATCH SCRIPT /////
////////////////////////////////////////////////////
// Use this for initialization
void Start()
{
FileStream file = null;
FileInfo fileInfo = null;
try
{
fileInfo = new FileInfo("C:\\file.txt"); // Creates a file at said location
file = fileInfo.OpenWrite();
for(int i = 0; i < 255; i++)
file.WriteByte((byte)i);
throw new ArgumentNullException("Something Happened"); // Creating own exception
}
catch(UnauthorizedAccessException e)
{
Debug.LogWarning(e.Message); // Warning message stating what happened
}
catch(ArgumentNullException e)
{
Debug.LogWarning("FAIL!!!");
}
finally
{
if (file != null)
file.Close();
}
}
示例5: Start
void Start()
{
FileStream file = null;
FileInfo fileInfo = null;
try
{
fileInfo = new FileInfo("C:\\file.txt");
file = fileInfo.OpenWrite();
for (int i = 0; i < 255; i++)
{
file.WriteByte((byte)i);
}
} catch (UnauthorizedAccessException e)
{
Debug.LogWarning(e.Message);
} finally
{
if (file != null)
{
file.Close();
}
}
}
示例6: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
String filName = s_strTFAbbrev+"TestFile";
Stream s2;
FileInfo fil2;
if(File.Exists(filName))
File.Delete(filName);
strLoc = "Loc_00005";
new FileStream(filName, FileMode.Create).Close();
iCountTestcases++;
fil2 = new FileInfo(filName);
s2 = fil2.OpenWrite();
iCountTestcases++;
if(s2.CanRead)
{
iCountErrors++;
printerr( "Error_00006! File not opened canread");
}
iCountTestcases++;
if(!s2.CanWrite)
{
iCountErrors++;
printerr( "Error_00007! File was opened canWrite");
}
s2.Close();
if(File.Exists(filName))
File.Delete(filName);
}
catch (Exception exc_general )
{
++iCountErrors;
Console.WriteLine (s_strTFAbbrev + " : Error Err_8888yyy! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
}
if ( iCountErrors == 0 )
{
Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString());
return true;
}
else
{
Console.WriteLine("FAiL! "+s_strTFName+" ,iCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums );
return false;
}
}
示例7: Start
// Use this for initialization
void Start()
{
FileStream file = null;
FileInfo fileInfo = null;
try
{
fileInfo = new FileInfo("C:\\file.txt");
file = fileInfo.OpenWrite();
for (int i = 0; i < 255; i++)
{
file.WriteByte((byte)i);
}
}
catch(UnauthorizedAccessException e)
{
Debug.Log("DIDNT WORK!");
}
}
示例8: WriteDebugLog
/// <summary>
/// 写日志
/// </summary>
/// <param name="methodName">方法名称</param>
/// <param name="errorInfo">错误信息</param>
public void WriteDebugLog(string methodName,string errorInfo,string userName)
{
try
{
string fname = serPath + "\\ControlLog.txt"; ///指定日志文件的目录
FileInfo finfo = new FileInfo(fname);///定义文件信息对象
if (finfo.Exists && finfo.Length > 100 * 1024 * 1024) ///判断文件是否存在以及是否大于512K
{
finfo.Delete();
}
using (FileStream fs = finfo.OpenWrite()) ///创建只写文件流
{
StreamWriter w = new StreamWriter(fs);///根据上面创建的文件流创建写数据流
w.BaseStream.Seek(0, SeekOrigin.End);///设置写数据流的起始位置为文件流的末尾
w.Write("<------------------------------------------------------------------------------------->\r\n");
///写入“Debug Info: ”
w.Write("Debug时间 :");
///写入当前系统时间
w.Write("{0} {1}\r\n", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
//写入出错的类名称
w.Write("Debug类名 :" + className +" 操作人:"+userName+ "\r\n");
//写入出错的方法名称
w.Write("Debug方法名 :" + methodName + "\r\n");
//写入错误信息
w.Write("Debug错误信息:" + errorInfo + "\r\n");
w.Write("<------------------------------------------------------------------------------------->\r\n");
///清空缓冲区内容,并把缓冲区内容写入基础流
w.Flush();
///关闭写数据流
w.Close();
}
}
catch (Exception ex)
{
string strEx = ex.ToString();
}
}
示例9: runTest
public bool runTest()
{
Console.WriteLine(s_strTFPath + "\\" + s_strTFName + " , for " + s_strClassMethod + " , Source ver " + s_strDtTmVer);
String strLoc = "Loc_0001";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
String filName = s_strTFAbbrev+"TestFile";
FileInfo fil2;
Stream stream;
if(File.Exists(filName))
File.Delete(filName);
iCountTestcases++;
try {
DateTime dt = File.GetLastAccessTime(null);
iCountErrors++;
printerr( "Error_0002! Expected exception not thrown");
} catch (ArgumentNullException exc){
printinfo("Expected exception thrown" + exc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0003! Unexpected exceptiont thrown: "+exc.ToString());
}
iCountTestcases++;
try {
DateTime dt = File.GetLastAccessTime("");
iCountErrors++;
printerr( "Error_0004! Expected exception not thrown");
} catch (ArgumentException exc){
printinfo("Expected exception thrown" + exc.Message);
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0005! Unexpected exceptiont thrown: "+exc.ToString());
}
strLoc = "Loc_0006";
FileStream fs = new FileStream(filName, FileMode.Create);
fs.Close();
fil2 = new FileInfo(filName);
stream = fil2.OpenWrite();
stream.Write(new Byte[]{10}, 0, 1);
stream.Close();
Thread.Sleep(4000);
fil2.Refresh();
iCountTestcases++;
try {
DateTime d1 = fil2.LastAccessTime ;
DateTime d2 = DateTime.Today ;
if( d1.Year != d2.Year || d1.Month != d2.Month || d1.Day != d2.Day) {
iCountErrors++;
printerr( "Error_0007! Creation time cannot be correct.. Expected:::" + DateTime.Today.ToString() + ", Actual:::" + fil2.LastAccessTime.ToString());
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0008! Unexpected exceptiont thrown: "+exc.ToString());
}
strLoc = "Loc_0009";
iCountTestcases++;
try {
if( (DateTime.Today - File.GetLastAccessTime(s_strTFName) ).Seconds > 60 ) {
iCountErrors++;
printerr( "Eror_0010! LastAccess time is not correct");
}
} catch (Exception exc) {
iCountErrors++;
printerr( "Error_0011! Unexpected exception thrown: "+exc.ToString());
}
fil2.Delete();
if(File.Exists(filName))
File.Delete(filName);
} catch (Exception exc_general ) {
++iCountErrors;
Console.WriteLine (s_strTFAbbrev + " : Error Err_0012! strLoc=="+ strLoc +", exc_general=="+exc_general.ToString());
}
if ( iCountErrors == 0 )
{
Console.WriteLine( "paSs. "+s_strTFName+" ,iCountTestcases=="+iCountTestcases.ToString());
return true;
}
else
{
Console.WriteLine("FAiL! "+s_strTFName+" ,iCountErrors=="+iCountErrors.ToString()+" , BugNums?: "+s_strActiveBugNums );
return false;
}
}
示例10: DecryptFile
internal static void DecryptFile(FileInfo cryptofile, FileInfo plainfile, string passPhrase)
{
FileStream fileInStream = cryptofile.OpenRead();
FileStream fileOuStream = plainfile.OpenWrite();
byte[] bufferPlain = new Byte[1024 * 1024];
byte[] bufferCrypt = new Byte[1024 * 1024];
byte[] bufferCryptShort;
byte[] bufferPlainShort;
int c = 0;
while (c * 1024 * 1024 < cryptofile.Length)
{
if ((c + 1) * 1024 * 1024 < cryptofile.Length)
{
fileInStream.Read(bufferCrypt, 0, 1024 * 1024);
bufferPlain = Decrypt(bufferCrypt, passPhrase, false);
fileOuStream.Write(bufferPlain, 0, 1024 * 1024);
}
else
{
bufferCryptShort = new byte[fileInStream.Length % (1024 * 1024)];
bufferPlainShort = new byte[fileInStream.Length % (1024 * 1024)];
fileInStream.Read(bufferCryptShort, 0, (int)(fileInStream.Length % (1024 * 1024)));
bufferPlainShort = Decrypt(bufferCryptShort, passPhrase,true);
fileOuStream.Write(bufferPlainShort, 0, (int)(fileInStream.Length % (1024 * 1024)));
}
c += 1;
}
fileInStream.Close();
fileOuStream.Close();
}
示例11: EncryptFile
public static void EncryptFile(FileInfo fileIn, FileInfo fileOut,string passPhrase)
{
FileStream fileInStream = fileIn.OpenRead();
FileStream fileOuStream = fileOut.OpenWrite();
byte[] bufferPlain = new Byte[1024*1024];
byte[] bufferCrypt = new Byte[1024*1024];
byte[] bufferPlainShort;
int c = 0;
while (c * 1024 *1024 < fileIn.Length )
{
if( (c+1) * 1024 * 1024 < fileIn.Length)
{
fileInStream.Read(bufferPlain, 0, 1024*1024);
bufferCrypt = Encrypt(bufferPlain, passPhrase,false);
fileOuStream.Write(bufferCrypt, 0, bufferCrypt.Length);
}
else
{
bufferPlainShort = new byte[fileInStream.Length % (1024 * 1024)];
fileInStream.Read(bufferPlainShort, 0, (int) (bufferPlainShort.Length ));
bufferCrypt = Encrypt(bufferPlainShort, passPhrase,true);
fileOuStream.Write(bufferCrypt, 0, (int) bufferCrypt.Length);
}
c += 1;
}
fileInStream.Close();
fileOuStream.Close();
}
示例12: runTest
public static void runTest()
{
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
String filName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
Stream s2;
FileInfo fil2;
if (File.Exists(filName))
File.Delete(filName);
// [] Open file and check for canread
//-----------------------------------------------------------------
strLoc = "Loc_00005";
new FileStream(filName, FileMode.Create).Dispose();
iCountTestcases++;
fil2 = new FileInfo(filName);
s2 = fil2.OpenWrite();
iCountTestcases++;
if (s2.CanRead)
{
iCountErrors++;
printerr("Error_00006! File not opened canread");
}
// [] Check file for canwrite
iCountTestcases++;
if (!s2.CanWrite)
{
iCountErrors++;
printerr("Error_00007! File was opened canWrite");
}
s2.Dispose();
//-----------------------------------------------------------------
if (File.Exists(filName))
File.Delete(filName);
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
}
//// Finish Diagnostics
if (iCountErrors != 0)
{
Console.WriteLine("FAiL! " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString());
}
Assert.Equal(0, iCountErrors);
}
示例13: runTest
public static void runTest()
{
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
String filName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
FileInfo fil2;
Stream stream;
if (File.Exists(filName))
File.Delete(filName);
// [] Exception for null string
//-----------------------------------------------------------
// [] Create a directory and check the creation time
strLoc = "Loc_r8r7j";
new FileStream(filName, FileMode.Create).Dispose();
fil2 = new FileInfo(filName);
stream = fil2.OpenWrite();
stream.Write(new Byte[] { 10 }, 0, 1);
stream.Dispose();
Task.Delay(4000).Wait();
fil2.Refresh();
iCountTestcases++;
try
{
// Access time is always shows the midday. For example if you create the file on 3/5/2000.
// The access time should be some thing like... 3/5/2000 12:00:00 AM.
DateTime d1 = DateTime.Now;
DateTime d2 = fil2.LastAccessTime;
if (d1.Year != d2.Year || d1.Month != d2.Month || d1.Month != d2.Month)
{
iCountErrors++;
printerr("Error_20hjx! Creation time cannot be correct");
}
}
catch (Exception exc)
{
iCountErrors++;
printerr("Bug 14952 Error_20fhd! Unexpected exceptiont thrown: " + exc.ToString());
}
// []
strLoc = "Loc_20yxc";
stream = fil2.Open(FileMode.Open);
stream.Read(new Byte[1], 0, 1);
stream.Dispose();
fil2.Refresh();
Task.Delay(1000).Wait();
iCountTestcases++;
try
{
DateTime d1 = DateTime.Now;
DateTime d2 = fil2.LastAccessTime;
if (d1.Year != d2.Year || d1.Month != d2.Month || d1.Month != d2.Month)
{
iCountErrors++;
Console.WriteLine((DateTime.Now - fil2.LastAccessTime).TotalMilliseconds);
printerr("Eror_209x9! LastAccessTime is way off");
}
}
catch (Exception exc)
{
iCountErrors++;
printerr("Error_209jx! Unexpected exception thrown: " + exc.ToString());
}
fil2.Delete();
//-----------------------------------------------------------
if (File.Exists(filName))
File.Delete(filName);
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
//.........这里部分代码省略.........
示例14: WriteFile
public static void WriteFile(string filePath,byte[] buffer)
{
try
{
if(!IsExistFile(filePath))
{
CreateFile(filePath,buffer);
}
else
{
FileInfo file = new FileInfo(filePath);
FileStream fs = file.OpenWrite();
fs.Write(buffer,0,buffer.Length);
fs.Close();
}
}
catch(Exception ex)
{
throw ex;
}
}
示例15: runTest
public static void runTest()
{
String strLoc = "Loc_000oo";
String strValue = String.Empty;
int iCountErrors = 0;
int iCountTestcases = 0;
try
{
///////////////////////// START TESTS ////////////////////////////
///////////////////////////////////////////////////////////////////
String filName = Path.Combine(TestInfo.CurrentDirectory, Path.GetRandomFileName());
FileInfo fil2;
Stream stream;
if (File.Exists(filName))
File.Delete(filName);
// [] Exception for null string
//-----------------------------------------------------------
// [] Create a directory and check the creation time
strLoc = "Loc_r8r7j";
new FileStream(filName, FileMode.Create).Dispose();
fil2 = new FileInfo(filName);
stream = fil2.OpenWrite();
stream.Write(new Byte[] { 10 }, 0, 1);
stream.Dispose();
Task.Delay(4000).Wait();
fil2.Refresh();
iCountTestcases++;
DateTime lastWriteTime = fil2.LastWriteTime;
if ((DateTime.Now - lastWriteTime).TotalMilliseconds < 2000 ||
(DateTime.Now - lastWriteTime).TotalMilliseconds > 5000)
{
iCountErrors++;
Console.WriteLine(lastWriteTime);
Console.WriteLine((DateTime.Now - lastWriteTime).TotalMilliseconds);
printerr("Error_20hjx! Last Write Time time cannot be correct");
}
// []
strLoc = "Loc_20yxc";
stream = fil2.Open(FileMode.Open, FileAccess.Read);
stream.Read(new Byte[1], 0, 1);
stream.Dispose();
fil2.Refresh();
if (fil2.LastWriteTime != lastWriteTime)
{
iCountErrors++;
printerr("Eror_209x9! LastWriteTime should not have changed due to a read");
}
stream = fil2.Open(FileMode.Open);
stream.Write(new Byte[] { 10 }, 0, 1);
stream.Dispose();
Task.Delay(3000).Wait();
fil2.Refresh();
iCountTestcases++;
if ((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds < 1000 ||
(DateTime.Now - fil2.LastWriteTime).TotalMilliseconds > 5000)
{
iCountErrors++;
Console.WriteLine((DateTime.Now - fil2.LastWriteTime).TotalMilliseconds);
printerr("Eror_f984f! LastWriteTime is way off");
}
//-----------------------------------------------------------
if (File.Exists(filName))
File.Delete(filName);
///////////////////////////////////////////////////////////////////
/////////////////////////// END TESTS /////////////////////////////
}
catch (Exception exc_general)
{
++iCountErrors;
Console.WriteLine("Error Err_8888yyy! strLoc==" + strLoc + ", exc_general==" + exc_general.ToString());
}
//// Finish Diagnostics
if (iCountErrors != 0)
{
Console.WriteLine("FAiL! " + s_strTFName + " ,iCountErrors==" + iCountErrors.ToString());
}
//.........这里部分代码省略.........