本文整理汇总了C#中System.IO.StreamWriter.Close方法的典型用法代码示例。如果您正苦于以下问题:C# StreamWriter.Close方法的具体用法?C# StreamWriter.Close怎么用?C# StreamWriter.Close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.StreamWriter
的用法示例。
在下文中一共展示了StreamWriter.Close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: WriteToLog
/// <summary>
/// Запись в ЛОГ-файл
/// </summary>
/// <param name="str"></param>
public void WriteToLog(string str, bool doWrite = true)
{
if (doWrite)
{
StreamWriter sw = null;
FileStream fs = null;
try
{
string curDir = AppDomain.CurrentDomain.BaseDirectory;
fs = new FileStream(curDir + "teplouchetlog.pi", FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
sw = new StreamWriter(fs, Encoding.Default);
if (m_vport == null) sw.WriteLine(DateTime.Now.ToString() + ": Unknown port: adress: " + m_address + ": " + str);
else sw.WriteLine(DateTime.Now.ToString() + ": " + m_vport.GetName() + ": adress: " + m_address + ": " + str);
sw.Close();
fs.Close();
}
catch
{
}
finally
{
if (sw != null)
{
sw.Close();
sw = null;
}
if (fs != null)
{
fs.Close();
fs = null;
}
}
}
}
示例2: Write
protected override async void Write(Core.LogEventInfo logEvent)
{
var request = (HttpWebRequest) WebRequest.Create(ServerUrl);
request.ContentType = "application/json; charset=utf-8";
request.Method = "POST";
var requestWriter = new StreamWriter(request.GetRequestStream());
requestWriter.Write("{ "
+ "\"version\": " + "\"" + "1.0" + "\",\n"
+ "\"host\": " + "\"" + AndroidId + "\",\n"
+ "\"short_message\": " + "\"" + logEvent.FormattedMessage + "\",\n"
+ "\"full_message\": " + "\"" + logEvent.FormattedMessage + "\",\n"
+ "\"timestamp\": " + "\"" + DateTime.Now.ToString(CultureInfo.InvariantCulture) +
"\",\n"
+ "\"level\": " + "\"" +
logEvent.Level.Ordinal.ToString(CultureInfo.InvariantCulture) + "\",\n"
+ "\"facility\": " + "\"" + "NLog Android Test" + "\",\n"
+ "\"file\": " + "\"" + Environment.CurrentDirectory + "AndroidApp" + "\",\n"
+ "\"line\": " + "\"" + "123" + "\",\n"
+ "\"Userdefinedfields\": " + "{}" + "\n"
+ "}");
requestWriter.Close();
LastResponseMessage = (HttpWebResponse) request.GetResponse();
}
示例3: ConvertFormat
public static void ConvertFormat(string strInputFile, string strOutputFile)
{
StreamReader sr = new StreamReader(strInputFile);
StreamWriter sw = new StreamWriter(strOutputFile);
string strLine = null;
while ((strLine = sr.ReadLine()) != null)
{
strLine = strLine.Trim();
string[] items = strLine.Split();
foreach (string item in items)
{
int pos = item.LastIndexOf('[');
string strTerm = item.Substring(0, pos);
string strTag = item.Substring(pos + 1, item.Length - pos - 2);
sw.WriteLine("{0}\t{1}", strTerm, strTag);
}
sw.WriteLine();
}
sr.Close();
sw.Close();
}
示例4: Extract
public static void Extract(Category category)
{
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Extract");
if(!Directory.Exists(path))
Directory.CreateDirectory(path);
pset.Clear();
pdic.Clear();
string downPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Down", category.Name);
string fileName = string.Format(@"{0}\{1}.txt", path, category.Name);
StreamWriter sw = new StreamWriter(fileName, false, Encoding.UTF8);
for (int i = category.DownPageCount; i >= 1; i--)
{
string htmlFileName = string.Format(@"{0}\{1}.html", downPath, i);
if (!File.Exists(htmlFileName))
Logger.Instance.Write(string.Format("{0}-{1}.html-not exist", category.Name, i));
StreamReader sr = new StreamReader(htmlFileName, Encoding.UTF8);
string text = sr.ReadToEnd();
sr.Close();
var action = CreateAction(category.Type);
if (action == null) continue;
Extract(text, sw, category.Name,category.DbName, action);
}
sw.Close();
Console.WriteLine("{0}:Extract Data Finished!", category.Name);
}
示例5: WriteLog
public static void WriteLog(string message)
{
var fileLog = Config.Global.Settings.LOG_PATH + "log_" + System.DateTime.Now.ToString("MM_dd_yyyy") + ".txt";
message = "\r\nTime: " + System.DateTime.Now.ToString("MM/dd/yyyy h:mm tt") + "\r\n" + message + "\r\n----------------------------------------------------";
FileStream fs = new FileStream(fileLog, FileMode.OpenOrCreate, FileAccess.ReadWrite);
StreamWriter sw = new StreamWriter(fs);
try
{
sw.Close();
fs.Close();
// Ghi file
fs = new FileStream(fileLog, FileMode.Append, FileAccess.Write);
sw = new StreamWriter(fs);
sw.Write(message);
sw.Close();
fs.Close();
}
catch
{
sw.Close();
fs.Close();
}
finally
{
sw.Close();
fs.Close();
}
}
示例6: OnStart
/* The Startup */
protected override void OnStart(string[] args)
{
/* Open a streamwriter */
StreamWriter streamWriter =
new StreamWriter("Startup.txt", true);
try
{
this.Host = new ServiceHost(typeof(EIAService), new Uri[0]);
this.Host.Open();
}
catch (Exception ex)
{
streamWriter.WriteLine(ex.ToString());
streamWriter.Flush();
streamWriter.Close();
return;
}
/* Spit it out */
streamWriter.WriteLine("Service up and running at:");
foreach (ServiceEndpoint serviceEndpoint in (Collection<ServiceEndpoint>)this.Host.Description.Endpoints)
streamWriter.WriteLine((object)serviceEndpoint.Address);
streamWriter.Flush();
streamWriter.Close();
}
示例7: SerializeToText
public static string SerializeToText(System.Type ObjectType, Object Object)
{
string RetVal;
StreamWriter Writer;
StreamReader Reader;
MemoryStream Stream;
RetVal = string.Empty;
Stream = new MemoryStream();
Reader = new StreamReader(Stream);
Writer = new StreamWriter(Stream);
try
{
if (Object != null && ObjectType != null)
{
Serialize(Writer, ObjectType, Object);
Stream.Position = 0;
RetVal = Reader.ReadToEnd();
Writer.Flush();
Writer.Close();
Reader.Close();
}
}
catch (Exception ex)
{
Writer.Flush();
Writer.Close();
Reader.Close();
throw ex;
}
return RetVal;
}
示例8: WriteFiles
public void WriteFiles(string content)
{
try
{
FileStream fi = new FileStream(AppDomain.CurrentDomain.BaseDirectory + "\\cache.txt", FileMode.Append);
StreamWriter sw = new StreamWriter(fi, Encoding.UTF8);
sw.WriteLine(content);
sw.WriteLine("-------------------------------------------------------");
if (fi.Length >= (1024 * 1024 * 5))
{
sw.Close();
fi.Close();
if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + "\\cache.txt"))
{
File.Delete(AppDomain.CurrentDomain.BaseDirectory + "\\cache.txt");
}
return;
}
sw.Close();
fi.Close();
}
catch (Exception ex)
{
}
}
示例9: Export
/// <summary>
/// Exports an answer matrix to a file.
/// </summary>
/// <param name="answerMatrix">Answer matrix</param>
/// <param name="filename">Output file path</param>
public static void Export(TLSimilarityMatrix answerMatrix, string filename)
{
TextWriter tw = null;
try
{
tw = new StreamWriter(filename);
foreach (string sourceID in answerMatrix.SourceArtifactsIds)
{
tw.Write(sourceID);
foreach (string targetID in answerMatrix.GetSetOfTargetArtifactIdsAboveThresholdForSourceArtifact(sourceID))
{
tw.Write(" " + targetID);
}
tw.WriteLine();
}
tw.Flush();
tw.Close();
}
catch (Exception e)
{
if (tw != null)
{
tw.Close();
}
throw new DevelopmentKitException("There was an exception writing to file (" + filename + ")", e);
}
}
示例10: IsTemplateEnabledFor
public static bool IsTemplateEnabledFor(object target, string templateName, DTE service = null)
{
try
{
var selectedItem = target as ProjectItem;
if (selectedItem == null)
{
return false;
}
string selectedFolderPath = DteHelper.GetFilePathRelative(selectedItem);
var templatePath = TemplateConfiguration.GetConfiguration(service).ExtRootFolderName + "\\" + templateName;
var wr = new StreamWriter(@"C:\test.text", true);
if (selectedFolderPath.ToLower().Contains(templatePath.ToLower()))
{
wr.WriteLine("SelectedFolderPath:{0}, TemplatePath:{1}, Valid", selectedFolderPath, templatePath);
wr.Close();
return true;
}
wr.WriteLine("SelectedFolderPath:{0}, TemplatePath:{1}, Invalid", selectedFolderPath, templatePath);
wr.Close();
return false;
}
catch (Exception ex)
{
MessageBox.Show(string.Format(ErrorMessages.GeneralError, ex.Message), MessageType.Error,
MessageBoxButtons.OK,
MessageBoxIcon.Error);
return false;
}
}
示例11: addLog
public void addLog(string method, string kind, string msg, LogType logType)
{
string localPath = "";
string logPath = AppDomain.CurrentDomain.BaseDirectory + "log/" + logType.ToString() + "/";
localPath = string.Format(logPath + "{0:yyyyMMdd}.log", DateTime.Now);
lock (localPath)
{
StreamWriter writer = null;
try
{
System.IO.FileInfo info = new FileInfo(localPath);
if (!info.Directory.Exists)
info.Directory.Create();
writer = new StreamWriter(localPath, true, System.Text.Encoding.UTF8);
writer.WriteLine(string.Format("{0}[{1:HH:mm:ss}] 方法{2} 用户:{3}[end]", kind, DateTime.Now, method, msg));
}
catch
{
if (writer != null)
writer.Close();
}
finally
{
if (writer != null)
writer.Close();
}
}
}
示例12: CommitQuizAnswers
private static void CommitQuizAnswers(String quizAnswers, String personName, String setupID)
{
XmlSerializer writer;
StreamWriter quizAnswersFile = null;
DataSet quizData = null;
try
{
if (String.IsNullOrEmpty(quizAnswers))
return;
quizData = RetrieveQuizAnswersData();
//Create the dataset of answers
quizData.Tables[0].Rows.Add(setupID, personName, quizAnswers, DateTime.Now);
writer = new XmlSerializer(typeof(DataSet));
quizAnswersFile = new StreamWriter(QuizAnswersFilePath);
writer.Serialize(quizAnswersFile, quizData);
quizAnswersFile.Close();
quizAnswersFile.Dispose();
}
catch (Exception ex)
{
RippleCommonUtilities.LoggingHelper.LogTrace(1, "Went wrong in CommitQuizAnswers at Screen side {0}", ex.Message);
writer = null;
if (quizAnswersFile != null)
{
quizAnswersFile.Close();
quizAnswersFile.Dispose();
}
}
}
示例13: Game_OnChat
static void Game_OnChat(GameChatEventArgs args)
{
if (!main.Item("enabled").GetValue<bool>())
return;
try{
var stream = new StreamWriter(_path, true, Encoding.UTF8);
if (args.Sender.IsAlly)
{
stream.WriteLine("[" + Utils.FormatTime(Game.ClockTime) + "]" + " sender: " + args.Sender.Name + " says: " + args.Message);
stream.Close();
}
else
{
stream.WriteLine("[" + Utils.FormatTime(Game.ClockTime) + "]" + "[enemy] sender: " + args.Sender.Name + " says: " + args.Message);
stream.Close();
}
if (main.Item("notify").GetValue<bool>())
Notifications.AddNotification(new Notification("Chat loged",500).SetBoxColor(Color.Black).SetTextColor(Color.Green));
if (main.Item("delay").GetValue<int>()!=0)
System.Threading.Thread.Sleep(main.Item("delay").GetValue<int>());
}
catch (Exception e)
{
//Notifications.AddNotification("ChatLog error: " + e.Message,1000);
}
}
示例14: createCsv
static float vel_Prec = 0; // Contiene il valore della velocità all'ultimo istante della finestra precedente.
#endregion Fields
#region Methods
// Metodo per la scrittura e creazione del .csv che contiene i dati.
public static void createCsv(float[,,] sampwin, string path)
{
StreamWriter file = new StreamWriter(@path, true);
string stream = "";
for (int s = 0; s < sampwin.GetLength(0); s++)
{
stream = stream + "SENSORE " + (s + 1) + ":" + "\n" + "\n";
for (int i = 0; i < sampwin.GetLength(1); i++)
{
for (int j = 0; j < sampwin.GetLength(2); j++)
{
stream = stream + sampwin[s, i, j].ToString() + ";";
}
stream = stream + "\n";
}
stream = stream + "\n";
try
{
file.Write(stream);
stream = "";
}
catch (Exception e)
{
file.Close(); stream = "";
}
}
file.Close();
}
示例15: SaveInfo3
public void SaveInfo3(string data, string filename)
{
StreamWriter sw = null;
try
{
FileStream fs = File.Open(filename, FileMode.Open);
sw = new StreamWriter(fs);
sw.Write(data);
sw.Close();
}
catch (FileNotFoundException fnfex)
{
Console.WriteLine("File does not exist: {0}\n",
fnfex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Unexpected exception:{0}\n",
ex.Message);
}
finally
{
if (sw != null)
{
sw.Close();
}
}
}