diff --git a/PCK-Studio/Classes/Misc/FTPClient.cs b/PCK-Studio/Classes/Misc/FTPClient.cs index 099f9029..0bea41c3 100644 --- a/PCK-Studio/Classes/Misc/FTPClient.cs +++ b/PCK-Studio/Classes/Misc/FTPClient.cs @@ -15,9 +15,8 @@ namespace PckStudio.Classes.Misc private FtpWebResponse response = null; private Stream _stream = null; - public FTPClient(string host, string username) - : this(new Uri(host), username) + : this(new Uri(host), username, string.Empty) { } @@ -52,7 +51,8 @@ namespace PckStudio.Classes.Misc return request; } - public void DownloadFile(string remoteFilepath, string localFile) + // TODO: let it accept a destination Stream ? + public void DownloadFile(string remoteFilepath, string localFilepath) { try { @@ -67,23 +67,25 @@ namespace PckStudio.Classes.Misc response = (FtpWebResponse)request.GetResponse(); _stream = response.GetResponseStream(); - FileStream fileStream = new FileStream(localFile, FileMode.OpenOrCreate); byte[] buffer = new byte[Convert.ToInt32(GetFileSize(remoteFilepath))]; int num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); - try + + using (FileStream fileStream = new FileStream(localFilepath, FileMode.OpenOrCreate)) { - while (num > 0) + try { - fileStream.Write(buffer, 0, num); - num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); + while (num > 0) + { + fileStream.Write(buffer, 0, num); + num = _stream.Read(buffer, 0, Convert.ToInt32(GetFileSize(remoteFilepath))); + } + } + catch (Exception ex) + { + Console.WriteLine(ex.ToString()); } } - catch (Exception ex) - { - Console.WriteLine(ex.ToString()); - } - fileStream.Close(); _stream.Close(); response.Close(); request = null; diff --git a/PCK-Studio/Forms/Utilities/installPS3.cs b/PCK-Studio/Forms/Utilities/installPS3.cs index e8d306d5..b0c1e4b8 100644 --- a/PCK-Studio/Forms/Utilities/installPS3.cs +++ b/PCK-Studio/Forms/Utilities/installPS3.cs @@ -292,8 +292,8 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); - client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "")) + client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } } @@ -341,8 +341,8 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); - client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "")) + client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } buttonMode("stop"); diff --git a/PCK-Studio/Forms/Utilities/installVita.cs b/PCK-Studio/Forms/Utilities/installVita.cs index 28c90622..25203963 100644 --- a/PCK-Studio/Forms/Utilities/installVita.cs +++ b/PCK-Studio/Forms/Utilities/installVita.cs @@ -292,8 +292,8 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); - client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "")) + client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } } @@ -341,8 +341,8 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", ""); - client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "")) + client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); MessageBox.Show("PCK Replaced!"); } buttonMode("stop"); diff --git a/PCK-Studio/Forms/Utilities/installWiiU.cs b/PCK-Studio/Forms/Utilities/installWiiU.cs index 7e02d173..c988e283 100644 --- a/PCK-Studio/Forms/Utilities/installWiiU.cs +++ b/PCK-Studio/Forms/Utilities/installWiiU.cs @@ -347,8 +347,8 @@ namespace PckStudio.Forms if (openPCK.ShowDialog() == DialogResult.OK) { - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); - client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443")) + client.UploadFile(openPCK.FileName, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); if(TextBoxPackImage.Text != "") { string PackID = GetPackID(openPCK.FileName); @@ -412,8 +412,8 @@ namespace PckStudio.Forms if (listViewPCKS.SelectedItems.Count != 0) { buttonMode("loading"); - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); - client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443")) + client.UploadFile(mod, dlcPath + "/" + listViewPCKS.SelectedItems[0].Text + "/" + listViewPCKS.SelectedItems[0].Tag.ToString()); if (TextBoxPackImage.Text != "") { string PackID = GetPackID(mod); @@ -437,8 +437,8 @@ namespace PckStudio.Forms private void GetARCFromConsole() { - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); - client.DownloadFile(dlcPath + "../../Common/Media/MediaWiiU.arc", Program.AppData + "MediaWiiU.arc"); + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443")) + client.DownloadFile(dlcPath + "../../Common/Media/MediaWiiU.arc", Program.AppData + "MediaWiiU.arc"); archive = ARCFileReader.Read(new MemoryStream(File.ReadAllBytes(Program.AppData + "MediaWiiU.arc"))); } @@ -452,14 +452,17 @@ namespace PckStudio.Forms private void SendARCToConsole() { - FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443"); - MemoryStream ms = new MemoryStream(); - ARCFileWriter.Write(ms, archive); - File.WriteAllBytes(Program.AppData + "MediaWiiU.arc", ms.ToArray()); - client.UploadFile(Program.AppData + "MediaWiiU.arc", dlcPath + "../../Common/Media/MediaWiiU.arc"); - archive.Clear(); - currentPCK.Files.Clear(); - currentPCK = null; + using (FTPClient client = new FTPClient("ftp://" + textBoxHost.Text, "", "a3262443")) + { + MemoryStream ms = new MemoryStream(); + ARCFileWriter.Write(ms, archive); + File.WriteAllBytes(Program.AppData + "MediaWiiU.arc", ms.ToArray()); + client.UploadFile(Program.AppData + "MediaWiiU.arc", dlcPath + "../../Common/Media/MediaWiiU.arc"); + archive.Clear(); + currentPCK.Files.Clear(); + currentPCK = null; + ms.Dispose(); + } GC.Collect(); }