|
Great, that's exactly what I was looking for. Thanks!
|
|
|
|
|
checked is horribly slow. If you're working with the carry as a performance optimization, you're better off calculating it manually. See Hacker's Delight chapter 2.12, Overflow Detection.
|
|
|
|
|
Hello all, got myself a confusing mess here. I am going to provide as much information as possible, just let me know if something doesn't make sense.
On load, I have a file check and backup that is executed. The backup, doesn't work at all (the Move) and the create, only creates for the first item in the cluster. Here is the code.
string instText = comboInst.GetItemText(comboInst.SelectedItem);
string folder = instText.Substring(0, 4);
string path = @"C:\Nightly\Institutions\" + folder + "\\";
string email = folder + "Email.txt";
string sourceFile = @"C:\Nightly\Institutions\" + folder + "\\" + folder + "Email.txt";
string destinationFile = @"C:\Nightly\Institutions\" + folder + @"\Archive\";
foreach (string file in settingsFiles)
{
if (File.Exists(path + email))
{
File.Move(sourceFile, destinationFile);
}
if (!File.Exists(path + email))
{
Directory.CreateDirectory(path);
using (File.Create(path + email)) { }
}
What happens is, the file does NOT get moved if the file exists, and when it creates the file if a file doesn't exist, it only does it for the first item in the list, not each as you would expect a foreach statement to process.
If anyone could offer some insight on the wrongdoings of my ways I would appreciate it. Thanks!
|
|
|
|
|
If you run the code you've shown it will throw exceptions at the points where it is failing - examine those exceptions and you will see some of where you're going wrong.
Other points -
You need to check the archive directory exists and doesn't contain a file with that name before calling Move
I would append the filename to your destinationFile string - not just the path
Your loop is going to keep creating/moving the same file
|
|
|
|
|
Put your code in a try-catch block or step through your code to find the exact location of your error.
Once you fix it, your files should move from one location to the other.
|
|
|
|
|
What is in settingsFiles and why do you have a loop which uses the same file name each time through? There is also a bit of duplication in your paths in the above: sourceFile and path + email both evaluate to the same string.
Use the best guess
|
|
|
|
|
settingsFile is a string of settings files, no way to know how many there will be so that is why it loops. Here is the string.
string[] settingsFiles = Directory.GetFiles(@"C:\Nightly\Institutions\", "settings.ini", SearchOption.AllDirectories);
|
|
|
|
|
Yes, but why are you using it to create a loop when it has nothing to do with the file that you are trying to move?
Use the best guess
|
|
|
|
|
The loop, I think, has everything to do with the file. How else would I know what email files are in each directory?
For each *email.txt file in the directories specified do this
{
If the file exists in the directory, move it
}
{
If the file does NOT exist, create it
}
^ so for each file it sees in the string, it should be doing this.
So if I have this structure:
(Folder) (Email Exists?)
1 Y
2 Y
3 N
4 N
5 Y
the intent is to move the file from folder 1 to folder 1\Archive, then create a new one, same for folder 2, then folder 3 doesn't have one so it just creates it, nothing to backup there. Repeat for 4. then backup the file in folder 5 to folder 5\Archive.
Hope that helps with my logic.
|
|
|
|
|
Yes, but look at the code inside the loop. It changes nothing, but uses the same path and file names every time round, nor does it use the content of the item taken from the list.
Use the best guess
|
|
|
|
|
Richard I really appreciate you pointing me in the right direction, I took another look at my logic and you're right, it didn't make sense. It sounded good in my head, but I drew it out on paper to see what I was actually trying to accomplish and it didn't work. So here's what I did, and I am getting the looping result I am after, and I think I can go from here. Take a look.
same strings as above, plus this, different for each
foreach (string file in settingsFiles)
{
IniFile ini = new IniFile(file);
string settingNum = ini.IniReadValue("info", "settingNumber");
if (file.Contains(settingNum))
{
MessageBox.Show("This is setting number: " + settingNum);
continue;
}
}
I have a file call setting.ini and in each I have the category [info] and a generic setting called settingNumber. NOW on form load it does a message box that says "This is setting number: 1, then 2, then 3, then 4. Now to just play with it to do a check to see if the email file exists, if it does, move it, then it will create an email file for each direcotory that contains a settings file. Thanks! This is the best way to learn, being pointed in the right direction, by someone who isn't an a**hole haha. I haven't really had any experiences myself, but sometimes when I use my good friend google, you see some of the responses people give and they are just straight up buttheads. Thanks again :P
|
|
|
|
|
and the finished product! It needs some cleaning up, but for functionalities sake, I am happy with it. Thanks again.
foreach (string file in settingsFiles)
{
IniFile ini = new IniFile(file);
string settingNum = ini.IniReadValue("info", "settingNumber");
if (file.Contains(settingNum))
{
if (File.Exists(@"C:\Nightly\Institutions\" + settingNum + "\\" + settingNum + email))
{
MessageBox.Show("The file Exists for: " + settingNum);
File.Move(@"C:\Nightly\Institutions\" + settingNum + "\\" + settingNum + email, @"C:\Nightly\Institutions\" + settingNum + "\\Archive\\" + Parameters.DateOrTime.fileDate.ToString(Parameters.DateOrTime.formatFileDate) + " - " + settingNum + email);
}
using (File.Create(@"C:\Nightly\Institutions\" + settingNum + "\\" + settingNum + email)) { }
}
}
so a little background on what this does - it checks to see if a file already exists, if it does it will move it to the archive folder in the directory the file is located, and renames it to today's date - email.txt. Then it recreates a blank email to be written to in the future.
|
|
|
|
|
Makes a lot more sense.
Use the best guess
|
|
|
|
|
string[] files = GetFileList();
foreach (string file in files)
{
Download(file);
}
public string[] GetFileList()
{
string[] downloadFiles;
StringBuilder result = new StringBuilder();
WebResponse response = null;
StreamReader reader = null;
try
{
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/"));
reqFTP.UseBinary = true;
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
reqFTP.Proxy = null;
reqFTP.KeepAlive = false;
reqFTP.UsePassive = false;
response = reqFTP.GetResponse();
reader = new StreamReader(response.GetResponseStream());
string line = reader.ReadLine();
while (line != null)
{
result.Append(line);
result.Append("\n");
line = reader.ReadLine();
}
// to remove the trailing '\n'
result.Remove(result.ToString().LastIndexOf('\n'), 1);
return result.ToString().Split('\n');
}
catch (Exception ex)
{
if (reader != null)
{
reader.Close();
}
if (response != null)
{
response.Close();
}
downloadFiles = null;
return downloadFiles;
}
}
private void Download(string file)
{
try
{
string uri = "ftp://" + ftpServerIP + "/" + remoteDir + "/" + file;
Uri serverUri = new Uri(uri);
if (serverUri.Scheme != Uri.UriSchemeFtp)
{
return;
}
FtpWebRequest reqFTP;
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + remoteDir + "/" + file));
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
reqFTP.KeepAlive = false;
reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
reqFTP.UseBinary = true;
reqFTP.Proxy = null;
reqFTP.UsePassive = false;
FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
Stream responseStream = response.GetResponseStream();
FileStream writeStream = new FileStream(localDestnDir + "\" + file, FileMode.Create);
int Length = 2048;
Byte[] buffer = new Byte[Length];
int bytesRead = responseStream.Read(buffer, 0, Length);
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = responseStream.Read(buffer, 0, Length);
}
writeStream.Close();
response.Close();
}
catch (WebException wEx)
{
MessageBox.Show(wEx.Message, "Download Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Download Error");
}
}
|
|
|
|
|
|
Have a look at my article Application Auto-update via Online Files in C#[^] where I implement a progress bar showing the progress of a file download.
“That which can be asserted without evidence, can be dismissed without evidence.”
― Christopher Hitchens
|
|
|
|
|
can any one tell is jabber. i want to develop a chat client but i do not want to develop any server end but i want that my clients can talk to other client in same group. outer client will not be expose.
i want to develop chat client which can transfer multiple from one end to other end and also will be capable to transfer image data in form of stream. i hard the jabber client is capable of doing chat and there are already many jabber server so i do not need to maintain my own.
so guide me how i can develop chat client with jabber as a result two user can communicate each other and transfer file and send stream of data from one end to another end. thanks
tbhattacharjee
|
|
|
|
|
Tridip Bhattacharjee wrote: can any one tell is jabber I can't but this guy[^] can.
Cheers,
Peter
Software rusts. Simon Stephenson, ca 1994. So does this signature. me, 2012
|
|
|
|
|
Serverless?? So how are you going to tell the client what other client to connect to??
Servers are in place to make it easy for a client to identify and connect to another client without having to type in complicated DNS names or IP addresses and ports. If you really want your users to type that stuff in, great, have at it.
|
|
|
|
|
i said i will not develop server end. i heard jabber has server.so my all jabber client will connect to that server and interact in each other. if my concept is wrong then discuss please what should i do and also tell me how jabber client works. how people use jabber client to interact in each other. thanks
tbhattacharjee
|
|
|
|
|
You never said you were going to use the Jabber servers as your servers. Good luck with that!
You might want to read this so you get an idea of what you're getting yourself into: Jabber Client Programmer's Cheat Sheet[^]
|
|
|
|
|
What results did you rule out when you searched in Google? I would hate to duplicate work that you have already done. Does the Jabber site have any information?
|
|
|
|
|
With all these questions, it may help you to learn how to use Google; you will be amazed at how much time it saves you.
Use the best guess
|
|
|
|
|
If I want to serialize an object I have to use [Serializable] attribute and all member variables will be written to the file. What I don't know how to do versioning e.g. if I add a new member variable (rename a variable or just remove a variable) to the object e.g. m_dRadius and then I open (deserialize) the file how can I determine that the variable was initialized during the load or not.
I know that there are version tolerant approaches and I can mark variables with [OptionalField(VersionAdded = 1)] attribute. If I open an old file the framework will ignore this optional (new variable) and it will be just zero/null. But again how can I determine if the variable is initialized by load to zero or it was ignored.
I can write the class/object version number to the stream. Use the ISerializable approach and in the constructor(SerializationInfo oInfo, StreamingContext context) method read this version number. This will exactly tell me what is the class version in the stream.
However I expected that such kind of versioning is already implemented by the streaming framework. I tried to obtain the Assembly version from the SerializationInfo but it is always set to current version not to the version which was used when the object was saved.
What is the preferred approach? I found a lot of articles on the net, but I could not find a good solution for this...
Any help is appreciated
Thanks,
Abyss
|
|
|
|
|
Abyss wrote: What is the preferred approach?
If there was a preferred approach that applies in all circumstances, all other approaches would be dropped.
I'd go for the simplest solution; meaning the OptionalField if I'd expect that the class is not going to change much. Anything that could have more/complexer versions, I'd write a custom ISerializable.
Abyss wrote: However I expected that such kind of versioning is already implemented by the streaming framework. I tried to obtain the Assembly version from the SerializationInfo but it is always set to current version not to the version which was used when the object was saved.
It'd be overkill, and not everybody will be wanting to use those version-numbers. Keep in mind that the version-number you see is the one of the assembly that the class is contained in. Alternatively, one might use different version numbers for files, created with a single version of a class. So, the one-size-fits all wouldn't fit here.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|