Am I correct in what I am trying to do, sourcepath is the file path of a document. 1) test if the file exists, if it doesnt throw exception 2) So now we know the file exists (and since the file is being downloaded from somewhere else) check that it contains data. (As it should not be empty), if it is empty throw error,....will this work for checking if the file is empty?
string sourcePath = "C:\Users\Martin\Desktop\document5.docx";
if (!File.Exists(sourcePath))
{
//throw exception
}
if (string.IsNullOrEmpty(sourcePath))
{
//throw exception
}
Your code will only check if (a) the file exists on disk (doesn't have to have any data) or (b) that the path has something in it.
To accurately test if a file has data, you can use:
var file = new FileInfo(sourcePath);
if (file.Length == 0)
{
//throw exception
}
More info here...
http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length(v=vs.110).aspx
As an aside, the path you've declared in the first line will not work. You'll need to escape the string for it to be seen as a valid path, so change:
string sourcePath = "C:\Users\Martin\Desktop\document5.docx";
to this:
string sourcePath = @"C:\Users\Martin\Desktop\document5.docx";
Given your comment about where the data is coming from; just edit that code:
// taken from example linked in comment
public long download(string remoteFile, string localFile)
{
long totalBytes = 0;
try
{
// ...blah
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
totalBytes += bytesRead;
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
// ...blah
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return totalBytes;
}
and just check whether that returns zero or non-zero. Also, that's pretty terrible exception handling (and here I use the word "handling" quite incorrectly).
You can check it by it's length
.
FileInfo fileInfo = new FileInfo(sourcePath);
if (fileInfo.Length > 0)
{
// file has content. file downloaded.
}
else
{
//throw exception
}
Am I correct in what I am trying to do, sourcepath is the file path of a document. 1) test if the file exists, if it doesnt throw exception 2) So now we know the file exists (and since the file is being downloaded from somewhere else) check that it contains data. (As it should not be empty), if it is empty throw error,....will this work for checking if the file is empty?
string sourcePath = "C:\Users\Martin\Desktop\document5.docx";
if (!File.Exists(sourcePath))
{
//throw exception
}
if (string.IsNullOrEmpty(sourcePath))
{
//throw exception
}
Your code will only check if (a) the file exists on disk (doesn't have to have any data) or (b) that the path has something in it.
To accurately test if a file has data, you can use:
var file = new FileInfo(sourcePath);
if (file.Length == 0)
{
//throw exception
}
More info here...
http://msdn.microsoft.com/en-us/library/system.io.fileinfo.length(v=vs.110).aspx
As an aside, the path you've declared in the first line will not work. You'll need to escape the string for it to be seen as a valid path, so change:
string sourcePath = "C:\Users\Martin\Desktop\document5.docx";
to this:
string sourcePath = @"C:\Users\Martin\Desktop\document5.docx";
Given your comment about where the data is coming from; just edit that code:
// taken from example linked in comment
public long download(string remoteFile, string localFile)
{
long totalBytes = 0;
try
{
// ...blah
try
{
while (bytesRead > 0)
{
localFileStream.Write(byteBuffer, 0, bytesRead);
totalBytes += bytesRead;
bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
}
}
// ...blah
}
catch (Exception ex) { Console.WriteLine(ex.ToString()); }
return totalBytes;
}
and just check whether that returns zero or non-zero. Also, that's pretty terrible exception handling (and here I use the word "handling" quite incorrectly).
You can check it by it's length
.
FileInfo fileInfo = new FileInfo(sourcePath);
if (fileInfo.Length > 0)
{
// file has content. file downloaded.
}
else
{
//throw exception
}
0 commentaires:
Enregistrer un commentaire