Add using block for FTPClient

This commit is contained in:
miku-666
2022-12-23 20:58:58 +01:00
parent 78eccdd9bd
commit ab01b5ae1c
4 changed files with 40 additions and 35 deletions

View File

@@ -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;

View File

@@ -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");

View File

@@ -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");

View File

@@ -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();
}