my project is framework 4.0 so i use DotNetZip.
i have a code to save zip to E:
using (ZipFile zip = new ZipFile())
{
zip.AddFile(CGlobalVar.CurrentMissionFolder + "\\OutputTxt\\" + satuan.sName + "\\child.txt");
zip.AddFile(CGlobalVar.CurrentMissionFolder + "\\OutputTxt\\" + satuan.sName + "\\gps_default.txt");
zip.AddFile(CGlobalVar.CurrentMissionFolder + "\\OutputTxt\\" + satuan.sName + "\\master.txt");
zip.AddFile(CGlobalVar.CurrentMissionFolder + "\\OutputTxt\\" + satuan.sName + "\\slave.txt");
zip.AddFile(CGlobalVar.CurrentMissionFolder + "\\OutputTxt\\" + satuan.sName + "\\sys_info.txt");
zip.Save("E:\\" + satuan.sName + ".zip");
}
but when i try to Extract the file, the path also inside the zip file
Ops_V10\bin\Debug\data\CurrentMission\OutputTxt\BMS Server
the question is how to remove path my project inside the zip.
example
using (ZipFile zip = new ZipFile())
{
zip.AddFile("archive.txt");
zip.Save("E:\\archive.zip");
}
when i extract the file archive zip, it must direct to archive.txt
without path Ops_V10\bin\Debug\data\CurrentMission\OutputTxt\BMS Server
i try stack question but i want to extract it manually.
how to fix that?
AddFile has a second parameter.. you pass an empty string to it dotnetzip will insert the item at the root path within the archive.
...AddFile(FullName, "")
Related
I'm using C# in Visual Studio 2017 Community.
I have a working, fully functional program that I am trying to optimize. In a nutshell, the program reads text and numbers from text files, does some math and reformatting, then outputs to a new text file.
The part I am trying to optimize is the way lines of text are written to the output file. Here is what works:
using System.IO
// Let's start building the MA output text file now.
// Designate an output file --- put it in same directory as original files
// give MA file ame name as the original file -- but different extension.
maOutFile = dir + "\\" + serialNumber[f] + ".ma";
//Create MA header lines and write to file.
string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";
// Open up the file for writing
File.WriteAllText(maOutFile, maHeaderLine1);
File.AppendAllText(maOutFile, maHeaderLine2);
The above code works fine. My concern is that it's not very efficient, especially over a slow network. It opens and closes the file every time it writes to it.
So, to make this run faster, I thought I would try StreamWriter.
My StreamWriter code looks like this:
// Designate an output file --- put it in same directory as the original files
// give MA file same name as the original file -- with different extension.
// maOutFile = dir + "\\" + serialNumber[f] + ".ma";
StreamWriter outfile;
outfile = dir + "\\" + serialNumber[f] + ".ma";
//Create MA header lines and write to file.
string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";
// Open up the file for writing
//File.WriteAllText(maOutFile, maHeaderLine1);
//File.AppendAllText(maOutFile, maHeaderLine2);
outfile.WriteLine(maHeaderLine1);
outfile.WriteLine(maHeaderLine2);
Visual Studio doesn't like my new code using StreamWriter. It puts a red line under everything to the right of the equals sign in the line "outfile = dir + "\" + serialNumber[f] + ".ma";
When I hover my mouse over the redlined code, the message is "cannot implicitly convert type string to System.IO.StreamWriter".
What doesn't it like about my new code?
You are assigning a string variable to a StreamWriter object instead of creating a StreamWriter object.
Try the following
using (StreamWriter sw = new StreamWriter(dir + "\\" + serialNumber[f] + ".ma"))
{
string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";
// Open up the file for writing
//File.WriteAllText(maOutFile, maHeaderLine1);
sw.WriteLine(maHeaderLine1);
sw.WriteLine(maHeaderLine2);
}
You're trying to assign a string to a StreamWriter here:
outfile = dir + "\\" + serialNumber[f] + ".ma";
And looking at the code you're trying to open a file as a stream, something along the lines of:
string outFileDir = dir + "\\" + serialNumber[f] + ".ma";
StreamWriter outFile = new StreamWriter(outFileDir);
string fileName = dir + "\\" + serialNumber[f] + ".ma" ;
try
{
using (StreamWriter writer = new StreamWriter(fileName))
{
string maHeaderLine1 = "TITLE: S/N:" + serialNumber[f] + "\n";
string maHeaderLine2 = "ENGLISH(IN)/METRIC(MM) INDICATOR :IN-P\n";
writer.Write(maHeaderLine1 + maHeaderLine2);
}
}
catch(Exception exp)
{
Console.Write(exp.Message);
}
}
I am uploading file before that I want to delete existing file.
string newfilename = txtname.Text + "Resume" + fileExtension;
System.IO.File.Delete(newfilename);
AsyncFileUpload1.SaveAs(Server.MapPath("/tmp/jobres/" + uId) + "\\" + newfilename;
If I upload a .doc file, after that upload a .pdf file then both are there. I want only latest uploaded file not old file. How I can delete old file?
Before upload saveas, you can take file path for the directory and then delete all the existing files and then upload the latest file.
You can reference below code:
string newfilename = txtname.Text + "Resume" + fileExtension;
string[] pathOfFiles = System.IO.Directory.GetFiles(Server.MapPath("/tmp/jobres/" + uId));
foreach (string filePath in pathOfFiles)
{
System.IO.File.Delete(filePath);
}
AsyncFileUpload1.SaveAs(Server.MapPath("/tmp/jobres/" + uId) + "\\" + newfilename;
I Need save the txt file on a correct create folder. But its saving on C:\Nova Pasta i need save on "C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text whats is wrong ?
private void btn_SaveFile_Click(object sender, EventArgs e)
{
objSQL.Search_RGP_CadastroPrint(Convert.ToInt32(comboBox1.Text), str_list);
objSQL.SearchPrint(Convert.ToInt32(comboBox1.Text));
string path = #"C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
StreamWriter file = new System.IO.StreamWriter(path + ".txt");
file.WriteLine("---------------------------------------------------------------------------------------------------------");
file.WriteLine("Nome: " + valor.retorna_nome);
file.WriteLine("RGP: " + comboBox1.Text);
file.WriteLine("Endereço: " + valor.retorna_endereco);
file.WriteLine("Telefone: " + valor.retorna_telefone + " Celular: " + valor.retorna_celular + "\r\n");
str_list.ForEach(file.WriteLine);
file.Close();
}
Say valor.retorna_nome is "hello", and comboBox1.Text is "world". Your code does the following:
string path = #"C:\Nova pasta\"+valor.retorna_nome+comboBox1.Text;
// -> path = "C:\Nova pasta\helloworld"
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
// -> created directory "C:\Nova pasta\helloworld"
}
StreamWriter file = new System.IO.StreamWriter(path + ".txt");
// -> writes to file "C:\Nova pasta\helloworld.txt"
So it's doing exactly what you told it to. What would you like the directory to be called? And the filename?
Your String path is equals to something like that : "C:\Nova pasta\aNameXXX"
where :
aName = valor.retorna_nome
XXX = Combobox1.Text
You create a directory, this must success, but after that your file path is :
path+.txt : "C:\Nova pasta\aNameXXX.txt"
it's creating a file named (aNameXXX.txt) next to your folder.
you need to add an "\" and a name to your file to make a path like : "C:\Nova pasta\aNameXXX\FILENAME.txt"
StreamWriter file = new System.IO.StreamWriter(path + "\" + FILENAME + ".txt");
I want to save my text file in a F drive but this file is written to a default folder of program . How to save it by guiding a path
string[] contents = new string[2];
contents[0] = "Name: " + textBox1.Text;
contents[1] = "age: " + textBox2.Text;
string path = #"F:\\"; // path to file
System.IO.File.WriteAllLines(textBox1.Text + ".txt", contents);
It would be a good idea to actually use your path variable:
string path = System.IO.Path.Combine(#"F:\", textBox1.Text + ".txt");
System.IO.File.WriteAllLines(path, contents);
Because you defining a path,but you don't use it.
string path = #"F:\" + textBox1.Text + ".txt";
File.WriteAllLines(path, contents);
As an alternative, you can use File.Move after you created it like;
File.WriteAllLines(textBox1.Text + ".txt", contents);
File.Move(Directory.GetCurrentDirectory() + textBox1.Text + ".txt",
path + textBox1.Text + ".txt");
I did:
if (averagesListTextFile != null)
{
Directory.CreateDirectory(subDirectoryName);
File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt");
And then I want to do:
reader = new StreamReader(subDirectoryName + "\\" + averagesListTextFile + ".txt");
But I'm getting error say the file is in use by another process...And that happen only after I did the File.Create
File.Create returns a stream, so you need to dispose it:
using (File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt"))
{
}
or equivalently in this case:
File.Create(subDirectoryName + "\\" + averagesListTextFile + ".txt").Dispose();
But if you've just created the file, why would you try to read it? It will be empty.
Note that your reader should use a using statement too. Alternatively, to read and write complete text files, you should look into File.WriteAllText and File.ReadAllText, which make life simpler.