本文整理汇总了C#中System.IO.FileStream.Write方法的典型用法代码示例。如果您正苦于以下问题:C# System.IO.FileStream.Write方法的具体用法?C# System.IO.FileStream.Write怎么用?C# System.IO.FileStream.Write使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了System.IO.FileStream.Write方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateFromDirectory
public void CreateFromDirectory( string Dir, string Outfile )
{
string[] Filepaths = System.IO.Directory.GetFiles( Dir );
ushort Filecount = (ushort)Filepaths.Length;
uint RequiredBytesForHeader = Util.Align( Filecount * 3u + 5u, 0x10u ); // 3 bytes per filesize + 3 bytes for an extra pointer to first file + 2 bytes for filecount
var Filestream = new System.IO.FileStream( Outfile, System.IO.FileMode.Create );
// header
Filestream.Write( BitConverter.GetBytes( Filecount ), 0, 2 );
Filestream.Write( Util.GetBytesForUInt24( RequiredBytesForHeader ), 0, 3 );
uint TotalFilesize = RequiredBytesForHeader;
foreach ( string Path in Filepaths ) {
TotalFilesize += (uint)( new System.IO.FileInfo( Path ).Length );
Filestream.Write( Util.GetBytesForUInt24( TotalFilesize ), 0, 3 );
TotalFilesize = TotalFilesize.Align( 0x10u );
}
while ( Filestream.Length < RequiredBytesForHeader ) { Filestream.WriteByte( 0x00 ); }
// files
foreach ( string Path in Filepaths ) {
var File = new System.IO.FileStream( Path, System.IO.FileMode.Open );
Util.CopyStream( File, Filestream, (int)File.Length );
File.Close();
while ( Filestream.Length % 0x10 != 0 ) { Filestream.WriteByte( 0x00 ); }
}
Filestream.Close();
}
示例2: Main
static void Main(string[] args)
{
Console.Write("> Введите количество файлов: ");
int n = 0;
Int32.TryParse(Console.ReadLine(), out n);
int[] setOfItems = new int[n];
Random rnd = new Random();
Dictionary<int, bool> tmpHash = new Dictionary<int, bool>();
for (int i = 0; i < n * 2; ++i) {
tmpHash[i] = false;
}
// Get a set of numbers
int j = 0;
while(j < n)
{
int randomNumber = rnd.Next(0, n * 2);
if (!tmpHash[randomNumber]) {
Console.WriteLine(randomNumber.ToString());
setOfItems[j++] = randomNumber;
tmpHash[randomNumber] = true;
}
}
Console.WriteLine("... набор данных готов!");
// Saving to the file
Console.Write("> Введите имя файла: ");
String filename = "w:\\Zd02\\";
filename += Console.ReadLine();
int count = n;
byte[] buffer = new byte[4];
try
{
using (System.IO.FileStream fs = new System.IO.FileStream(filename,
System.IO.FileMode.Create,
System.IO.FileAccess.Write))
{
buffer = BitConverter.GetBytes(count);
fs.Write(buffer, 0, buffer.Length);
for (int i = 0; i < count; ++i)
{
buffer = BitConverter.GetBytes(setOfItems[i]);
fs.Write(buffer, 0, buffer.Length);
}
fs.Close();
}
Console.WriteLine(String.Format("... файл сохранен как {0}!", filename));
}
catch (System.Exception ex)
{
Console.WriteLine("... Error! " + ex.ToString());
}
Console.ReadLine();
}
示例3: Main
static void Main(string[] args)
{
try
{
// open file to output extracted fragments
System.IO.FileInfo f1 = new System.IO.FileInfo("./out.txt");
System.IO.FileStream fos = new System.IO.FileStream(f1.FullName, System.IO.FileMode.Create);
// instantiate the parser
VTDGen vg = new VTDGen();
if (vg.parseFile("./soap2.xml", true))
{
VTDNav vn = vg.getNav();
// get to the SOAP header
if (vn.toElementNS(VTDNav.FC, "http://www.w3.org/2003/05/soap-envelope", "Header"))
{
if (vn.toElement(VTDNav.FC))
// to first child
{
do
{
// test MUSTHAVE
if (vn.hasAttrNS("http://www.w3.org/2003/05/soap-envelope", "mustUnderstand"))
{
long l = vn.getElementFragment();
int len = (int)(l >> 32);
int offset = (int)l;
byte[] b = vn.getXML().getBytes();
fos.Write(b, offset, len); //write the fragment out into out.txt
System.Text.Encoding encoder = System.Text.Encoding.GetEncoding("ASCII");
byte[] bytes = encoder.GetBytes("\n=========\n");
fos.Write(bytes, 0, bytes.Length);
}
}
while (vn.toElement(VTDNav.NS)); // navigate next sibling
}
else
System.Console.Out.WriteLine("Header has not child elements");
}
else
System.Console.Out.WriteLine(" Dosesn't have a header");
fos.Close();
}
}
catch (NavException e)
{
System.Console.Out.WriteLine(" Exception during navigation " + e);
}
catch (System.IO.IOException e)
{
System.Console.Out.WriteLine(" IO exception condition" + e);
}
}
示例4: DownloadFile
//---------- 一些静态方法 ----------
// 下载文件到指定的路径
public static void DownloadFile(string URL, string filename)
{
try
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create);
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
System.Windows.Forms.Application.DoEvents();
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
}
catch (Exception ex)
{
logLog(ex.ToString());
}
}
示例5: Test
// Following example show how to save file and read back from gridfs(using official mongodb driver):
// http://www.mongovue.com/
public void Test()
{
MongoDB.Driver.MongoServer server = MongoServer.Create("mongodb://localhost:27020");
MongoDB.Driver.MongoDatabase database = server.GetDatabase("tesdb");
string fileName = "D:\\Untitled.png";
string newFileName = "D:\\new_Untitled.png";
using (System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open))
{
MongoDB.Driver.GridFS.MongoGridFSFileInfo gridFsInfo = database.GridFS.Upload(fs, fileName);
BsonValue fileId = gridFsInfo.Id;
ObjectId oid = new ObjectId(fileId.AsByteArray);
MongoDB.Driver.GridFS.MongoGridFSFileInfo file = database.GridFS.FindOne(Query.EQ("_id", oid));
using (System.IO.Stream stream = file.OpenRead())
{
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
using (System.IO.FileStream newFs = new System.IO.FileStream(newFileName, System.IO.FileMode.Create))
{
newFs.Write(bytes, 0, bytes.Length);
} // End Using newFs
} // End Using stream
} // End using fs
}
示例6: CreateFromDirectory
public void CreateFromDirectory( string Dir, string Outfile )
{
string[] Filepaths = System.IO.Directory.GetFiles( Dir );
ushort Filecount = (ushort)Filepaths.Length;
uint RequiredBytesForHeader = Util.Align( Filecount * 4u + 4u, 0x50u ); // pretty sure this is not right but should work
var Filestream = new System.IO.FileStream( Outfile, System.IO.FileMode.Create );
// header
Filestream.WriteByte( (byte)'S' ); Filestream.WriteByte( (byte)'C' );
Filestream.WriteByte( (byte)'M' ); Filestream.WriteByte( (byte)'P' );
uint TotalFilesize = RequiredBytesForHeader;
foreach ( string Path in Filepaths ) {
Filestream.Write( BitConverter.GetBytes( TotalFilesize ), 0, 4 );
TotalFilesize += (uint)( new System.IO.FileInfo( Path ).Length );
TotalFilesize = TotalFilesize.Align( 0x10u );
}
while ( Filestream.Length < RequiredBytesForHeader ) { Filestream.WriteByte( 0x00 ); }
// files
foreach ( string Path in Filepaths ) {
var File = new System.IO.FileStream( Path, System.IO.FileMode.Open );
Util.CopyStream( File, Filestream, (int)File.Length );
File.Close();
while ( Filestream.Length % 0x10 != 0 ) { Filestream.WriteByte( 0x00 ); }
}
Filestream.Close();
}
示例7: CreateReportAsPDF
public void CreateReportAsPDF(string grnNumbers, string fileName)
{
try
{
this.SP_GRNReportTableAdapter.Fill(this.grnReportDataSet.SP_GRNReport, grnNumbers);
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
// Setup the report viewer object and get the array of bytes
byte[] bytes = reportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
using (System.IO.FileStream sw = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
{
sw.Write(bytes, 0, bytes.Length);
sw.Close();
}
}
catch (Exception ex)
{
}
}
示例8: CreateReportAsPDF
public void CreateReportAsPDF(long? dyeingJoID, string fileName)
{
try
{
this.SP_CompactingJoDetailsTableAdapter.Fill(this.CompactingJoDataSet.SP_CompactingJoDetails, dyeingJoID);
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
// Setup the report viewer object and get the array of bytes
byte[] bytes = reportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
using (System.IO.FileStream sw = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
{
sw.Write(bytes, 0, bytes.Length);
sw.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
示例9: UploadFile
public void UploadFile(RemoteFileInfo request)
{
// report start
Console.WriteLine("Start uploading " + request.FileName);
Console.WriteLine("Size " + request.Length);
// create output folder, if does not exist
if (!System.IO.Directory.Exists("Upload")) System.IO.Directory.CreateDirectory("Upload");
// kill target file, if already exists
string filePath = System.IO.Path.Combine("Upload", request.FileName);
if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath);
int chunkSize = 2048;
byte[] buffer = new byte[chunkSize];
using (System.IO.FileStream writeStream = new System.IO.FileStream(filePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
{
do
{
// read bytes from input stream
int bytesRead = request.FileByteStream.Read(buffer, 0, chunkSize);
if (bytesRead == 0) break;
// write bytes to output stream
writeStream.Write(buffer, 0, bytesRead);
} while (true);
// report end
Console.WriteLine("Done!");
writeStream.Close();
}
}
示例10: SaveFile
private void SaveFile(byte[] byteArray)
{
// Displays a SaveFileDialog so the user can save the Image
// assigned to Button2.
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.Filter = "MCEdit Schematic|*.schematic";
saveFileDialog1.Title = "Save a Schematic File";
saveFileDialog1.ShowDialog();
// If the file name is not an empty string open it for saving.
if (saveFileDialog1.FileName != "")
{
try
{
// Open file for reading
System.IO.FileStream fs = new System.IO.FileStream(saveFileDialog1.FileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
// Writes a block of bytes to this stream using data from a byte array.
fs.Write(byteArray, 0, byteArray.Length);
// close file stream
fs.Close();
}
catch (Exception _Exception)
{
// Error
Console.WriteLine("Exception caught in process: {0}", _Exception.ToString());
}
}
}
示例11: ExtractVideoResourceToFile
private void ExtractVideoResourceToFile(string targetFile)
{
// Create temporary output stream file name.
System.IO.FileStream file = new System.IO.FileStream(
targetFile, System.IO.FileMode.Create);
Uri uri = new Uri(ResourceNames.IntroductionVideo, UriKind.RelativeOrAbsolute);
StreamResourceInfo resourceInfo = Application.GetResourceStream(uri);
System.IO.Stream resourceStream = resourceInfo.Stream;
if (null != resourceStream)
{
try
{
int offset = 0, bytesRead = 0;
byte[] buffer = new byte[4096];
while ((bytesRead = resourceStream.Read(buffer, offset, buffer.Length)) > 0)
{
file.Write(buffer, offset, bytesRead);
}
file.Flush();
file.Close();
}
catch (Exception)
{
}
}
}
示例12: LogError
public void LogError(Exception ex, string source)
{
try
{
String LogFile = HttpContext.Current.Request.MapPath("/Errorlog.txt");
if (LogFile != "")
{
String Message = String.Format("{0}{0}=== {1} ==={0}{2}{0}{3}{0}{4}{0}{5}"
, Environment.NewLine
, DateTime.Now
, ex.Message
, source
, ex.InnerException
, ex.StackTrace);
byte[] binLogString = Encoding.Default.GetBytes(Message);
System.IO.FileStream loFile = new System.IO.FileStream(LogFile
, System.IO.FileMode.OpenOrCreate
, System.IO.FileAccess.Write, System.IO.FileShare.Write);
loFile.Seek(0, System.IO.SeekOrigin.End);
loFile.Write(binLogString, 0, binLogString.Length);
loFile.Close();
}
}
catch { /*No need to catch the error here. */}
}
示例13: CreateReportAsPDF
public void CreateReportAsPDF(long? orderID, long? orderProductID, string fileName)
{
try
{
this.SP_MaterialDetailsTableAdapter.Fill(this.OrderManagerDBDataSet.SP_MaterialDetails, orderProductID, orderID);
this.SP_MaterialOtherCostDetailsTableAdapter.Fill(this.OrderManagerDBOtherCostDataSet.SP_MaterialOtherCostDetails, orderProductID);
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
// Setup the report viewer object and get the array of bytes
byte[] bytes = reportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
using (System.IO.FileStream sw = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
{
sw.Write(bytes, 0, bytes.Length);
sw.Close();
}
}
catch (Exception ex)
{
}
}
示例14: CreateReportAsPDF
public void CreateReportAsPDF(int? supplierID, long? jobOrderId, string fileName)
{
try
{
this.SP_JoGRNTableAdapter.Fill(this.joGRNReportDataSet.SP_JoGRN, supplierID, jobOrderId);
// Variables
Warning[] warnings;
string[] streamIds;
string mimeType = string.Empty;
string encoding = string.Empty;
string extension = string.Empty;
reportViewer1.LocalReport.Refresh();
// Setup the report viewer object and get the array of bytes
byte[] bytes = reportViewer1.LocalReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamIds, out warnings);
using (System.IO.FileStream sw = new System.IO.FileStream(fileName, System.IO.FileMode.Create))
{
sw.Write(bytes, 0, bytes.Length);
sw.Close();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
示例15: Execute
public static int Execute( List<string> args )
{
string filename = args[0];
uint width = UInt32.Parse( args[1] );
uint height = UInt32.Parse( args[2] );
byte[] data = System.IO.File.ReadAllBytes( filename );
string path = filename + ".dds";
using ( var fs = new System.IO.FileStream( path, System.IO.FileMode.Create ) ) {
fs.Write( Textures.DDSHeader.Generate( width, height, 1, format: Textures.TextureFormat.DXT5 ) );
fs.Write( data );
fs.Close();
}
return 0;
}