I want to get the file name from user and then according to that name i will get another input. It's a multi input multi output problem.
So i Normalized the problem as below.
1.I am using Script Tast in Control flow And asked to enter the file Name Using a form in C#. I am getting file name as browsing the file.
2.Then Unzip the file and check the name of the file according to the name of the file i set 3 varible which are globel.
3.According to variables i took 3 Data flow tasks. And its working well for the above specified problem.
4.Its Working for different Data flow tasks.
But i have another issue in this when i am using the flat file source name as dynamicly(Using Expression) It is giving me error about the Can not open the file.
Error "[Flat File Source [1]] Error: Cannot open the datafile "C:\Documents and Settings\XQN4P\Desktop\Inputs\Intraday\OPTION_DAILY_INTRADAY_ASIA20140212.csv". "
Note this file is generated from Unziping the entered file by user.
There is warning as well "[Flat File Source [1]] Warning: The process cannot access the file because it is being used by another process.
".
here is the code i am using to unzip it.
}
private void unzipInFile(string inputFilePath, string destDirectory,string destFileName)
{
FileStream fsIn =new FileStream (inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
FileStream fsOut =new FileStream(destDirectory +"\\"+destFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
int buffersize = 4096;
int count = 0;
var buffer= new byte [buffersize];
//using (Stream compressed = File.OpenRead(inputFilePath))
//using (ZlibStream zlib = new ZlibStream(compressed, CompressionMode.Decompress))
using(GZipStream gZip = new GZipStream(fsIn, Ionic.Zlib.CompressionMode.Decompress, Ionic.Zlib.CompressionLevel.BestCompression, true ))
{
byte[] buf = new byte[short.MaxValue];
int bufl;
while (0 != (bufl = gZip.Read(buf, 0, buf.Length)))
{
if (count != 0)
fsOut.Write(buffer, 0, count);
if (count != buffersize)
return ;
}
}
fsOut.Close();
}
Another Address variable
Dts.Variables["FileAddress"].Value = filePath + ".csv";
SSIS structure.:
"ScriptTask" sending output to 3 Data Flow Tasks.
Problem Solved.
Problem was in formating of the unzipped file.
It was not able to get the row delimiter.
And some times file was still in use.
So i worked around for this and it's ok
Related
Hi I am trying to write the assembly version to a textfile for use with an autoupdater program.
This is what I have for getting the current assembly version to a string.
// Get assembly info to string
string assemblyVersion = AssemblyName.GetAssemblyName("MainApplication.exe").Version.ToString();
This is then being written to a textfile using filestream.
private void SaveVersion()
{
// creating filestream that can write a file
FileStream fs = new FileStream("Version.txt", FileMode.Create, FileAccess.Write);
// if we don't have permission to write we exit function
if (!fs.CanWrite)
return;
byte[] buffer = Encoding.ASCII.GetBytes(assemblyVersion);
// writing whole buffer array
fs.Write(buffer, 0, buffer.Length);
// closing filestream
fs.Flush();
fs.Close();
}
However for some reason the Version.txt file is never being populated.
What am I missing here. Thanks.
You can do this with a single line of code
System.IO.File.WriteAllText("Version.txt", assemblyVersion);
Here is my code:
enter image description here
using System.IO;
namespace Randoms
{
public class Program
{
public static void Main()
{
byte[] buffer = new byte[10240]; // buffer size
string path = #"C:\Users\RAHUL\Desktop\file.txt";
using (FileStream source = new FileStream(path, FileMode.Open, FileAccess.Read))
{
long fileLength = source.Length;
using (FileStream dest = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
long totalBytes = 0;
int currentBlockSize = 0;
while ((currentBlockSize = source.Read(buffer, 0, buffer.Length)) > 0)
{
totalBytes += currentBlockSize;
double percentage = (double)totalBytes * 100.0 / fileLength;
dest.Write(buffer, 0, currentBlockSize);
}
}
}
}
}
}
Please check the image which shows the error which I am getting.I have tried to change the FileAccess multiple times but not getting any luck.
This post explains how to both read and write to a file using a single stream:
How to both read and write a file in C#
Consider you may have the file still open from a previous erroneous run. Use a tool like Sysinternals Process Monitor or Unlocker to verify it isn't open by another instance.
How to use Process Monitor:
http://www.manuelmeyer.net/2013/09/tooltips-unlocking-files-with-sysinternals-process-monitor/
Both source and dest are referencing the same file. In the first instance (source) you open it exclusively (ie not shared).
In the second instance (dest) you now want to create the file which you opened in the first instance, but allow it to be shared.
Since the source is already open an in use you cannot write over the top of it using dest.
I think what you may be really want is to have the path parameter for the dest to be different to path parameter for the source, since you are essentially trying to re-write the same data into the same file at the same location right now.
I'm attempting to use StreamReader and StreamWriter to grab a temporary output log (.txt format) from another application.
The output log is always open and constantly written to.
Unhelpfully if the application closes or crashes, the log file ends up deleted - hence the need for a tool that can grab the information from this log and save it.
What my program currently does is:
Create a new .txt file, and stores the path of that file as the
string "destinationFile".
Finds the .txt log file to read, and stores the path of that file as
the string "sourceFile"
It then passes those two strings to the method below.
Essentially I'm trying to read the sourceFile one line at a time.
Each time one line is read, it is appended to destinationFile.
This keeps looping until the sourceFile no longer exists (i.e. the application has closed or crashed and deleted its log).
In addition, the sourceFile can get quite big (sometimes 100Mb+), and this program may be handling more than one log at a time.
Reading the whole log rather than line by line will most likely start consuming a fair bit of memory.
private void logCopier(string sourceFile, string destinationFile)
{
while (File.Exists(sourceFile))
{
string textLine;
using (var readerStream = File.Open(sourceFile,
FileMode.Open,
FileAccess.Read,
FileShare.ReadWrite))
using (var reader = new StreamReader(readerStream))
{
while ((textLine = reader.ReadLine()) != null)
{
using (FileStream writerStream = new FileStream(destinationFile,
FileMode.Append,
FileAccess.Write))
using (StreamWriter writer = new StreamWriter(writerStream))
{
writer.WriteLine(textLine);
}
}
}
}
}
The problem is that my WPF application locks up and ceases to respond when it reaches this code.
To track down where, I put a MessageBox just before the writerStream line of the code to output what the reader was picking up.
It was certainly reading the log file just fine, but there appears to be a problem with writing it to the file.
As soon as it reaches the using (FileStream writerStream = new FileStream part of the code, it stops responding.
Is using the StreamWriter in this manner not valid, or have I just gone and dome something silly in the code?
Am also open to a better solution than what I'm trying to do here.
Simply what I understand is you need to copy a file from source to destination which may be deleted at any time.
I'll suggest you to use FileSystemWatcher to watch for source file changed event, then just simply copy the whole file from source to destination using File.Copy.
I've just solved the problem, and the issue was indeed something silly!
When creating the text file for the StreamWriter, I had forgotten to use .Dispose();. I had File.Create(filename); instead of File.Create(filename).Dispose(); This meant the text file was already open, and the StreamWriter was attempting to write to a file that was locked / in use.
The UI still locks up (as expected), as I've yet to implement this on a new thread as SteenT mentioned. However the program no longer crashes and the code correctly reads the log and outputs to a text file.
Also after a bit of refinement, my log reader/writer code now looks like this:
private void logCopier(string sourceFile, string destinationFile)
{
int num = 1;
string textLine = String.Empty;
long offset = 0L;
while (num == 1)
{
if (File.Exists(sourceFile))
{
FileStream stream = new FileStream(sourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
using (new StreamReader(stream))
{
stream.Seek(offset, SeekOrigin.Begin);
TextReader reader2 = new StreamReader(stream);
while ((textLine = reader2.ReadLine()) != null)
{
Thread.Sleep(1);
StreamWriter writer = new StreamWriter(destinationFile, true);
writer.WriteLine(textLine);
writer.Flush();
writer.Close();
offset = stream.Position;
}
continue;
}
}
else
{
num = 0;
}
}
}
Just putting this code up here in case anyone else is looking for something like this. :)
Here is my code:
public static TextWriter twLog = null;
private int fileNo = 1;
private string line = null;
TextReader tr = new StreamReader("file_no.txt");
TextWriter tw = new StreamWriter("file_no.txt");
line = tr.ReadLine();
if(line != null){
fileNo = int.Parse(line);
twLog = new StreamWriter("log_" + line + ".txt");
}else{
twLog = new StreamWriter("log_" + fileNo.toString() + ".txt");
}
System.IO.File.WriteAllText("file_no.txt",string.Empty);
tw.WriteLine((fileNo++).ToString());
tr.Close();
tw.Close();
twLog.Close();
It throws this error:
IOException: Sharing violation on path C:\Users\Water Simulation\file_no.txt
What i'm trying to do is just open a file with log_x.txt name and take the "x" from file_no.txt file.If file_no.txt file is empty make log file's name log_1.txt and write "fileNo + 1" to file_no.txt.After a new program starts the new log file name must be log_2.txt.But i'm getting this error and i couldn't understand what am i doing wrong.Thanks for help.
Well, you're trying to open the file file_no.txt for reading and for writing using separate streams. This may not work as the file will be locked by the reading stream, so the writing stream can't be created and you get the exception.
One solution would be to read the file first, close the stream and then write the file after increasing the fileNo. That way the file is only opened once at a time.
Another way would be to create a file stream for both read and write access like that:
FileStream fileStream = new FileStream(#"file_no.txt",
FileMode.OpenOrCreate,
FileAccess.ReadWrite,
FileShare.None);
The accepted answer to this question seems to have a good solution also, even though I assume you do not want to allow shared reads.
Possible alternate solution
I understand you want to create unique log files when your program starts. Another way to do so would be this:
int logFileNo = 1;
string fileName = String.Format("log_{0}.txt", logFileNo);
while (File.Exists(fileName))
{
logFileNo++;
fileName = String.Format("log_{0}.txt", logFileNo);
}
This increases the number until it finds a file number where the log file doesn't exist. Drawback: If you have log_1.txt and log_5.txt, the next file won't be log_6.txt but log_2.txt.
To overcome this, you could enumerate all the files in your directory with mask log_*.txt and find the greatest number by performing some string manipulation.
The possibilities are endless :-D
Well this may be old but the accepted answer didn't work for me. This is caused when you try to Read or Write a file you just created from a separate stream. Solving this is very simple, just dispose the filestream you used in creating it and then you can access the file freely.
if (!File.Exists(myfile))
{
var fs = new FileStream(fav, FileMode.Create);
fs.Dispose();
string text = File.ReadAllText(myfile);
}
enter image description here
var stream = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
resizedBitmap.Compress(Bitmap.CompressFormat.Png, 200, stream); //problem here
stream.Close();
return resizedBitmap;
In the Compress method, I was passing the value of the quality parameter as 200, which sadly doesn't allows values outside the range 0-100.
I changed back the value of quality to 100 and the issue got fixed.
None of the proposed options helped me. But I found a solution:
In my case, the problem was with Anti-Virus, with intensive writing to a file, Anti-Virus started scanning the file and at that moment there was a problem with writing to the file.
I downloaded the source project from the website, using as is, except I changed the target file from upload.php to upload.aspx, which contains the following code to receive the file data:
int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
string fileName = Path.GetFileName(Request.Files[0].FileName);
// Read stream
BinaryReader br = new BinaryReader(Request.InputStream);
byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
//byte[] appended = buffer.Take(149).ToArray();
// Write stream
BinaryWriter bw = new BinaryWriter(File.Open(Server.MapPath("~/uploadfiles" + fileName), chunk == 0 ? FileMode.Create : FileMode.Append));
bw.Write(buffer);
bw.Close();
The problem is when I upload a jpg file, or any other file, there is data prepended and appended to every chunk, that obviously makes the file corrupted, and increases the file size. Any idea why that would happen?
You ned to read from Request.Files[0] not from Request.InputStream.
see marco's post: here