Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Determining if an inputted file name exists?

Determining if an inputted file name exists?

Scheduled Pinned Locked Moved Managed C++/CLI
helpquestioncsharpc++tutorial
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TabascoSauce
    wrote on last edited by
    #1

    Thanks for looking at my question! I am trying to figure out how to determine if a user-inputted file name exists, and I am completely unsure how to do this in .net C++. I have tried just using the C fopen code:

    if (fopen(String::Concat(room, ".txt"), "r") == 0)

    (room is the inputted variable name, and .txt is obviously the file extension) But I get a "cannot convert System::String ^ to const char *" error. I am assuming this is because fopen requires a constant name like "Bedroom.txt" and wont accept a variable as part of the name, so I need help determining another way to see if a file exists. Any help would be greatly appreciated. Thanks!

    D 1 Reply Last reply
    0
    • T TabascoSauce

      Thanks for looking at my question! I am trying to figure out how to determine if a user-inputted file name exists, and I am completely unsure how to do this in .net C++. I have tried just using the C fopen code:

      if (fopen(String::Concat(room, ".txt"), "r") == 0)

      (room is the inputted variable name, and .txt is obviously the file extension) But I get a "cannot convert System::String ^ to const char *" error. I am assuming this is because fopen requires a constant name like "Bedroom.txt" and wont accept a variable as part of the name, so I need help determining another way to see if a file exists. Any help would be greatly appreciated. Thanks!

      D Offline
      D Offline
      dybs
      wrote on last edited by
      #2

      Try Google.[^] For this method, I believe you'll need the full path. I would also recommend reading up on the System.IO namespace. Dybs

      T 1 Reply Last reply
      0
      • D dybs

        Try Google.[^] For this method, I believe you'll need the full path. I would also recommend reading up on the System.IO namespace. Dybs

        T Offline
        T Offline
        TabascoSauce
        wrote on last edited by
        #3

        Lol thanks! I actually just did this right before you linked me. But um, since I'm here. You wouldn't happen to know how to get the string value of a selected item in a list box would you?

        D 1 Reply Last reply
        0
        • T TabascoSauce

          Lol thanks! I actually just did this right before you linked me. But um, since I'm here. You wouldn't happen to know how to get the string value of a selected item in a list box would you?

          D Offline
          D Offline
          dybs
          wrote on last edited by
          #4

          listBox1.SelectedItem.ToString()

          Dybs

          T 1 Reply Last reply
          0
          • D dybs

            listBox1.SelectedItem.ToString()

            Dybs

            T Offline
            T Offline
            TabascoSauce
            wrote on last edited by
            #5

            Hmm I must be doing something wrong with a different part of my code then. I am trying to delete a file (in the same directory as the application) with a name that is respective to whats in a list box. For example there is a "doom.txt" file and the word "doom" in my list box. I select doom and hit the delete button and the "doom.txt" file should disappear. (what I'm doing is actually more complicated, but this is the part I'm having trouble with) Here is what my code looks like:

            		    if (roomList->SelectedIndex != -1 && delCheck->Checked)
            			{
            				roomList->Items->RemoveAt(roomList->SelectedItem);
            				File::Delete(String::Concat(Convert::ToString(roomList->SelectedValue), ".txt"));
            			}
            

            Any idea what I did wrong? Thanks for all the help :)

            T 1 Reply Last reply
            0
            • T TabascoSauce

              Hmm I must be doing something wrong with a different part of my code then. I am trying to delete a file (in the same directory as the application) with a name that is respective to whats in a list box. For example there is a "doom.txt" file and the word "doom" in my list box. I select doom and hit the delete button and the "doom.txt" file should disappear. (what I'm doing is actually more complicated, but this is the part I'm having trouble with) Here is what my code looks like:

              		    if (roomList->SelectedIndex != -1 && delCheck->Checked)
              			{
              				roomList->Items->RemoveAt(roomList->SelectedItem);
              				File::Delete(String::Concat(Convert::ToString(roomList->SelectedValue), ".txt"));
              			}
              

              Any idea what I did wrong? Thanks for all the help :)

              T Offline
              T Offline
              teejayem
              wrote on last edited by
              #6

              you're removing the selected item from the list box. Then the next line you're trying to get the selected value (which will be null because you removed it already!). String::Concat will then create a string as ".txt". Now you're trying to delete a file called ".txt" which probably doesn't exist. It probably seems like nothing is happening because from MSDN[^] it says: Deletes the specified file. An exception is not thrown if the specified file does not exist. You could do something like this:

              			 if (roomList->SelectedIndex > -1 && delCheck->Checked)
              			 {
              				 int nIndex = roomList->SelectedIndex;
              				 String ^sFileName = (String^)roomList->Items\[nIndex\];
              
              				 if (!File::Exists(sFileName))
              					 throw gcnew Exception("File does not exist!");
              
              				 File::Delete(sFileName);
              
              				 roomList->Items->RemoveAt(nIndex);
              			 }
              

              Don't be overcome by evil, but overcome evil with good

              T 1 Reply Last reply
              0
              • T teejayem

                you're removing the selected item from the list box. Then the next line you're trying to get the selected value (which will be null because you removed it already!). String::Concat will then create a string as ".txt". Now you're trying to delete a file called ".txt" which probably doesn't exist. It probably seems like nothing is happening because from MSDN[^] it says: Deletes the specified file. An exception is not thrown if the specified file does not exist. You could do something like this:

                			 if (roomList->SelectedIndex > -1 && delCheck->Checked)
                			 {
                				 int nIndex = roomList->SelectedIndex;
                				 String ^sFileName = (String^)roomList->Items\[nIndex\];
                
                				 if (!File::Exists(sFileName))
                					 throw gcnew Exception("File does not exist!");
                
                				 File::Delete(sFileName);
                
                				 roomList->Items->RemoveAt(nIndex);
                			 }
                

                Don't be overcome by evil, but overcome evil with good

                T Offline
                T Offline
                TabascoSauce
                wrote on last edited by
                #7

                Aha I see the logic in this now. It's funny how something so fundamentally obvious can completely escape my thought process haha. Anyway, thank you very much :D It appears to work now :D:D

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups