C# String must be filtered Through textbox???

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cnixuser
    New Member
    • Dec 2006
    • 80

    C# String must be filtered Through textbox???

    Hi, I am having trouble using a string in a "File.Move" operation to rename a file. Here is the statement in which I load the string with a value:
    Code:
    fileName += ASCIIEncoding.ASCII.GetString(fileByte);
    if I attempt to use the string in the File.Move operation directly:
    Code:
    File.Move("c:\\tempname.txt", "c:\\" + fileName);
    I get an error, but if I filter the value of the variable "fileName" through a text box
    Code:
    textbox1.Text = fileName;
    this operation:
    Code:
    File.Move("c:\\tempname.txt","c:\\"+textbox1.Text);
    works just fine. Any ideas? Any assistance that could be provided would be greatly appreciated.
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    Why is the filename coming in as bytes?

    Comment

    • cnixuser
      New Member
      • Dec 2006
      • 80

      #3
      Originally posted by Plater
      Why is the filename coming in as bytes?
      I am capturing it over a network stream.

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Have you set a breakpoint in there to see if there are any funny characters coming in the byte[]?

        Comment

        • cnixuser
          New Member
          • Dec 2006
          • 80

          #5
          Originally posted by Plater
          Have you set a breakpoint in there to see if there are any funny characters coming in the byte[]?
          No I have not ;however, why would the value when filtered through a text box act like a perfectly normal string when using the string directly would not?

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Originally posted by cnixuser
            No I have not ;however, why would the value when filtered through a text box act like a perfectly normal string when using the string directly would not?
            Because the textbox will remove any characters it can't figure out how to display.

            Comment

            • cnixuser
              New Member
              • Dec 2006
              • 80

              #7
              Originally posted by Plater
              Because the textbox will remove any characters it can't figure out how to display.
              Interesting, what would be a good way to mimic what the textbox does so that I could filter against any possible characters that would not be displayable in a text box and presumably not be useable for a file name?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Well first, figure out if that is actually the case by examining the byte[].
                I would guess they would either only be at the end or begining. If that is true, a simple .Trim() on the string would suffice.

                Comment

                • cnixuser
                  New Member
                  • Dec 2006
                  • 80

                  #9
                  Originally posted by Plater
                  Well first, figure out if that is actually the case by examining the byte[].
                  I would guess they would either only be at the end or begining. If that is true, a simple .Trim() on the string would suffice.
                  Well I found some interesting things out about the byte[]. The byte[] is 2048 characters in size when converted to a string. 1034 or so of the characters are legal including the whole file name, and then the first letter of the file name hundreds of times, and then the full name of the file again and about 1,014 illegal non-displayable characters. What I want to know is, how is filtering the string through a text box getting rid of all of this stuff? Below is the code I used to get the information on the byte, and the code I use to collect they byte.

                  Code:
                   int thisRead = 0;                 
                  int blockSize = 1024;                 
                  Byte[] dataByte = new Byte[blockSize];
                  Byte[] fileByte = new Byte[blockSize];
                  
                  int i = 0;
                                  while (true)
                                  {
                                        thisRead = networkStream.Read(fileByte, 0, blockSize);
                                        fileName += ASCIIEncoding.ASCII.GetString(fileByte);
                                        if (thisRead == 0) break;
                                  }
                                      string filterFileName = "";
                                      Regex r = new Regex("[0-9A-Za-z. ]");
                  
                                      MessageBox.Show(fileName.Length.ToString());
                                      while (i < fileName.Length)
                                      {
                                          Match m = r.Match(fileName, i);
                                          if (m.Success)
                                          {
                                              filterFileName += m.Value.ToString();
                                          }
                                          else
                                          {
                                              MessageBox.Show(filterFileName, "Illegal Character Detected");
                                              MessageBox.Show(i.ToString(), "i value");
                                          }
                                          i++;
                                          MessageBox.Show(filterFileName,"filterFileName");
                                      }
                                      string dPath = @"c:\"+filterFileName;
                                      File.Move(sPath, dPath);
                  any help / ideas is still quite appreciated :)

                  Comment

                  • Plater
                    Recognized Expert Expert
                    • Apr 2007
                    • 7872

                    #10
                    Well you are reading a full block size, and never dealing with only the amount of data read ("thisread" I think)
                    So if you have a datablock of 2048 and said read 2048, it might only have 100 characters...an d then fill in the rest of the datasize with garbage.
                    You could also be checking the amount of data available to be read on the stream and only reading that amount.

                    Comment

                    • cnixuser
                      New Member
                      • Dec 2006
                      • 80

                      #11
                      Originally posted by Plater
                      Well you are reading a full block size, and never dealing with only the amount of data read ("thisread" I think)
                      So if you have a datablock of 2048 and said read 2048, it might only have 100 characters...an d then fill in the rest of the datasize with garbage.
                      You could also be checking the amount of data available to be read on the stream and only reading that amount.
                      So how did filtering through the text box prevent the garbage from getting included in the value of textbox1.Text? Also what would you recommend as a good method for checking only the amount of data available to be read from the stream?
                      Last edited by cnixuser; Aug 6 '08, 08:03 PM. Reason: Additional question

                      Comment

                      • Plater
                        Recognized Expert Expert
                        • Apr 2007
                        • 7872

                        #12
                        Well your TcpClient object will tell you how much data is available to read with .Available property.
                        As for why it strips thing in Textbox I dunno, there's probably a single 0x00 byte at the end of the real set of data and it just stops at that.

                        Comment

                        • cnixuser
                          New Member
                          • Dec 2006
                          • 80

                          #13
                          Originally posted by Plater
                          Well your TcpClient object will tell you how much data is available to read with .Available property.
                          As for why it strips thing in Textbox I dunno, there's probably a single 0x00 byte at the end of the real set of data and it just stops at that.
                          Well for now I am sticking with the textbox workaround until I have time to develop a more structured solution, once I do I'll give your suggestion a try, thanks again for your help! :)
                          Last edited by cnixuser; Aug 6 '08, 08:18 PM. Reason: grammar

                          Comment

                          Working...