Create the file if readalltext can't find it - c#

I'm writing an application in visual studio using c#. I want to check if readalltext finds the file correctly, if not it needs to create the file and put a zero in it. In pseudocode:
if(x=File.ReadAllText("file.txt")==NULL)
{
File.WriteAllText("file.txt", "0");
x=File.ReadAllText("file.txt");
}
How can I do this? Thanks in advance, I tried some google but I may be inputting the wrong keywords

You can check whether a file exists with the File.Exists() method.
string path = "file.txt";
if (!File.Exists(path))
{
File.WriteAllText(path, "0");
}

The problem with using File.Exist() is that there is a risk the file is created or deleted after the check was made. The risk may be small, but may still need to be handled. One way to handle this would be with a try/catch inside a loop:
while (true)
{
try
{
if (!File.Exists(path))
{
File.WriteAllText(path, "0");
return "0";
}
else
{
return File.ReadAllText(path);
}
}
catch (IOException)
{
// try again
}
}
Another way would be to skip ReadAllText and instead open a fileStream. If that succeeds you know you have exclusive access to the file, to either read or write to it:
try
{
using var fs = File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);
if (fs.Length == 0)
{
using var sw = new StreamWriter(fs);
sw.Write('0');
return "0";
}
else
{
using var sr = new StreamReader(fs);
return sr.ReadToEnd();
}
}
catch (Exception)
{
// Handle the various types of exception that may occur.
}

Related

How to write a .csv file on a network location?

I am currently writing a .csv to client browser using Response object, which was/is a fairly easy job.
But now the requirement has been changed to create this file at a network location from where any time the job will pick it.
I am not sure how can I achieve this, any suggestions would be helpful.
Existing Code:
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment;filename=" + GenerateFileName(publishToViewModel[0].ProjectId));
Response.ContentType = "text/csv";
StreamWriter writer = new StreamWriter();
try
{
string CSVFriendlyData = this.GetCSV(publishToViewModel);
writer.Write(CSVFriendlyData);
Response.Write(writer.ToString());
Response.End();
}
since it is hard to guarantee that a write to a network file will be successful (your old file might still be there, and the timed job might have a lock on it, etc) it's a good idea to build a mechanism that will retry writing to the file several times.
void WriteToNetworkFile()
{
int retries = 3;
while(retries > 0)
{
if(tryWriteFile())
{
break;
}
retries--;
// you could add a timeout here to make sure your attempts are a little more
//spaced out.
//it could be in the form of a Thread.Sleep, or you could extract a method and
//call it using a timer.
if(retries < 1)
{
//log that we failed to write the file and gave up on trying.
}
}
}
protected void tryWriteFile()
{
try
{
//you could pass this path as a parameter too.
var fileLoc = "\\server\folder\file.ext";
//open and obtain read/write lock on the file
//using FileMode.CreateNew will ensure that a new file is created.
//alternatively, you can use FileMosw.Create to create a new file
//or overwrite the old file if it is there.
using (var fs = File.Open(fileLoc, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.None))
{
var sw = new StreamWriter(fs);
sw.Write("file contents go here");
sw.Flush();
sw.Close();
return true;
}
}
catch(Exception e)
{
//you might want to log why the write failed here.
return false;
}
}

Issue in create and write in a text file using c#

I try to create a text file and write some data to it. I am using the following code:
public void AddNews(string path,string News_Title,string New_Desc)
{
FileStream fs = null;
string fileloc = path + News_Title+".txt";
if (!File.Exists(fileloc))
{
using (fs = new FileStream(fileloc,FileMode.OpenOrCreate,FileAccess.Write))
{
using (StreamWriter sw = new StreamWriter(fileloc))
{
sw.Write(New_Desc);
}
}
}
}
I got this exception in stream writer:
The process cannot access the file '..............\Pro\Content\News\AllNews\Par.txt'
because it is being used by another process.
Text file is created, but I can't write to it.
When you create your StreamWriter object, you're specifying the same file that you already opened as a FileStream.
Use the constructor overload of StreamWriter that accepts your FileStream object, instead of specifying the file again, like this:
using (StreamWriter sw = new StreamWriter(fs))
I would simply do this:
public void AddNews(string path, string News_Title, string New_Desc)
{
string fileloc = Path.Combine(path, News_Title+".txt");
if (!File.Exists(fileloc)) {
File.WriteAllText(fileloc, New_Desc);
}
}
Note that I use Path.Combine as a better way to create paths, and File.WriteAllText as a simple way of creating a file and writing something to it. As MSDN says:
If the target file already exists, it is overwritten.
so we first check if the file already exists, as you did. If you want to overwrite its contents, just don't check and write directly.
The issue could be that the file is open or in use. Consider checking if the file is open before writing to it...
public bool IsFileOpen(FileInfo file)
{
FileStream stream = null;
try
{
stream = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException)
{
// Is Open
return true;
}
finally
{
if (stream != null)
stream.Close();
}
//Not Open
return false;
}
Good Luck!
using (TextWriter tw = new StreamWriter(path, true))
{
tw.WriteLine("The next line!");
}

How do we use FileMode.Append on C#?

I've been trying to come up with a way for my code to open a file or create one (if my given file name is non-existent). Afterwards, it will run a program that will end up creating an array and I want the contents of that array to be converted into string and appended into the file that I am creating and opening. I've got everything right except for the 'Append' part. It say in the end that the "Object reference not set to an instance of an object." Can you please enlighten me on this one? Help will be much appreciated.
try
{
FileStream fs = new FileStream("inventory.ini", FileMode.OpenOrCreate, FileAccess.Read);
StreamReader reader = new StreamReader(fs);
while (!reader.EndOfStream)
{
string line = reader.ReadLine();
string[] data = line.Split('|');
int code = int.Parse(data[0]);
string name = data[1];
double price = double.Parse(data[2]);
Item item = new Item(code, name, price);
app.array[inventoryCount++] = item;
}
reader.Close();
fs.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
app.Run();
try
{
FileStream fs = new FileStream("inventory.ini", FileMode.Append, FileAccess.Write);
StreamWriter writer = new StreamWriter(fs);
foreach (Item item in app.array)
{
writer.WriteLine(item.Code + "|" + item.Name + "|" + item.Price);
}
writer.Close();
fs.Close();
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Console.ReadLine();
}
You can use another constructor of StreamWriter, that allows appending, and then write like this:
StreamWriter writer = new StreamWriter("inventory.ini", true);
I never used FileStream in my apps, but StreamWriter has been quite reliable. You can also switch to Using statement, then you don't need to Close().
Also I suggest switching to lists, then you will always have the exact amount of items you need inside app.array (which btw needs a better name). So this:
app.array[inventoryCount++] = item;
will change to something like this:
app.list.Add(item);
Aside from memory management headache relief, you no longer need inventoryCount variable, since you can get this value from list.Count;
The general approach here is to minimize amount of code you need to write, for the same amount of functionality. Then you have no place for the error to lurk.
catch (Exception e)
{
Console.WriteLine(e.Message);
}
You are digging yourself a pretty deep hole with exception handling like this. A hard rule in catching an exception is that you restore the state of your program when you handle it. You don't. In particular, you are forgetting to close the file. This then goes wrong, later, when you try to open the file again to write. The exception message is misleading, unfortunately, talking about another process having the file already opened. Not the case, it is your process that still has the file opened.
There are plenty of countermeasures against this failure. You should be using the using statement to ensure the file is closed even if there's an exception. And you'll need to fix your EndOfStream test, it isn't accurate on text files, use a while(true) loop and break when ReadLine() returns null. Which solves the original problem.
But the real fix is to not hide an inconvenient truth. Allowing your program to continue running when a config file is broken just begets more trouble when it doesn't do what you hope it does. And you can't tell because the message you write to the console was scrolled off the screen. Very hard to diagnose.
Remove the try/catch from this code. Now you get to address the real problem.
Note that you can also just use File.AppendText() to open a StreamWriter in append mode.
You should also use using instead of .Close() to close the stream - then it will work even if an exception occurs.
So your code would look more like this:
try
{
using (var writer = File.AppendText("inventory.ini"))
{
foreach (Item item in app.array)
{
if (item != null)
writer.WriteLine(item.Code + "|" + item.Name + "|" + item.Price);
}
}
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
Why don't you use using statement
using (FileStream fs = new FileStream("inventory.ini", FileMode.OpenOrCreate, FileAccess.Read))
using (StreamReader reader = new StreamReader(fs))
{
// do stuff
}

Is there a better way than this to find out if an IsolatedStorage file exists or not?

I'm using IsolatedStorage in a Silverlight application for caching, so I need to know if the file exists or not which I do with the following method.
I couldn't find a FileExists method for IsolatedStorage so I'm just catching the exception, but it seems to be a quite general exception, I'm concerned it will catch more than if the file doesn't exist.
Is there a better way to find out if a file exists in IsolatedStorage than this:
public static string LoadTextFromIsolatedStorageFile(string fileName)
{
string text = String.Empty;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
try
{
using (IsolatedStorageFileStream isfs = new IsolatedStorageFileStream(fileName,
FileMode.Open, isf))
{
using (StreamReader sr = new StreamReader(isfs))
{
string lineOfData = String.Empty;
while ((lineOfData = sr.ReadLine()) != null)
text += lineOfData;
}
}
return text;
}
catch (IsolatedStorageException ex)
{
return "";
}
}
}
From the "manual" (.net framework 2.0 Application Development Foundation):
Unlike the application programming interface (API) for files stored arbitrarily
in the file system, the API for files in Isolated Storage does not support checking
for the existence of a file directly like File.Exists does. Instead, you need to ask the
store for a list of files that match a particular file mask. If it is found, you can open the
file, as shown in this example
string[] files = userStore.GetFileNames("UserSettings.set");
if (files.Length == 0)
{
Console.WriteLine("File not found");
}
else
{
// ...
}

Best way to read contents of web.config

I need to read the contents of the Web.Config and send them off in an email, is there a better way to do the following:
string webConfigContents = String.Empty;
using (FileStream steam = new FileStream(
Server.MapPath("~/web.config"),
FileMode.Open, FileAccess.Read, FileShare.Read))
{
using (StreamReader reader = new StreamReader(steam))
{
webConfigContents = reader.ReadToEnd();
}
}
I dont want to lock the file. Any ideas?
Edit - I need a raw dump of the file, I cant attach the file (Webhost says nay!), and im not looking for anything specific inside it
You can replace your code with this:
string webConfigContents = File.ReadAllText(Server.MapPath("~/web.config"));
The file will be locked for writing while it is being read, but not for reading. But I don't think this will be a problem, because the web.config file is not usually being written to (since this would restart the web application).
By using the asp.net api.
System.Configuration.Configuration rootWebConfig1 =
System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(null);
if (0 < rootWebConfig1.AppSettings.Settings.Count)
{
System.Configuration.KeyValueConfigurationElement customSetting =
rootWebConfig1.AppSettings.Settings["customsetting1"];
if (null != customSetting) {
Console.WriteLine("customsetting1 application string = \"{0}\"", customSetting.Value);
}
else {
Console.WriteLine("No customsetting1 application string");
}
}

Categories

Resources