『壹』 VB中如何獲取當前程序的絕對路徑
System.Environment.CurrentDirectory;
//例:c:/test/
Application.ExecutablePath;(包括名稱)
//例:c:/test/myapp.exe
Application.StartupPath;(不包括名稱)
//例:c:/test/
絕對路徑是直接到達目標位置,通常是從盤符開始的路徑。完整的描述文件位置的路徑就是絕對路徑,以web站點根目錄為參考基礎的目錄路徑。
絕對路徑名的指定是從樹型目錄結構頂部的根目錄開始到某個目錄或文件的路徑,由一系列連續的目錄組成,中間用斜線分隔,直到要指定的目錄或文件,路徑中的最後一個名稱即為要指向的目錄或文件。之所以稱為絕對,意指當所有網頁引用同一個文件時,所使用的路徑都是一樣的。
(1)獲取當前文件的路徑擴展閱讀
幾種編程語言獲取程序所在路徑的方法:
1、在golang程序裡面獲取程序所在路徑:
package main
import (
"path/filepath"
"os"
"fmt"
"log"
)
func main() {
execDir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err = nil {
log.Fatal(err)
}fmt.Println(execDir)
}
2、python腳本所在路徑:
import os
print(os.path.split(os.path.realpath(__file__))[0])
3、shell腳本獲取腳本的絕對路徑:
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
『貳』 c語言如何獲得文件當前路徑
C語言里
在main函數來的第二個參數裡面,自
保存著當前程序運行的目錄
也就是argv[0]
main( int argc, char *argv[])
{
printf("%s ", argv[0] );
}
就是文件當前所在位置
不過需要注意的一點是
這個路徑裡面保存了當前文件的文件名
如果你只是需要路徑的話還需要自己操作一下
main(int a,char *c[])
{
char s[100];
int i;
//把路徑保存到字元串s里
strcpy(s,c[0]);
for(i=strlen(s); i>0 ; i--)
if( s[i] == '\\')
{
s[i]='\0';
break;
}
//找到最後一個 \ 並刪除之後的內容
//最後輸出的s,就是當前文件的路徑了
puts(s);
}
『叄』 python文件名獲取文件路徑
使用os.path.abspath()函數來獲取文件絕對路徑
文件目錄結構如下:
os.path.abspath(path)返回path規范化的絕對路徑(但這個路徑不一定是真實的路徑),如果path僅是一個文件名,使用該函數後返回的路徑是當前工作目錄路徑連接改文件名後所組成的新的路徑名。
>>> import os.path
>>> os.path.abspath("a.py")
'C:\Users\Administrator\a.py'
os.path.split(path)將path分割成目錄和文件名二元組返回
>>> os.path.split("C:\Users\Administrator\a.py")
('C:\Users\Administrator', 'a.py')
os.path.dirname(path)返回path的目錄,其實就是os.path.split(path)的第一個元素
>>> os.path.dirname("C:\Users\Administrator\a.py")
'C:\Users\Administrator'
os.path.basename(path)返回path最後的文件名。如果path以/或結尾,就會返回空值。即os.path.split(path)的第二個元素。
>>> os.path.basename("C:\Users\Administrator\a.py")
'a.py'
os.path.commonprefix(list)返回list中所有path共有的最長的路徑,從左向右,相同字元。
os.path.exists(path)如果path存在,返回True;如果path不存在,返回False。
os.path.isabs(path)如果path是絕對路徑,返回True。
os.path.normpath(path)規范path字元串形式(規範文件路徑)
os.path.isfile(path)判斷路徑是否為文件,是返回True,否則返回False
os.path.isdir(path)如果path是一個存在的目錄,返回True,否則返貨False。
os.path.islink(path)是否是鏈接;但如果系統不支持鏈接,則返回False。
『肆』 如何用perl獲取某一文件所在路徑
用perl獲取某一文件所在路徑,參考代碼如下:x0dx0ax0dx0ause Cwd 'abs_path'x0dx0aprint abs_path($0)."\n";x0dx0aman Cwdx0dx0aNAMEx0dx0aCwd - get pathname of current working Cwd;x0dx0amy $dir = getcwd;x0dx0ause Cwd 'abs_path'x0dx0amy $abs_path = abs_path($file);
『伍』 如何獲得當前java文件的路徑
public class Test {
public static void main(String[] args) {
String path = "Test.java";
File file = new File(path);
System.out.println(file.getAbsoluteFile());
}
}
-----
運行結果:
D:\workspaces\studyStruts2\Test.java
不加任何路徑,就是指當版前路徑
望採納權
『陸』 如何獲取執行程序的當前路徑
C#獲取當前應用程序所在路徑及環境變數
一、獲取當前文件的路徑
string str1=Process.GetCurrentProcess().MainMole.FileName;//可獲得當前執行的的文件名。
string str2=Environment.CurrentDirectory;//獲取和設置當前目錄(即該進程從中啟動的目錄)的完全限定路徑。(備注:按照定義,如果該進程在本地或網路驅動器的根目錄中啟動,則此屬性的值為驅動器名稱後跟一個尾部反斜杠(如「C:\」)。如果該進程在子目錄中啟動,則此屬性的值為不帶尾部反斜杠的驅動器和子目錄路徑[如「C:\mySubDirectory」])。
string str3=Directory.GetCurrentDirectory(); //獲取應用程序的當前工作目錄。
string str4=AppDomain.CurrentDomain.BaseDirectory;//獲取基目錄,它由程序集沖突解決程序用來探測程序集。
string str5=Application.StartupPath;//獲取啟動了應用程序的可執行文件的路徑,不包括可執行文件的名稱。
string str6=Application.ExecutablePath;//獲取啟動了應用程序的可執行文件的路徑,包括可執行文件的名稱。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//獲取或設置包含該應用程序的目錄的名稱。
1. System.Diagnostics.Process.GetCurrentProcess().MainMole.FileName
獲取模塊的完整路徑。
2. System.Environment.CurrentDirectory
獲取和設置當前目錄(該進程從中啟動的目錄)的完全限定目錄。
3. System.IO.Directory.GetCurrentDirectory()
獲取應用程序的當前工作目錄。這個不一定是程序從中啟動的目錄啊,有可能程序放在C:\www里,這個函數有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有時不一定返回什麼東東,這是任何應用程序最後一次操作過的目錄,比如你用Word打開了E:\doc\my.doc這個文件,此時執行這個方法就返回了E:\doc了。
4. System.AppDomain.CurrentDomain.BaseDirectory
獲取程序的基目錄。
5. System.Windows.Forms.Application.StartupPath
獲取啟動了應用程序的可執行文件的路徑。效果和2、5一樣。只是5返回的字元串後面多了一個"\"而已。
6. System.Windows.Forms.Application.ExecutablePath
獲取啟動了應用程序的可執行文件的路徑及文件名,效果和1一樣。
7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase
獲取和設置包括該應用程序的目錄的名稱。
二、操作環境變數
利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系統環境變數,如:System.Environment.GetEnvironmentVariable("windir")就可以取得windows系統目錄的路徑。
以下是一些常用的環境變數取值:
System.Environment.GetEnvironmentVariable("windir");
System.Environment.GetEnvironmentVariable("INCLUDE");
System.Environment.GetEnvironmentVariable("TMP");
System.Environment.GetEnvironmentVariable("TEMP");
System.Environment.GetEnvironmentVariable("Path");
三、應用實例
編寫了一個WinForm程序,項目文件存放於D:\Projects\Demo,編譯後的文件位於D:\Projects\Demo\bin\Debug,最後的結果如下:
1、System.Diagnostics.Process.GetCurrentProcess().MainMole.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe
2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug
3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug
4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\
5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\
6、System.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug
7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE
System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS
System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\
System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp
System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\
『柒』 java怎樣獲取當前目錄路徑
很多朋友都想知道java如何獲取當前目錄路徑?下面就一起來了解一下吧~
1、利用System.getProperty()函數獲取當前路徑:
System.out.println(System.getProperty("user.dir"));//user.dir指定了當前的路徑
2、使用File提供的函數獲取當前路徑:
File directory = new File("");//設定為當前文件夾 try{ System.out.println(directory.getCanonicalPath());//獲取標準的路徑 System.out.println(directory.getAbsolutePath());//獲取絕對路徑 }catch(Exceptin e){} File.getCanonicalPath()和File.getAbsolutePath()大約只是對於new File(".")和new File("..")兩種路徑有所區別。 # 對於getCanonicalPath()函數,「."就表示當前的文件夾,而」..「則表示當前文件夾的上一級文件夾 # 對於getAbsolutePath()函數,則不管」.」、「..」,返回當前的路徑加上你在new File()時設定的路徑 # 至於getPath()函數,得到的只是你在new File()時設定的路徑 比如當前的路徑為 C:/test : File directory = new File("abc"); directory.getCanonicalPath(); //得到的是C:/test/abc directory.getAbsolutePath(); //得到的是C:/test/abc direcotry.getPath(); //得到的是abc File directory = new File("."); directory.getCanonicalPath(); //得到的是C:/test directory.getAbsolutePath(); //得到的是C:/test/. direcotry.getPath(); //得到的是. File directory = new File(".."); directory.getCanonicalPath(); //得到的是C:/ directory.getAbsolutePath(); //得到的是C:/test/.. direcotry.getPath(); //得到的是.. 另外:System.getProperty()中的字元串參數如下: System.getProperty()參數大全 # java.version Java Runtime Environment version # java.vendor Java Runtime Environment vendor # java.vendor.url Java vendor URL # java.home Java installation directory # java.vm.specification.version Java Virtual Machine specification version # java.vm.specification.vendor Java Virtual Machine specification vendor # java.vm.specification.name Java Virtual Machine specification name # java.vm.version Java Virtual Machine implementation version # java.vm.vendor Java Virtual Machine implementation vendor # java.vm.name Java Virtual Machine implementation name # java.specification.version Java Runtime Environment specification version # java.specification.vendor Java Runtime Environment specification vendor # java.specification.name Java Runtime Environment specification name # java.class.version Java class format version number # java.class.path Java class path # java.library.path List of paths to search when loading libraries # java.io.tmpdir Default temp file path # java.compiler Name of JIT compiler to use # java.ext.dirs Path of extension directory or directories # os.name Operating system name # os.arch Operating system architecture # os.version Operating system version # file.separator File separator ("/" on UNIX) # path.separator Path separator (":" on UNIX) # line.separator Line separator ("/n" on UNIX) # user.name User』s account name # user.home User』s home directory # user.dir User』s current working directory
JAVA中獲取路徑 關鍵字: java中獲取路徑
1、jsp中取得路徑:
以工程名為TEST為例:
(1)得到包含工程名的當前頁面全路徑:request.getRequestURI() 結果:/TEST/test.jsp (2)得到工程名:request.getContextPath() 結果:/TEST (3)得到當前頁面所在目錄下全名稱:request.getServletPath() 結果:如果頁面在jsp目錄下 /TEST/jsp/test.jsp (4)得到頁面所在伺服器的全路徑:application.getRealPath("頁面.jsp") 結果:D:/resin/webapps/TEST/test.jsp (5)得到頁面所在伺服器的絕對路徑:absPath=new java.io.File(application.getRealPath(request.getRequestURI())).getParent(); 結果:D:/resin/webapps/TEST
2、在類中取得路徑: (1)類的絕對路徑:Class.class.getClass().getResource("/").getPath() 結果:/D:/TEST/WebRoot/WEB-INF/classes/pack/ (2)得到工程的路徑:System.getProperty("user.dir") 結果:D:/TEST
『捌』 如何獲取正確的文件路徑
打開電腦上的文件夾,選中一個文件,可以看到此文件的完整路徑並非用反斜杠字元分隔文件夾名來顯示完整的文件或文件夾路徑。
接下來請點擊地址欄空白處
點擊地址欄空白處後,此時在地址欄就顯示了完整的文件路徑,
選中顯示出來的完整文件路徑,滑鼠右鍵,從快捷菜單中選擇復制,即可把此文件的路徑完整復制下來了。
還有一個方法:按shift鍵不放,再右擊文件從彈出的快捷菜單中選擇「復制為路徑」,也能拷貝一個文件的完整路徑,如下圖所示。"E:電腦資料備份資料庫語言K3如何在資料庫更改計量單位.doc"
在需要的地方比如在運行中粘貼一下,即可顯示出來此文件的完整路徑。
點擊「確定」按鈕後,就可以直接打開此WORD文檔
『玖』 怎麼獲取文件路徑和目錄
File類有兩個常用方法可以得到文件路徑一個是:getCanonicalPath(),另一個是:getAbsolutePath(),可以通過File類的實例調用這兩個方法例如file.getAbsolutePath()其中file是File的實例對象。下面是一個具體例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之處在於,getCanonicalPath()得到的是一個規范的路徑,而getAbsolutePath()是用構造File對象的路徑+當前工作目錄。例如在上面的例子中.(點號)代表當前目錄。getCanonicalPath()就會把它解析為當前目錄但是getAbsolutePath()會把它解析成為目錄名字(目錄名字是點號)。
下面是上面程序在我電腦上的輸出:
G:\xhuoj\konw\.\src\
G:\xhuoj\konw\src\
『拾』 win7文件路徑怎麼獲得
自從win7系統以後,所有的windows系統中的文件夾都是以分層樣式來顯示的,所以一些用戶在升級到win7後以上版本後不知道如何獲取文件夾路徑,故此這里小編為大家帶來了獲取文件夾/文件路徑的方法,其實在win7系統中獲取文件路徑也是非常簡單的,下面我們一起看下。
win7查看文件路徑的方法1.首先,將電腦返回到桌面的界面,然後找到桌面的計算機圖標並雙擊打開,並在其中找到需要操作的那個文件夾,也是需要獲取路徑的文件夾。小編這里就使用Documents這個文件路徑來舉例吧!不妨看看上方地址欄中的顯示,並非就是文件夾的路徑位置。
2.如果需要找到該文件夾的絕對路徑的話,其實也很簡單,只需要將滑鼠移動到地址欄中的空白處,然後單擊一下左鍵,地址欄中就會立馬出現該文件夾的路徑了。
上述方法就是我們獲取文件夾路徑的方法了,是不是非常的簡單呢?該方法獲取其實和winxp等原來系統是一樣的,只不過將原來地址以分層樣式顯示的。