code_junkie

.NET Settings Relative Path

Nov 12th, 2011
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.22 KB | None | 0 0
  1. public string ExecutingAssemblyPath()
  2. {
  3. Assembly actualAssembly = Assembly.GetEntryAssembly();
  4. if (this.actualAssembly == null)
  5. {
  6. actualAssembly = Assembly.GetCallingAssembly();
  7. }
  8. return actualAssembly.Location;
  9. }
  10.  
  11. string absolutePath = Settings.Default.ImagePath;
  12. if(!Path.IsPathRooted(absolutePath))
  13. {
  14. string root = Assembly.GetEntryAssembly().Location;
  15. root = Path.GetDirectoryName(root);
  16. absolutePath = Path.Combine(root, absolutePath);
  17. }
  18.  
  19. new System.IO.FileInfo(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile).Directory;
  20.  
  21. Application.StartupPath
  22.  
  23. public static string RealAssemblyFilePath()
  24. {
  25. string dllPath=Assembly.GetExecutingAssembly().CodeBase.Substring(8);
  26. return dllPath;
  27. }
  28.  
  29. public static IEnumerable<DirectoryInfo> ParentDirs(this DirectoryInfo dir) {
  30. while (dir != null) {
  31. yield return dir;
  32. dir = dir.Parent;
  33. }
  34. }
  35. public static DirectoryInfo FindDataDir(string relpath, Assembly assembly) {
  36. return new FileInfo((assembly).Location)
  37. .Directory.ParentDirs()
  38. .Select(dir => Path.Combine(dir.FullName + @"", relpath))
  39. .Where(Directory.Exists)
  40. .Select(path => new DirectoryInfo(path))
  41. .FirstOrDefault();
  42. }
Add Comment
Please, Sign In to add comment