VS 2008 で C# Web アプリケーションを開発しています。ユーザーに入力ファイルを選択させてから、ファイル パスを文字列変数に格納します。ただし、このパスは として保存され"C:\\folder\\..."
ます。私の質問は、このファイルパスを単一の「\」に変換するにはどうすればよいですか?
皆さん、助けてくれてありがとう!私は ASP.NET 開発の初心者であるため、ご容赦ください。これは、コンテキスト内の私のコードの詳細です。まず、ディレクトリが存在するかどうかを確認します。ファイルが存在するかどうかを確認する場合、これを確認する必要はないと思います。しかし、これはまだ正しく動作するはずですか? そして現在、私の「パス」文字列変数は、必要な方法で表示されていません。この声明をどのように定式化するかわかりません。最終的に、ReadAllText ステートメントを実行したいと思います (最後の行を参照)。
protected void btnAppend_Click(object sender, EventArgs e)
{
string fullpath = Page.Request.PhysicalPath;
string fullPath2 = fullpath.Replace(@"\\", @"\");
if (!Directory.Exists(fullpath2))
{
string msg = "<h1>The upload path doesn't exist: {0}</h1>";
Response.Write(String.Format(msg, fullpath2));
Response.End();
}
string path = "@" + fullpath2 + uploadFile.PostedFile.FileName;
if (File.Exists(path))
{
// Create a file to write to.
try
{
StreamReader sr = new StreamReader(path);
string s = "";
while(sr.Peek() > 0)
s = sr.ReadLine();
sr.Close();
}
catch (IOException exc)
{
Console.WriteLine(exc.Message + "Cannot open file.");
return;
}
}
if (uploadFile.PostedFile.ContentLength > 0)
{
inputfile = System.IO.File.ReadAllText(path);