Resource folder / Folder Prob....

已答覆 Resource folder / Folder Prob....

  • 2012年4月25日 上午 02:52
     
     

    I added a folder to a project i have and inside that folder are other folders with txt. docs in them what i'm tring to do is, lets say that the folder name that i added is named "StoreNames" and inside that folder is a txt.doc name "First and another one that is "Last" I want to check to see if they are there and if not recreate them( Just in case they get deleted and will keep the app from crashing):

    if (checkbox == true)

    {

       // then check to see if the txt is in the folder loacation.

     //if not the remake it.

    }

    I can get the folder to show up on the location ( Project1.Properties.Resources. ) to check???

    Please help

所有回覆

  • 2012年4月25日 上午 03:08
     
     

    What I would do is store the path names in Project1.Properties.Settings.Default.Paths of type System.Collections.Specialized.StringCollection. Then just loop through all the Paths to see if they exist using:

    System.IO.File.Exits(...);

    So you want to use Settings.Default (found on the "Settings" tab) instead of Resources.


    Dan Randolph

  • 2012年4月25日 下午 05:35
     
     

    Can you put a little code sample (Still learning ... If I see it I can understand it better)

    The folder that I made is in the project with the resource folder... shouldn't you be able to give a direct path, so that you were ever you install it.. ( If not in C dir) it will allways find it ... I thought that I saw a function .. like "Getpath" would that work????(Don't know how to use it...)

    Thanks

  • 2012年4月26日 下午 03:35
     
      包含代碼

    Can you be more specific about what you want to do? I can't do a specific code sample if I don't have the requirements written clearly.

    Here is a general sample. Uses Visual Studio to add relative path strings like this screen shot (no direct coding required):

    Paths are relative to your exe location.

    Here is "directory exists" sample code:

    using System.IO;
    using PathsTest.Properties;
    
    namespace PathsTest
    {
      class Program
      {
        static void Main(string[] args)
        {
          int len = Settings.Default.Paths.Count;
          for (int i = 0; i < len; ++i)
          {
            Console.WriteLine("Path {0} exists = {1}", 
                               Settings.Default.Paths[i], 
                               System.IO.Directory.Exists(Settings.Default.Paths[i]));
          }
        }
      }
    }


    Dan Randolph


  • 2012年4月26日 下午 05:08
     
     

    Let me see if I got this right ... If I add a folder to the project I would have to use ( Not sure...)

    System.IO.Directory.Exists(Settings.Default.Paths.StoreNames.Names);

    And I would be looking for a folder for the "First.txt" file they is there and if not recreate it. ( I'm going to need to write to it...)

    I hope that clears up what I'm trying to do... if there is a better way please let me know....

    Thanks

  • 2012年4月26日 下午 05:17
     
      包含代碼

    To check directories use

    Directory.Exists("mypath");

    To check files use

    File.Exists(@"mypath\filename") ... or

    if (File.Exists(Settings.Default.Paths[0] + "First.txt")) {...} //combining example from above

    Dan Randolph

  • 2012年4月26日 下午 06:11
     
     

    OR....it is a Windows share and the file is copied or created by a windows process use the fileWatcher instead of a timer:

    http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

  • 2012年4月27日 上午 02:01
     
     

    Doing some research it think what i need is this, but it doent work???? when i added the folder with the text file in it it's in a folder in the project ...

    string path = AppDomain.CurrentDomain.BaseDirectory + @"\test1.txt";

    if (File.Exists(path))

    {

    MessageBox.Show("Worked");

    }

  • 2012年4月27日 上午 02:33
     
     
    Good luck to you!

    Dan Randolph

  • 2012年4月27日 上午 08:47
    版主
     
     

    Hi superlurker,

    How's it going? Do you have any updates about the previous issue?


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us

  • 2012年4月27日 下午 01:12
     
     

    Not realy that would work for me... I wasn't sure on how to get to a folder that i created in my project I think this is what I'm looking for but it doesn't work ... i think that the code is wrong..

    Doing some research it think what i need is this, but it doent work???? when i added the folder with the text file in it it's in a folder in the project ...

    string path = AppDomain.CurrentDomain.BaseDirectory + @"\test1.txt";

    if (File.Exists(path))

    {

    MessageBox.Show("Worked");

    }

  • 2012年5月1日 上午 05:51
    版主
     
     已答覆 包含代碼

    Hi superlurker,

    It seems that "StoreNames" is a folder which added in your project. If so, try the sample below.

                string path = System.Environment.CurrentDirectory;
                string path2 = path.Substring(0, path.LastIndexOf("bin")) + "StoreNames" + "\\test1.txt";
                if (File.Exists(path2))
                {
                    MessageBox.Show("Worked");
                }
                else
                    File.Create(path2);

    Have a nice day.


    Bob Shen [MSFT]
    MSDN Community Support | Feedback to us


  • 2012年5月1日 下午 04:33
     
     

    string StoreFolderCheck = Path.GetDirectoryName(Application.StartupPath.Substring(0, Application.StartupPath.LastIndexOf("\\"))) + "\\StoreNames";

    I used this .. it works ...but I got the idea from your code... this way were very i put it will work from there I made folder and file checks( don't want to crash if missing ...LOL

    Thanks



    • 已編輯 superlurker 2012年5月1日 下午 04:34
    •